You can use Rules to detect a specific word and modify a query setting because of this word.
Limiting the search to only a subset of attributes
Use Case - Title or ID (SKU) searches
A good example of altering a query is with an online document library that allows keyword searches inside documents. If a user types in the word “article ref21”, they are probably signalling to the system that they are looking for an article whose title or ID contains “ref21”.
Rule
If query = “article ref21” then remove article and search for an article whose title or ID contains ‘ref21’
API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var response = await client . SaveRuleAsync (
"ALGOLIA_INDEX_NAME" ,
"article-rule" ,
new Rule
{
ObjectID = "article-rule" ,
Conditions = new List < Condition >
{
new Condition { Pattern = "article" , Anchoring = Enum . Parse < Anchoring >( "StartsWith" ) },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Query = new ConsequenceQuery (
new ConsequenceQueryObject
{
Edits = new List < Edit >
{
new Edit { Type = Enum . Parse < EditType >( "Remove" ), Delete = "article" },
},
}
),
RestrictSearchableAttributes = new List < string > { "title" , "book_id" },
},
},
}
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
final response = await client . saveRule (
indexName: "ALGOLIA_INDEX_NAME" ,
objectID: "article-rule" ,
rule: Rule (
objectID: "article-rule" ,
conditions: [
Condition (
pattern: "article" ,
anchoring: Anchoring . fromJson ( "startsWith" ),
),
],
consequence: Consequence (
params: ConsequenceParams (
query: ConsequenceQueryObject (
edits: [
Edit (
type: EditType . fromJson ( "remove" ),
delete: "article" ,
),
],
),
restrictSearchableAttributes: [
"title" ,
"book_id" ,
],
),
),
),
);
1
2
3
4
5
6
7
8
9
10
11
12
13
response , err := client . SaveRule ( client . NewApiSaveRuleRequest (
"ALGOLIA_INDEX_NAME" , "article-rule" ,
search . NewEmptyRule () . SetObjectID ( "article-rule" ) . SetConditions (
[] search . Condition { * search . NewEmptyCondition () . SetPattern ( "article" ) . SetAnchoring ( search . Anchoring ( "startsWith" ))}) . SetConsequence (
search . NewEmptyConsequence () . SetParams (
search . NewEmptyConsequenceParams () . SetQuery ( search . ConsequenceQueryObjectAsConsequenceQuery (
search . NewEmptyConsequenceQueryObject () . SetEdits (
[] search . Edit { * search . NewEmptyEdit () . SetType ( search . EditType ( "remove" )) . SetDelete ( "article" )}))) . SetRestrictSearchableAttributes (
[] string { "title" , "book_id" })))))
if err != nil {
// handle the eventual error
panic ( err )
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
client . saveRule (
"ALGOLIA_INDEX_NAME" ,
"article-rule" ,
new Rule ()
. setObjectID ( "article-rule" )
. setConditions ( Arrays . asList ( new Condition (). setPattern ( "article" ). setAnchoring ( Anchoring . STARTS_WITH )))
. setConsequence (
new Consequence ()
. setParams (
new ConsequenceParams ()
. setQuery ( new ConsequenceQueryObject (). setEdits ( Arrays . asList ( new Edit (). setType ( EditType . REMOVE ). setDelete ( "article" ))))
. setRestrictSearchableAttributes ( Arrays . asList ( "title" , "book_id" ))
)
)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const response = await client . saveRule ({
indexName : ' indexName ' ,
objectID : ' article-rule ' ,
rule : {
objectID : ' article-rule ' ,
conditions : [{ pattern : ' article ' , anchoring : ' startsWith ' }],
consequence : {
params : {
query : { edits : [{ type : ' remove ' , delete : ' article ' }] },
restrictSearchableAttributes : [ ' title ' , ' book_id ' ],
},
},
},
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var response = client . saveRule (
indexName = "ALGOLIA_INDEX_NAME" ,
objectID = "article-rule" ,
rule = Rule (
objectID = "article-rule" ,
conditions = listOf (
Condition (
pattern = "article" ,
anchoring = Anchoring . entries . first { it . value == "startsWith" },
),
),
consequence = Consequence (
params = ConsequenceParams (
query = ConsequenceQueryObject (
edits = listOf (
Edit (
type = EditType . entries . first { it . value == "remove" },
delete = "article" ,
),
),
),
restrictSearchableAttributes = listOf ( "title" , "book_id" ),
),
),
),
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$response = $client -> saveRule (
'ALGOLIA_INDEX_NAME' ,
'article-rule' ,
[ 'objectID' => 'article-rule' ,
'conditions' => [
[ 'pattern' => 'article' ,
'anchoring' => 'startsWith' ,
],
],
'consequence' => [ 'params' => [ 'query' => [ 'edits' => [
[ 'type' => 'remove' ,
'delete' => 'article' ,
],
],
],
'restrictSearchableAttributes' => [
'title' ,
'book_id' ,
],
],
],
],
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
response = client . save_rule (
index_name = "ALGOLIA_INDEX_NAME" ,
object_id = "article-rule" ,
rule = {
"objectID" : "article-rule" ,
"conditions" : [
{
"pattern" : "article" ,
"anchoring" : "startsWith" ,
},
],
"consequence" : {
"params" : {
"query" : {
"edits" : [
{
"type" : "remove" ,
"delete" : "article" ,
},
],
},
"restrictSearchableAttributes" : [
"title" ,
"book_id" ,
],
},
},
},
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
response = client . save_rule (
"ALGOLIA_INDEX_NAME" ,
"article-rule" ,
Algolia :: Search :: Rule . new (
algolia_object_id: "article-rule" ,
conditions: [ Algolia :: Search :: Condition . new ( pattern: "article" , anchoring: "startsWith" )],
consequence: Algolia :: Search :: Consequence . new (
params: Algolia :: Search :: ConsequenceParams . new (
query: Algolia :: Search :: ConsequenceQueryObject . new (
edits: [ Algolia :: Search :: Edit . new ( type: "remove" , delete: "article" )]
),
restrict_searchable_attributes: [ "title" , "book_id" ]
)
)
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
val response = client . saveRule (
indexName = "ALGOLIA_INDEX_NAME" ,
objectID = "article-rule" ,
rule = Rule (
objectID = "article-rule" ,
conditions = Some (
Seq (
Condition (
pattern = Some ( "article" ),
anchoring = Some ( Anchoring . withName ( "startsWith" ))
)
)
),
consequence = Consequence (
params = Some (
ConsequenceParams (
query = Some (
ConsequenceQueryObject (
edits = Some (
Seq (
Edit (
`type` = Some ( EditType . withName ( "remove" )),
delete = Some ( "article" )
)
)
)
)
),
restrictSearchableAttributes = Some ( Seq ( "title" , "book_id" ))
)
)
)
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let response = try await client . saveRule (
indexName : "ALGOLIA_INDEX_NAME" ,
objectID : "article-rule" ,
rule : Rule (
objectID : "article-rule" ,
conditions : [ SearchCondition ( pattern : "article" , anchoring : SearchAnchoring . startsWith )],
consequence : SearchConsequence ( params : SearchConsequenceParams (
restrictSearchableAttributes : [ "title" , "book_id" ],
query : SearchConsequenceQuery
. searchConsequenceQueryObject ( SearchConsequenceQueryObject ( edits : [ SearchEdit (
type : SearchEditType . remove ,
delete : "article"
)]))
))
)
)
Using the dashboard
You can also add your rules in your Algolia dashboard.
Select the Search product icon on your dashboard and then select your index.
Select the Rules section from the left sidebar menu in the Algolia dashboard .
Under the heading Rules , select the index you are adding a Rule to.
Click the New rule button.
Select Create your first rule or New rule . In the dropdown, click on the Manual Editor option.
In the Condition(s) section, keep Query toggled on, select Contains in the dropdown, and enter “article” in the input field.
In the Consequence(s) section:
Click the Add consequence button and select Add Query Parameter .
In the input field that appears, enter the JSON search parameter you want to add. For example, { "restrictSearchableAttributes": ["title","id"] }
.
Click the Add consequence button again and select Remove Word .
Type or select “article” in the input field.
Don’t forget to save your changes.
© Algolia · Privacy Policy · Cookie settings