m_search (zotonic_core v1.0.0-rc.17)

The m_search model provides access to different kinds of search queries for searching through models.

Most searches in Zotonic are implemented in the mod_search module, searching through the rsc table in different kinds of ways.

Though, any module can implement a search by observing the search_query notification.

The search module is used inside templates. For example, the following snippet fetches the latest 10 modified pages in the "text" category:

{% for id in m.search[{latest cat="text" pagelen=10}] %}
    {{ m.rsc[id].title }}
{% endfor %}

Another example, searching for a text and requesting the second page with 20 results at a time:

{% for id, rank in m.search.paged[{fulltext text=query_string page=2 pagelen=20}] %}
    {{ m.rsc[id].title }}
{% endfor %}

Available Model API Paths

MethodPath patternDescription
get/paged/+searchname/...Run named search +searchname with request arguments and return paged search results.
get/count/+searchname/...Run named search +searchname and return only the total match count.
get/paged/+name_props_searchprops/...Run deprecated tuple-style search ({Name, Props}) and return paged results.
get/+name_props_searchprops/...Run deprecated tuple-style search ({Name, Props}) and return non-paged results.
get/pagedRun search from payload/query arguments and return paged results. No further lookups.
get/countRun search from payload/query arguments and return only the count. No further lookups.
get/+searchname/...Run named search +searchname with request arguments and return results.
get/Run search from payload/query arguments using the default search handling. No further lookups.

/+name marks a variable path segment. A trailing /... means extra path segments are accepted for further lookups.

See also

Search , pager tag , mod_search module , Custom search

Summary

Functions

m_get/3

search(S, Context)

-spec search(S, Context) ->
                #search_result{search_name :: binary() | atom(),
                               search_args :: map() | proplists:proplist(),
                               result :: list(),
                               page :: pos_integer(),
                               pagelen :: pos_integer() | undefined,
                               options :: z_search:search_options(),
                               total :: non_neg_integer() | undefined,
                               pages :: non_neg_integer() | undefined,
                               is_total_estimated :: boolean(),
                               next :: pos_integer() | false,
                               prev :: pos_integer(),
                               facets :: #{binary() => map()} | undefined}
                when
                    S ::
                        {atom(), proplists:proplist()} |
                        {binary(), map()} |
                        {binary(), proplists:proplist()},
                    Context :: z:context().

search(Name, Args, Context)

-spec search(binary(), map(), z:context()) ->
                {ok,
                 #search_result{search_name :: binary() | atom(),
                                search_args :: map() | proplists:proplist(),
                                result :: list(),
                                page :: pos_integer(),
                                pagelen :: pos_integer() | undefined,
                                options :: z_search:search_options(),
                                total :: non_neg_integer() | undefined,
                                pages :: non_neg_integer() | undefined,
                                is_total_estimated :: boolean(),
                                next :: pos_integer() | false,
                                prev :: pos_integer(),
                                facets :: #{binary() => map()} | undefined}} |
                {error, term()}.

search_pager(S, Context)

-spec search_pager(S, Context) ->
                      #search_result{search_name :: binary() | atom(),
                                     search_args :: map() | proplists:proplist(),
                                     result :: list(),
                                     page :: pos_integer(),
                                     pagelen :: pos_integer() | undefined,
                                     options :: z_search:search_options(),
                                     total :: non_neg_integer() | undefined,
                                     pages :: non_neg_integer() | undefined,
                                     is_total_estimated :: boolean(),
                                     next :: pos_integer() | false,
                                     prev :: pos_integer(),
                                     facets :: #{binary() => map()} | undefined}
                      when
                          S ::
                              {atom(), proplists:proplist()} |
                              {binary(), map()} |
                              {binary(), proplists:proplist()},
                          Context :: z:context().