Add Default Search Parameters with Rules

You can create Rules that add default search time parameters: these are parameters added 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 1000m. We will also set the validity period of this Rule to one month.

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
18
$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 index you are adding a Rule to.
2. Select the Query Rules tab and click on the New Rule button.
3. In the Conditions section, right below the Set condition(s) header, click on Query contains and select Universal.
4. In the Consequences section, click on Add consequence and select Add Query Parameter.
5. In the input field that appears enter the search parameter you want to add followed by a colon and the value you want to add. For example:\

1
2
3
{
  "aroundRadius" => 1000
}

String values should be written in quotation marks, number values and booleans should not. To get details on JSON syntax, checkout our page on record structure.

6. In the Rule Settings, under the Timeframe in UTC header, select a time range of one month.
7. If you want to forward the Rule to replicas indices, check the relevant box at the bottom of the page.
8. Don’t forget to save your changes.\

Did you find this page helpful?