You can use Query 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
| // turn json into an array
$rule = array(
'objectID' => 'article',
'condition' => array(
'pattern' => 'article',
'anchoring' => 'startsWith'
),
'consequence' => array(
'params' => array(
'query' => array(
'remove' => 'article'
),
'restrictSearchableAttributes' => array(
'title',
'book_id'
)
)
)
);
// push rule to index
$index->saveRule($rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| var rule = new Rule
{
ObjectID = "asap-rule",
Condition = new Condition { Anchoring = "startsWith", Pattern = "article" },
Consequence = new Consequence
{
Params = new ConsequenceParams
{
SearchableAttributes = new List<string> { "title", "book_id" },
Edits = new List<Edit>
{
new Edit {Type = EditType.Remove, Delete = "article"}
}
}
}
};
index.SaveRule(rule);
// Asynchronous
await index.SaveRuleAsync(rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Condition condition = new Condition().setPattern("article").setAnchoring("startsWith");
Consequence consequence = new Consequence();
List<Edit> edits = Collections.singletonList(new Edit().setDelete("article"));
ConsequenceParams params = new ConsequenceParams();
params.setRestrictSearchableAttributes(Arrays.asList("title","book_id"));
params.setConsequenceQuery(new ConsequenceQuery().setEdits(edits));
consequence.setParams(params);
Rule rule =
new Rule()
.setObjectID("article")
.setCondition(condition)
.setConsequence(consequence);
index.saveRule(rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| rule := search.Rule{
ObjectID: "article",
Condition: search.RuleCondition{Anchoring: search.StartsWith, Pattern: "article"},
Consequence: search.RuleConsequence{
Params: &search.RuleParams{
Query: search.NewRuleQueryObject(
search.RuleQueryObjectQuery{
Edits: []search.QueryEdit{
search.RemoveEdit("article"),
},
},
),
QueryParams: search.QueryParams{
RestrictSearchableAttributes: opt.RestrictSearchableAttributes(
"title",
"book_id",
),
},
},
},
}
res, err := index.SaveRule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| val rule = Rule(
objectID = "article",
condition = Condition(
anchoring = "startsWith",
pattern = "article",
),
consequence = Consequence(
params = Some(
Map(
"query" -> Map("edits" -> Seq(Edit("delete", "article"))),
"restrictSearchableAttributes" -> Seq("title", "book_id"),
)
)
)
)
client.execute {
save rule rule inIndex "indexName"
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| val rules = rules {
rule(
"rule",
Condition(StartsWith, Literal("article")),
Consequence(
query = query {
restrictSearchableAttributes {
+"title"
+"book_id"
}
},
edits = edits { +"article" }
)
)
}
index.saveRules(rules)
|
Dashboard
You can also add your rules in your Algolia dashboard.
- Go to your dashboard and select your index.
- Click the Query Rules tab.
- Click the New rule button.
- In the Condition section:
- In the If the query… input field, set the anchoring to Contains.
- Type “article” in the input field and press
Enter
.
- In the Consequences section:
- Click the Add consequence button and select Add Query Parameter.
- In the Custom JSON Data panel that shows up, add:
{ "restrictSearchableAttributes": ["title","id"] }
- Click the Add consequence button and select Remove Word.
- Type “article” in the input field and press
Enter
.
- Click Save.