stable
  • User Guide
  • Developer Guide
  • Cookbooks
  • Best Practices
  • Reference
    • Modules
    • Actions
    • Controllers
    • All dispatch rules
    • Filters
    • Models
      • m_acl
      • m_acl_rule
      • m_acl_user_group
      • m_admin_blocks
      • m_admin_menu
      • m_admin_rsc
      • m_backup
      • m_backup_revision
      • m_category
      • m_comment
      • m_config
      • m_content_group
      • m_custom_redirect
      • m_edge
      • m_email_receive_recipient
      • m_email_status
      • m_facebook
      • m_filestore
      • m_hierarchy
      • m_identity
      • m_import_csv_data
      • m_l10n
      • m_log
      • m_log_email
      • m_mailinglist
      • m_media
      • m_modules
      • m_notifier
      • m_oauth_app
      • m_oauth_perms
      • m_page
      • m_persistent
      • m_predicate
      • m_req
      • m_rsc
      • m_rsc_gone
      • m_search
        • Implementing a custom search
      • m_session
      • m_signal
      • m_site
      • m_survey
      • m_tkvstore
      • m_translation
    • Services
    • Builtin Tags
    • Custom Tags
    • Validators
    • Directory structure
    • Icons
    • Notifications
    • Installation requirements
    • Global configuration
    • Site configuration
    • The Status site
    • Command-line
    • Transport
    • Troubleshooting
    • EDoc reference
  • Glossary
Zotonic
  • Docs »
  • Reference »
  • Models »
  • m_search
  • Edit on GitHub

m_search¶

  • Module: core

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 %}

See also

Search, the scomp-pager scomp, and the mod_search module.

Implementing a custom search¶

Like stated, any module can implement a search by observing the search_query notification:

 observe_search_query({search_query, Req, OffsetLimit}, Context) ->
     search(Req, OffsetLimit, Context).

search({foo_table, [{bar, Bar}]}, _OffsetLimit, _Context) ->
  #search_sql{
      select="f.id",
      from="foo f",
      where="f.bar = $1",
      order="f.baz desc",
      args=[Bar],
      tables=[{foo,"f"}]
  };

search(_Other, _OffsetLimit, _Context) ->
    undefined.

Do not forget to add the last function that matches and returns undefined for any other search request. If you forget this, the notification fold will crash when using any other search query.

This can then be used in your template like this:

{% for id in m.search[{foo bar=123}] %}
... looping over all ids in the 'foo' table for which bar = 123
{% endfor %}
Next Previous

© Copyright 2009–2017, The Zotonic Project (zotonic.com). Revision f600c2bd.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: stable
Versions
latest
stable
0.x
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.