Go to page content

Searchable Keywords

Adding custom keywords to a page and customer the indexer.

Why

Suppose you have a website with a very exciting page about a small village in the west of Ireland called "Ahascragh". When people want to find this page, they come to your site and type "ahascragh" into the search box. However, after studying your analytic software you  discover that people are typing "ahascraw" and not getting any results.

Assumptions

Readers are expected to be familiar with Erlang programming and Zotonic template development.

Solution

We can add a searchable text field to the text pages on a site to deal with common misspellings.
So here is how to do it:

  1. Create a template called _admin_edit_content.text.tpl, and save it in your sites template directory:

    {# Show extra keywords field for a text page #}
    {% with m.rsc[id] as r %}
    <div class="item-wrapper">
        <h3 class="above-item clearfix do_blockminifier { minifiedOnInit: false }">
            <span class="title">{_ Extra Keywords _}</span>
            <span class="arrow">{_ make smaller _}</span>
        </h3>
        <div class="item">
            <fieldset class="admin-form">
                <div class="notification notice">
                    {_ Specify extra Keywords.  _}
                </div>
                <div class="zp-80">
                    <div class="form-item clearfix">
                        <label for="extra_keywords">{_ Extra Keywords _}
                        </label>
                        <input id="extra_keywords" type="text"
                               name="extra_keywords"
                               value="{{ r.extra_keywords }}"
                               style="width: 80%" />
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
    {% endwith %} 
  2. In yoursite.erl create and export a function called do_custom_pivot

    -export([init/1, do_custom_pivot/2]).
    
    do_custom_pivot({custom_pivot, Id}, Context) ->
        Keywords = m_rsc:p(Id, extra_keywords, Context),
        {?MODULE, [{extra_keywords, Keywords}]}.
  3. In yoursite.erl, modify the init function to define and observe the custom pivot:

    init(Context) ->
        z_pivot_rsc:define_custom_pivot(?MODULE, [{extra_keywords, "text"}], Context),
        z_notifier:observe(custom_pivot,{?MODULE, do_custom_pivot}, Context).
  4. In the admin area of your site, go to System and click rebuild search indices, this could take some time, depending on how many pages your have on your site.

Troubleshooting

The data for your custom pivot is stored in a table called pivot_yoursite. If you think your keywords are not working, have a look and see if they have been copied into this table. If not, you could start debugging your do_custom_pivot function.

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