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 users haven’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. To trigger this rule, you must send landing as a ruleContexts on the frontend.

As soon as user begins typing, the rule is turned off. If you want a rule that’s always active for a particular context regardless of the query string, create a context-only rule. For more information, see Using rules to customize search results by platform.

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. Use either filters or context to determine 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 drop-down menu, click Visual Editor.
  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. 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 drop-down menu, click Manual Editor.
  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 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. Save your changes.

In JSON, you must write string values inside quotation marks, and number values and booleans without quotations. For more information about JSON syntax, see What is in a record.

Did you find this page helpful?