Sometimes, specific terms can act as cues that you can use to filter search results. You can use positive, negative, or numerical filters:
- Positive filters include a specific subset of matching records in the results. For example, if a user types “diet” on a restaurant website, return every record that has “low-carb” or “low-fat”.
- Negative filters exclude matching records from results. For example, if a user types “gluten-free” on a restaurant website, you could filter out any gluten-containing meal.
- Numerical filters convert text queries into a numerical range. For example, if a user types “cheap” on a website for kitchen appliances, you could filter out anything costing more than $50.
Positive filters
If you want to filter out every non-diet-friendly meal whenever user’s search queries contain the term “diet”,
you could use the _tags
attribute to categorize meals depending on their individual qualities:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [
{
"name": "Chicken Stuffed Baked Avocados",
"restaurant": "The Hive",
"_tags": ["low-carb"]
},
{
"name": "Spinach Quiche",
"restaurant": "Bert's Inn",
"_tags": ["low-carb", "vegetarian"]
},
{
"name": "Pizza Chicken Bake",
"restaurant": "Millbrook Deli",
"_tags": ["cheese"]
},
{
"name": "Strawberry Sorbet",
"restaurant": "The Hive",
"_tags": ["low-fat", "vegetarian", "vegan"]
}
]
|
When users include the term “diet” in their search, you want to automatically return every record that has “low-carb” or “low-fat” in their _tags
attribute. Because _tags
is already optimized for filtering, you don’t have to set it as an attribute for faceting. You can directly create a new Rule that detects the term “diet” in a query and applies a positive filter on tags “low-carb” and “low-fat”.
To use the term “diet” only for filtering and not as a search term,
add a consequence in your rule to remove the word from your query.
Using the API
To add a rule, use the saveRule
method.
When creating a rule, you must define a condition and a consequence.
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",
"diet-rule",
new Rule
{
ObjectID = "diet-rule",
Conditions = new List<Condition>
{
new Condition { Pattern = "diet", Anchoring = Enum.Parse<Anchoring>("Contains") },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Filters = "'low-carb' OR 'low-fat'",
Query = new ConsequenceQuery(
new ConsequenceQueryObject
{
Edits = new List<Edit>
{
new Edit { Type = Enum.Parse<EditType>("Remove"), Delete = "diet" },
},
}
),
},
},
}
);
|
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
| final response = await client.saveRule(
indexName: "ALGOLIA_INDEX_NAME",
objectID: "diet-rule",
rule: Rule(
objectID: "diet-rule",
conditions: [
Condition(
pattern: "diet",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
filters: "'low-carb' OR 'low-fat'",
query: ConsequenceQueryObject(
edits: [
Edit(
type: EditType.fromJson("remove"),
delete: "diet",
),
],
),
),
),
),
);
|
1
2
3
4
5
6
7
8
9
10
11
12
| response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"ALGOLIA_INDEX_NAME", "diet-rule",
search.NewEmptyRule().SetObjectID("diet-rule").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("diet").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetFilters("'low-carb' OR 'low-fat'").SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetEdits(
[]search.Edit{*search.NewEmptyEdit().SetType(search.EditType("remove")).SetDelete("diet")})))))))
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",
"diet-rule",
new Rule()
.setObjectID("diet-rule")
.setConditions(Arrays.asList(new Condition().setPattern("diet").setAnchoring(Anchoring.CONTAINS)))
.setConsequence(
new Consequence()
.setParams(
new ConsequenceParams()
.setFilters("'low-carb' OR 'low-fat'")
.setQuery(new ConsequenceQueryObject().setEdits(Arrays.asList(new Edit().setType(EditType.REMOVE).setDelete("diet"))))
)
)
);
|
1
2
3
4
5
6
7
8
9
10
11
| const response = await client.saveRule({
indexName: 'indexName',
objectID: 'diet-rule',
rule: {
objectID: 'diet-rule',
conditions: [{ pattern: 'diet', anchoring: 'contains' }],
consequence: {
params: { filters: "'low-carb' OR 'low-fat'", query: { edits: [{ type: 'remove', delete: 'diet' }] } },
},
},
});
|
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 = "diet-rule",
rule = Rule(
objectID = "diet-rule",
conditions = listOf(
Condition(
pattern = "diet",
anchoring = Anchoring.entries.first { it.value == "contains" },
),
),
consequence = Consequence(
params = ConsequenceParams(
filters = "'low-carb' OR 'low-fat'",
query = ConsequenceQueryObject(
edits = listOf(
Edit(
type = EditType.entries.first { it.value == "remove" },
delete = "diet",
),
),
),
),
),
),
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $response = $client->saveRule(
'ALGOLIA_INDEX_NAME',
'diet-rule',
['objectID' => 'diet-rule',
'conditions' => [
['pattern' => 'diet',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['filters' => "'low-carb' OR 'low-fat'",
'query' => ['edits' => [
['type' => 'remove',
'delete' => 'diet',
],
],
],
],
],
],
);
|
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
| response = client.save_rule(
index_name="ALGOLIA_INDEX_NAME",
object_id="diet-rule",
rule={
"objectID": "diet-rule",
"conditions": [
{
"pattern": "diet",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"filters": "'low-carb' OR 'low-fat'",
"query": {
"edits": [
{
"type": "remove",
"delete": "diet",
},
],
},
},
},
},
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| response = client.save_rule(
"ALGOLIA_INDEX_NAME",
"diet-rule",
Algolia::Search::Rule.new(
algolia_object_id: "diet-rule",
conditions: [Algolia::Search::Condition.new(pattern: "diet", anchoring: "contains")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
filters: "'low-carb' OR 'low-fat'",
query: Algolia::Search::ConsequenceQueryObject.new(
edits: [Algolia::Search::Edit.new(type: "remove", delete: "diet")]
)
)
)
)
)
|
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
35
36
37
| val response = Await.result(
client.saveRule(
indexName = "ALGOLIA_INDEX_NAME",
objectID = "diet-rule",
rule = Rule(
objectID = "diet-rule",
conditions = Some(
Seq(
Condition(
pattern = Some("diet"),
anchoring = Some(Anchoring.withName("contains"))
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
filters = Some("'low-carb' OR 'low-fat'"),
query = Some(
ConsequenceQueryObject(
edits = Some(
Seq(
Edit(
`type` = Some(EditType.withName("remove")),
delete = Some("diet")
)
)
)
)
)
)
)
)
)
),
Duration(100, "sec")
)
|
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: "diet-rule",
rule: Rule(
objectID: "diet-rule",
conditions: [SearchCondition(pattern: "diet", anchoring: SearchAnchoring.contains)],
consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "'low-carb' OR 'low-fat'",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(edits: [SearchEdit(
type: SearchEditType.remove,
delete: "diet"
)]))
))
)
)
|
Using the dashboard
You can also add rules from the Algolia dashboard.
- Select the Search product icon on your dashboard.
- Select the Rules section from the left sidebar menu in the Algolia dashboard.
- Under the heading Rules, select the index to which you’re adding a Rule.
- Select Create your first rule or New rule. In the drop-down menu, select Manual Editor.
- In the Condition(s) sections, keep Query contains and enter “diet” 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:
{ "filters": "'low-carb' OR 'low-fat'" }
.
- Click the Add consequence button again and select Remove Word.
- Type or select “diet” in the input field.
- Save your changes.
Negative filters
To exclude gluten-containing foods from the search results,
when a user searches for gluten-free meals:
To do this:
- Create an
allergens
attribute (with “gluten” as one of the potential values).
- Create a rule that filters out records with “gluten” in that attribute.
Example records
1
2
3
4
5
6
7
8
9
10
11
12
| [
{
"name": "Pasta Bolognese",
"restaurant": "Millbrook Deli",
"allergens": ["eggs", "lactose"]
},
{
"name": "Breakfast Waffles",
"restaurant": "The Hive",
"allergens": ["gluten", "lactose"]
}
]
|
Using the API
Set allergens
as an attributesForFaceting
in your index:
1
2
3
4
| var response = await client.SetSettingsAsync(
"ALGOLIA_INDEX_NAME",
new IndexSettings { AttributesForFaceting = new List<string> { "allergens" } }
);
|
1
2
3
4
5
6
7
8
| final response = await client.setSettings(
indexName: "ALGOLIA_INDEX_NAME",
indexSettings: IndexSettings(
attributesForFaceting: [
"allergens",
],
),
);
|
1
2
3
4
5
6
7
8
| response, err := client.SetSettings(client.NewApiSetSettingsRequest(
"ALGOLIA_INDEX_NAME",
search.NewEmptyIndexSettings().SetAttributesForFaceting(
[]string{"allergens"})))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
| client.setSettings("ALGOLIA_INDEX_NAME", new IndexSettings().setAttributesForFaceting(Arrays.asList("allergens")));
|
1
2
3
4
| const response = await client.setSettings({
indexName: 'ALGOLIA_INDEX_NAME',
indexSettings: { attributesForFaceting: ['allergens'] },
});
|
1
2
3
4
5
6
| var response = client.setSettings(
indexName = "ALGOLIA_INDEX_NAME",
indexSettings = IndexSettings(
attributesForFaceting = listOf("allergens"),
),
)
|
1
2
3
4
5
6
7
| $response = $client->setSettings(
'ALGOLIA_INDEX_NAME',
['attributesForFaceting' => [
'allergens',
],
],
);
|
1
2
3
4
5
6
7
8
| response = client.set_settings(
index_name="ALGOLIA_INDEX_NAME",
index_settings={
"attributesForFaceting": [
"allergens",
],
},
)
|
1
2
3
4
| response = client.set_settings(
"ALGOLIA_INDEX_NAME",
Algolia::Search::IndexSettings.new(attributes_for_faceting: ["allergens"])
)
|
1
2
3
4
5
6
7
8
9
| val response = Await.result(
client.setSettings(
indexName = "ALGOLIA_INDEX_NAME",
indexSettings = IndexSettings(
attributesForFaceting = Some(Seq("allergens"))
)
),
Duration(100, "sec")
)
|
1
2
3
4
| let response = try await client.setSettings(
indexName: "ALGOLIA_INDEX_NAME",
indexSettings: IndexSettings(attributesForFaceting: ["allergens"])
)
|
- Use the
saveRule
method to create a rule that detects the term “gluten-free” in a query and applies a negative filter on facet value allergens:gluten
.
- Add a consequence in your rule to remove the word “gluten-free” from your query. This way, it won’t be used as a search term, only for filtering purposes.
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",
"gluten-free-rule",
new Rule
{
ObjectID = "gluten-free-rule",
Conditions = new List<Condition>
{
new Condition { Pattern = "gluten-free", Anchoring = Enum.Parse<Anchoring>("Contains") },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Filters = "NOT allergens:gluten",
Query = new ConsequenceQuery(
new ConsequenceQueryObject
{
Edits = new List<Edit>
{
new Edit { Type = Enum.Parse<EditType>("Remove"), Delete = "gluten-free" },
},
}
),
},
},
}
);
|
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
| final response = await client.saveRule(
indexName: "ALGOLIA_INDEX_NAME",
objectID: "gluten-free-rule",
rule: Rule(
objectID: "gluten-free-rule",
conditions: [
Condition(
pattern: "gluten-free",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
filters: "NOT allergens:gluten",
query: ConsequenceQueryObject(
edits: [
Edit(
type: EditType.fromJson("remove"),
delete: "gluten-free",
),
],
),
),
),
),
);
|
1
2
3
4
5
6
7
8
9
10
11
12
| response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"ALGOLIA_INDEX_NAME", "gluten-free-rule",
search.NewEmptyRule().SetObjectID("gluten-free-rule").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("gluten-free").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetFilters("NOT allergens:gluten").SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetEdits(
[]search.Edit{*search.NewEmptyEdit().SetType(search.EditType("remove")).SetDelete("gluten-free")})))))))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| client.saveRule(
"ALGOLIA_INDEX_NAME",
"gluten-free-rule",
new Rule()
.setObjectID("gluten-free-rule")
.setConditions(Arrays.asList(new Condition().setPattern("gluten-free").setAnchoring(Anchoring.CONTAINS)))
.setConsequence(
new Consequence()
.setParams(
new ConsequenceParams()
.setFilters("NOT allergens:gluten")
.setQuery(
new ConsequenceQueryObject().setEdits(Arrays.asList(new Edit().setType(EditType.REMOVE).setDelete("gluten-free")))
)
)
)
);
|
1
2
3
4
5
6
7
8
9
10
11
| const response = await client.saveRule({
indexName: 'indexName',
objectID: 'gluten-free-rule',
rule: {
objectID: 'gluten-free-rule',
conditions: [{ pattern: 'gluten-free', anchoring: 'contains' }],
consequence: {
params: { filters: 'NOT allergens:gluten', query: { edits: [{ type: 'remove', delete: 'gluten-free' }] } },
},
},
});
|
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 = "gluten-free-rule",
rule = Rule(
objectID = "gluten-free-rule",
conditions = listOf(
Condition(
pattern = "gluten-free",
anchoring = Anchoring.entries.first { it.value == "contains" },
),
),
consequence = Consequence(
params = ConsequenceParams(
filters = "NOT allergens:gluten",
query = ConsequenceQueryObject(
edits = listOf(
Edit(
type = EditType.entries.first { it.value == "remove" },
delete = "gluten-free",
),
),
),
),
),
),
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $response = $client->saveRule(
'ALGOLIA_INDEX_NAME',
'gluten-free-rule',
['objectID' => 'gluten-free-rule',
'conditions' => [
['pattern' => 'gluten-free',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['filters' => 'NOT allergens:gluten',
'query' => ['edits' => [
['type' => 'remove',
'delete' => 'gluten-free',
],
],
],
],
],
],
);
|
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
| response = client.save_rule(
index_name="ALGOLIA_INDEX_NAME",
object_id="gluten-free-rule",
rule={
"objectID": "gluten-free-rule",
"conditions": [
{
"pattern": "gluten-free",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"filters": "NOT allergens:gluten",
"query": {
"edits": [
{
"type": "remove",
"delete": "gluten-free",
},
],
},
},
},
},
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| response = client.save_rule(
"ALGOLIA_INDEX_NAME",
"gluten-free-rule",
Algolia::Search::Rule.new(
algolia_object_id: "gluten-free-rule",
conditions: [Algolia::Search::Condition.new(pattern: "gluten-free", anchoring: "contains")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
filters: "NOT allergens:gluten",
query: Algolia::Search::ConsequenceQueryObject.new(
edits: [Algolia::Search::Edit.new(type: "remove", delete: "gluten-free")]
)
)
)
)
)
|
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
35
36
37
| val response = Await.result(
client.saveRule(
indexName = "ALGOLIA_INDEX_NAME",
objectID = "gluten-free-rule",
rule = Rule(
objectID = "gluten-free-rule",
conditions = Some(
Seq(
Condition(
pattern = Some("gluten-free"),
anchoring = Some(Anchoring.withName("contains"))
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
filters = Some("NOT allergens:gluten"),
query = Some(
ConsequenceQueryObject(
edits = Some(
Seq(
Edit(
`type` = Some(EditType.withName("remove")),
delete = Some("gluten-free")
)
)
)
)
)
)
)
)
)
),
Duration(100, "sec")
)
|
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: "gluten-free-rule",
rule: Rule(
objectID: "gluten-free-rule",
conditions: [SearchCondition(pattern: "gluten-free", anchoring: SearchAnchoring.contains)],
consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "NOT allergens:gluten",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(edits: [SearchEdit(
type: SearchEditType.remove,
delete: "gluten-free"
)]))
))
)
)
|
Using the dashboard
You can also add rules from the Algolia dashboard.
- Select the Search product icon on your dashboard and then select your index.
- Click the Configuration tab.
- In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the
allergens
attribute from the drop-down menu.
- Click the Rules tab.
- Select Create your first rule or New rule. In the drop-down menu, select Manual Editor.
- In the Condition(s) section, keep Query toggled on, select Contains in the drop-down menu, and enter “gluten-free” 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:
{ "filters": "NOT allergens:gluten" }
- Click the Add consequence button again and select Remove Word.
- Type or select “gluten-free” in the input field.
- Save your changes.
Numerical filters
Consider the query “cheap toaster 800w”. You can use Rules to filter the results by “toaster” and “prices between 0 and 25” so that the only textual search is the remaining term, “800w”, which could further be used to limit the results with that wattage.
Rule
If query = “cheap toaster” then price < 10 and type=toaster
This requires two rules.
Using the API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| var response = await client.SaveRuleAsync(
"ALGOLIA_INDEX_NAME",
"cheap",
new Rule
{
ObjectID = "cheap",
Conditions = new List<Condition>
{
new Condition { Pattern = "cheap", Anchoring = Enum.Parse<Anchoring>("Contains") },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Query = new ConsequenceQuery(
new ConsequenceQueryObject { Remove = new List<string> { "cheap" } }
),
Filters = "price < 10",
},
},
}
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| final response = await client.saveRule(
indexName: "ALGOLIA_INDEX_NAME",
objectID: "cheap",
rule: Rule(
objectID: "cheap",
conditions: [
Condition(
pattern: "cheap",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
query: ConsequenceQueryObject(
remove: [
"cheap",
],
),
filters: "price < 10",
),
),
),
);
|
1
2
3
4
5
6
7
8
9
10
11
12
| response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"ALGOLIA_INDEX_NAME", "cheap",
search.NewEmptyRule().SetObjectID("cheap").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("cheap").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetRemove(
[]string{"cheap"}))).SetFilters("price < 10")))))
if err != nil {
// handle the eventual error
panic(err)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| client.saveRule(
"ALGOLIA_INDEX_NAME",
"cheap",
new Rule()
.setObjectID("cheap")
.setConditions(Arrays.asList(new Condition().setPattern("cheap").setAnchoring(Anchoring.CONTAINS)))
.setConsequence(
new Consequence()
.setParams(
new ConsequenceParams().setQuery(new ConsequenceQueryObject().setRemove(Arrays.asList("cheap"))).setFilters("price < 10")
)
)
);
|
1
2
3
4
5
6
7
8
9
| const response = await client.saveRule({
indexName: 'indexName',
objectID: 'cheap',
rule: {
objectID: 'cheap',
conditions: [{ pattern: 'cheap', anchoring: 'contains' }],
consequence: { params: { query: { remove: ['cheap'] }, filters: 'price < 10' } },
},
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| var response = client.saveRule(
indexName = "ALGOLIA_INDEX_NAME",
objectID = "cheap",
rule = Rule(
objectID = "cheap",
conditions = listOf(
Condition(
pattern = "cheap",
anchoring = Anchoring.entries.first { it.value == "contains" },
),
),
consequence = Consequence(
params = ConsequenceParams(
query = ConsequenceQueryObject(
remove = listOf("cheap"),
),
filters = "price < 10",
),
),
),
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| $response = $client->saveRule(
'ALGOLIA_INDEX_NAME',
'cheap',
['objectID' => 'cheap',
'conditions' => [
['pattern' => 'cheap',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['query' => ['remove' => [
'cheap',
],
],
'filters' => 'price < 10',
],
],
],
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| response = client.save_rule(
index_name="ALGOLIA_INDEX_NAME",
object_id="cheap",
rule={
"objectID": "cheap",
"conditions": [
{
"pattern": "cheap",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"query": {
"remove": [
"cheap",
],
},
"filters": "price < 10",
},
},
},
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| response = client.save_rule(
"ALGOLIA_INDEX_NAME",
"cheap",
Algolia::Search::Rule.new(
algolia_object_id: "cheap",
conditions: [Algolia::Search::Condition.new(pattern: "cheap", anchoring: "contains")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
query: Algolia::Search::ConsequenceQueryObject.new(remove: ["cheap"]),
filters: "price < 10"
)
)
)
)
|
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
| val response = Await.result(
client.saveRule(
indexName = "ALGOLIA_INDEX_NAME",
objectID = "cheap",
rule = Rule(
objectID = "cheap",
conditions = Some(
Seq(
Condition(
pattern = Some("cheap"),
anchoring = Some(Anchoring.withName("contains"))
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
query = Some(
ConsequenceQueryObject(
remove = Some(Seq("cheap"))
)
),
filters = Some("price < 10")
)
)
)
)
),
Duration(100, "sec")
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| let response = try await client.saveRule(
indexName: "ALGOLIA_INDEX_NAME",
objectID: "cheap",
rule: Rule(
objectID: "cheap",
conditions: [SearchCondition(pattern: "cheap", anchoring: SearchAnchoring.contains)],
consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "price < 10",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(remove: ["cheap"]))
))
)
)
|
Using the dashboard
Since there are two rules, you must set up both separately.
Preparation
- Select the Search product icon on your dashboard and then select your index.
- Click the Configuration tab.
- In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the
product_type
attribute from the drop-down menu.
For the first rule
- Select the Rules section from the left sidebar menu in the Algolia dashboard.
- Under the heading Rules, select the index to which you’re adding a rule.
- Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
- In the Condition(s) section, keep Query toggled on, select Contains in the drop-down menu, and enter “toaster” 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, add the JSON parameters you want to apply when the user’s query matches the Rule:
{ "filters": "product_type:toaster" }
- Click the Add consequence button again and select Remove Word.
- Type or select “toaster” in the input field.
- Save your changes.
For the second rule
- Go back to the Rules section.
- Select New rule. In the drop-down menu, click the Manual Editor option.
- In the Condition(s) section, keep Query toggled on, select Contains in the drop-down menu, and enter “cheap” 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, add the JSON parameters you want to apply when the user’s query matches the Rule:
{ "filters": "price<10" }
- Click the Add consequence button again and select Remove Word.
- Type or select “cheap” in the input field.
- Save your changes.