Guides / Managing results / Rules / Rules Overview

Adding Default Search Parameters with Rules

You can create Rules that add default search parameters: we add these parameters to every search made to the Rule’s associated index. This is most useful when applied with a validity period. If you want to add always active parameters for an indefinite period of time, you should change your index’s settings.

This guide shows you how to create a Rule that sets the aroundRadius search parameter, for all queries, to 1000 meters. We will also set the validity period of this Rule to one month. This Rule is an example of a conditionless Rule.

Using the API

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$rule = array(
    'objectID' => 'a-rule-id',
    'consequence' => array(
        'params' => array(
            'aroundRadius' => 1000,
        )
    )
);

// set validity to 30 days
$rule['validity'] = array(
  array(
    'from' => time(),
    'until' => time() + 30*24*60*60,
  )
);
$index->saveRule($rule);

Using the dashboard

  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, click the Remove button with the trash can icon at the top right of the condition.
  5. In the Consequence(s) section, click on Add consequence and select Add Query Parameter.
  6. 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
    
    {
      "aroundRadius": 1000
    }
    
  7. In the Additional Settings, under the Timeframe in UTC header, select a time range of one month.
  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?