Go to page content

The Query search-model

Using the query search API you can generate lists of pages in various ways.

The query search-model is a powerful search model that accepts a lot of options. This page documents those options.

You access the query model in the following way:

{% for id in m.search[{query (options go here...) }] %}

For instance, to select all news items, ordered by their modification date, newest first:

{% for id in m.search[{query cat='news' sort='-rsc.modified'}] %}
	{{ id }}
{% endfor %}

Trying it out

Ofcourse you can create your own for-loop in a template, but there are more easier ways to check out the inner workings of the query-model: through your browser.

The queyr-model is exposed to the browser in (currently) 2 URLs: the Atom feed module for creating a customized update feed, and the API for receiving lists of ids in JSON.

Get all "documentation" pages of zotonic.com:
http://zotonic.com/api/search?cat=documentation

Get a feed of most recent documentation containg the word "filter":
http://zotonic.com/feed/search?cat=documentation&text=filter

Note that mod_atom_feed automatically appends a sorting on last-modified date, something which  api/search does not do.

All query-model arguments

ArgumentDescriptionExample
authoritativeBoolean, filters whether a page is considered "authoritative" (belonging on this site) or not.authoritative=1
catFilter pages on a specific category. Specifying multiple 'cat' arguments will do an "or" on the categories.cat='news'
cat_excludeFilter pages to exclude the given category.cat_exclude='meta'
hassubjectSelect all pages that have an outgoing connection to the given page, which is specified by the argument (123 in the example). Optionally, you can give the name of a predicate as second argument, to specify that the connection should have this predicate. Specifying this multiple times does an "or" of the conditions.hassubject=123
hassubject=[123,'author']
hasobjectLike hassubject, but selects all pages that have an incoming connection to the given page, which is specified by the argument. Optionally, you can give the name of a predicate as second argument, to specify that the connection should have this predicate.

hasobject=123
hasobject=[123,'document']

is_featuredA boolean option that specifies if a page should be featured or not.is_featured
is_publishedSelect published, unpublished or omit the publish check. Legal values are true,false or all.is_published='all'
is_publicFilter on whether an item is publicly visible or not. Valid values are 'true', 'false', 'all'.is_public='false'
upcomingSpecifying 'upcoming' means that you only want to select things that have a start date which lies in the future. Like the name says, useful to select upcoming events.upcoming
sort

Sort the result on a field. The name of the field is a string which directly refers to the sql-join that is being used. If you specify a dash ("-") in front of the field, the order is descending. Leaving this out or specifying a "+" means ascending.

Some sort fields:

  • rsc.modified - date of last modification
  • rsc.pivot_date_start - the start date specified in the admin
  • rsc.pivot_date_end - the end date specified in the admin
  • rsc.pivot_title - the title of the page. When making multilanguage sites, the behaviour of sorting on title is undefined.
For all the sort fields, you will have to consult the Zotonic's data model.
sort='-rsc.modified'
custompivotAdd a join on the given custom pivot table. The table is joined to the primary "rsc" table. The alias

custompivot=foo

(add pivot_foo to join)

hasobjectpredicateFilter on all things which have any outgoing edge with given predicate.hasobjectpredicate='document'
hassubjectpredicateFilter on all things which have any incoming edge with given predicate.hasobject='author'
textPerform a fulltext search on the primary "rsc" table. The result will automatically be ordered on the relevancy (rank) of the result.text="test"
query_idLoad the query arguments from the saved 'query' resource.query_id=331
publication_monthFilter on month of publication date 
publication_yearFilter on year of publication date 
date_start_afterSelect items with a start date greater than given valuedate_start_after="2010-01-01"
date_start_beforeSelect items with a start date smaller than given valuedate_start_before="2010-01-01"
date_start_yearSelect items with a "event start date" in the given year.date_start_year=2012
date_end_yearSelect items with a "event end date" in the given year.date_end_year=2012

This page is part of the Zotonic documentation, which is licensed under the Apache License 2.0.