Hiding Results
Hiding Results
You can use Query Rules to hide some records from the user, depending on the context.
Hiding a single item
Use Case
When a user types “harry potter”, you might want to exclude a discontinued book set.
Rule
*If query=harry potter then hide discontinued book set (where objectID = HP-12345)
API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// turn json into an array
$rule = array(
'objectID' => 'hide-12345',
'condition' => array(
'pattern' => 'Harry Potter',
'anchoring' => 'contains'
),
'consequence' => array(
'hide' => array(
'objectID' => 'HP-12345'
)
)
);
// push rule to index
$index->saveRule($rule);
Hiding a set of items
Use Case
You sell t-shirts and bananas. Some of your t-shirts are called “banana t-shirt”. If a user types in “banana” you don’t want to show t-shirts. So you hide all t-shirts from the search.
Rule
If query = “banana” then hide all t-shirts
You’ll need to create an attribute for all t-shirt items called t-shirt.
API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// turn json into an array
$rule = array(
'objectID' => 'Hide t-shirts',
'condition' => array(
'pattern' => 'banana',
'anchoring' => 'exact'
),
'consequence' => array(
'hide' => array(
'filters' => 't-shirts:false'
)
)
);
// push rule to index
$index->saveRule($rule['objectID'], $rule);
You can also add your rules in your Algolia dashboard.
- Go to your dashboard and select your index.
- Click the Configuration tab.
- In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the
clothing-type
attribute in the dropdown. - Go to the Query Rules tab and click the “New Rule” button.
- In the Condition section:
- Set the anchoring of “if the query…” to
Contains
. - Type “{facet:clothing-director} t-shirt” in the input field and press
Enter
.
- Set the anchoring of “if the query…” to
- In the Consequences section:
- Click the Add consequence button and select Remove Word.
- Type
t-shirt
in the input field and press enter.
- Don’t forget to save your changes!