Guides / Managing results / Rules / Merchandising

Merchandising on Empty Queries

You can create Rules that promote or hide items, boost, or bury categories, or activate filters even when the user hasn’t typed anything yet.

This guide shows you how to create a Rule that boosts the category clearance:true, whenever there is an empty query and the context is set to landing. You can use other consequences, such as promoting or hiding items, and boosting or burying categories, with the same condition. Note, to trigger this Rule, you must send landing as a ruleContexts on the front end.

As soon as user begins typing, we disable the Rule. If you want a Rule that is always active for a particular context regardless of the query string, create a Rule with context as the only condition. Our guide on using Rules to customize search results by platform shows how to create context based rules.

To boost, bury, or filter on values of a certain attribute, you must declare that attribute as an attributesForFaceting.

Rules on empty queries are useful for landing or category pages. It’s necessary to use either filters or context in the Rule conditions to differentiate between which category pages should trigger these Rules.

Using the API

To create a Rule with an API client, use the saveRule method on a Rule object with an empty search as the condition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// turn json into an array
$rule = array(
    'objectID' => 'clearance-category-filter',
    'conditions' => array(
      array(
        'pattern' => '',
        'anchoring' => 'is'
      ),
      array(
        'context' => 'landing',
      ),
    ),
    'consequence' => array(
        'params' => array(
            'optionalFilters' => 'clearance:true'
        )
    )
);
// push Rule to index
$index->saveRule($rule);

Using the Visual Editor

  1. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  2. Under the heading Rules, select the index you are adding a Rule to.
  3. Select Create your first rule or New rule. In the dropdown, click on the Visual Editor option.
  4. Select Set the search query.
  5. In the Your search section, right below the Define the query that triggers the rule header, select is and leave the next input box empty.
  6. In Add a context (optional), add the context “landing”. If you want to trigger this Rule whenever there is an empty query, regardless of context, leave this input box empty. Select Apply.
  7. Below What do you want to do? Select Boost categories.
  8. Fill in the Category section to be “clearance” is “true”.
  9. Select Apply. This will provide you a preview of the results.
  10. Before exiting, don’t forget to Save as Draft or Review and Publish.

Using the Manual Editor

  1. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  2. Under the heading Rules, select the index you are adding a Rule to.
  3. Select Create your first rule or New rule. In the dropdown, click on the Manual Editor option.
  4. In the Condition(s) section, keep Query toggled on, select Is, and leave the text input field empty.
  5. If you want to trigger this Rule whenever there is an empty query and for this specific context only, toggle Context on. In the dedicated input field, enter the context “landing”. Without including context as part of the condition, this Rule triggers on every empty query.
  6. In the Consequence(s) section, click on Add consequence and select Add Query Parameter.
  7. In the input field that appears, enter the JSON search parameter you want to add followed by a colon and the value you want to add. For example:
    1
    2
    3
    
    {
      "optionalFilters": "clearance:true"
    }
    
  8. If you want to forward the Rule to replicas or other indices, toggle Copy this rule to other indices, and enter the relevant indices.
  9. Don’t forget to save your changes.

In JSON, you must write string values inside quotation marks, and number values and booleans without quotations. To get further details on JSON syntax, checkout our page on record structure.

Did you find this page helpful?