Before you begin
Add the records from the example dataset to an Algolia index. Configurecolor, product_type, and brand as
attributes for faceting.
How to create a dynamic filtering rule
In this example for a product catalog, you create a rule that displays products with the “red” value in thecolor facet if the query contains the word “red”.
With the example dataset,
this rule limits results to records with “red” in the color attribute.
It ignores “red” in other attributes such as brand.
| Query | Results |
|---|---|
| red | FM, Clothing, LondonLook, Red, 21.99 The Mandal 800w, Toaster, Black & Decker, Red, 149.99 |
Create the rule in the dashboard
You can add this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- In the Algolia dashboard, select your application and index, open Rules, and select New rule > Visual Editor.
- Click Set query condition(s), select contains, enter
red, and click Apply. - In Consequences, select Filter results.
Select the
colorattribute and theredvalue, then click Apply. To boost red products while keeping other colors in the results, use Boost categories instead. - Click Add Consequence, expand More consequences, and select Remove word.
Enter
redin Word(s) and click Apply. - Review the results, click Review and Publish, and click Publish.
- Go to the Algolia dashboard and select your Algolia . On the left sidebar, select Search, select your index, and open Rules. Select Create your first rule or New rule, then select Manual Editor.
-
In Condition(s), keep Query selected, select Contains, and enter
red. -
In Consequence(s), click Add consequence and select Add Query Parameter.
Enter
{"filters":"color:red"}. To promote red products without excluding products of other colors, see Optional filters. -
Click Add consequence and select Remove Word.
Enter
red. - Save your changes.
Create the rule with the API
To run the code examples on this page, install the latest API client. This example uses thefilters parameter.
With this approach, you need one rule per filter value.
If you have 10 color options, you need 10 rules, one for each color.
var response = await client.SaveRuleAsync(
"INDEX_NAME",
"red-color",
new Rule
{
ObjectID = "red-color",
Conditions = new List<Condition>
{
new Condition { Pattern = "red", Anchoring = Enum.Parse<Anchoring>("Contains") },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Query = new ConsequenceQuery(
new ConsequenceQueryObject { Remove = new List<string> { "red" } }
),
Filters = "color:red",
},
},
}
);
final response = await client.saveRule(
indexName: "INDEX_NAME",
objectID: "red-color",
rule: Rule(
objectID: "red-color",
conditions: [
Condition(
pattern: "red",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
query: ConsequenceQueryObject(
remove: [
"red",
],
),
filters: "color:red",
),
),
),
);
response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"INDEX_NAME", "red-color",
search.NewEmptyRule().SetObjectID("red-color").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("red").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetRemove(
[]string{"red"}))).SetFilters("color:red")))))
if err != nil {
// handle the eventual error
panic(err)
}
UpdatedAtResponse response = client.saveRule(
"INDEX_NAME",
"red-color",
new Rule()
.setObjectID("red-color")
.setConditions(Arrays.asList(new Condition().setPattern("red").setAnchoring(Anchoring.CONTAINS)))
.setConsequence(
new Consequence().setParams(
new ConsequenceParams().setQuery(new ConsequenceQueryObject().setRemove(Arrays.asList("red"))).setFilters("color:red")
)
)
);
const response = await client.saveRule({
indexName: 'indexName',
objectID: 'red-color',
rule: {
objectID: 'red-color',
conditions: [{ pattern: 'red', anchoring: 'contains' }],
consequence: { params: { query: { remove: ['red'] }, filters: 'color:red' } },
},
});
var response =
client.saveRule(
indexName = "INDEX_NAME",
objectID = "red-color",
rule =
Rule(
objectID = "red-color",
conditions =
listOf(
Condition(
pattern = "red",
anchoring = Anchoring.entries.first { it.value == "contains" },
)
),
consequence =
Consequence(
params =
ConsequenceParams(
query = ConsequenceQueryObject(remove = listOf("red")),
filters = "color:red",
)
),
),
)
$response = $client->saveRule(
'INDEX_NAME',
'red-color',
['objectID' => 'red-color',
'conditions' => [
['pattern' => 'red',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['query' => ['remove' => [
'red',
],
],
'filters' => 'color:red',
],
],
],
);
response = client.save_rule(
index_name="INDEX_NAME",
object_id="red-color",
rule={
"objectID": "red-color",
"conditions": [
{
"pattern": "red",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"query": {
"remove": [
"red",
],
},
"filters": "color:red",
},
},
},
)
response = client.save_rule(
"INDEX_NAME",
"red-color",
Algolia::Search::Rule.new(
algolia_object_id: "red-color",
conditions: [Algolia::Search::Condition.new(pattern: "red", anchoring: "contains")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
query: Algolia::Search::ConsequenceQueryObject.new(remove: ["red"]),
filters: "color:red"
)
)
)
)
val response = Await.result(
client.saveRule(
indexName = "INDEX_NAME",
objectID = "red-color",
rule = Rule(
objectID = "red-color",
conditions = Some(
Seq(
Condition(
pattern = Some("red"),
anchoring = Some(Anchoring.withName("contains"))
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
query = Some(
ConsequenceQueryObject(
remove = Some(Seq("red"))
)
),
filters = Some("color:red")
)
)
)
)
),
Duration(100, "sec")
)
let response = try await client.saveRule(
indexName: "INDEX_NAME",
objectID: "red-color",
rule: Rule(
objectID: "red-color",
conditions: [SearchCondition(pattern: "red", anchoring: SearchAnchoring.contains)],
consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "color:red",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(remove: ["red"]))
))
)
)
Example: one rule per facet
Use one rule to detect values of thecolor facet and apply the corresponding filter.
For example, the query red applies color:red, while black applies color:black.
This avoids creating a separate rule for each color.
| Query | Results |
|---|---|
| red | FM, Clothing, LondonLook, Red, 21.99 |
| red | The Mandal 800w, Toaster, Black & Decker, Red, 149.99 |
| black | Will Carpenter, T-shirt, Red or Dead, Black, 199.99 |
Create the rule in the dashboard
You can add this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- Create a rule with Visual Editor.
- Click Set query condition(s) and select contains.
Select Match any value of a facet and choose
color. Click Apply. - In Consequences, expand More consequences and select Filter / Boost matching attribute.
Select
colorin Facet. The facet must be present in every query condition. - Choose Filter to keep only matching records or Boost to promote them without excluding other records. Set Score from 1 to 50 to control the filter score. If your plan doesn’t support optional filters in rules, select Filter facets to filter the results.
- Under Filter type, select Conjunctive to require all matching values or Disjunctive to accept any of them. Click Apply.
- Review the results for different color queries, click Review and Publish, and click Publish.
- Create a rule with the Manual Editor. See how to create a dynamic filtering rule.
- In Condition(s), click the box next to Query contains and select Add Facet “color”.
- In Consequence(s), click Add consequence and select Filter/Boost Matching Attributes. Click Filter and select Add Facet “color”.
- Save your changes.
Create the rule with the API
var response = await client.SaveRuleAsync(
"INDEX_NAME",
"color-facets",
new Rule
{
ObjectID = "color-facets",
Conditions = new List<Condition> { new Condition { Pattern = "{facet:color}" } },
Consequence = new Consequence
{
Params = new ConsequenceParams
{
AutomaticFacetFilters = new AutomaticFacetFilters(
new List<AutomaticFacetFilter> { new AutomaticFacetFilter { Facet = "color" } }
),
},
},
}
);
final response = await client.saveRule(
indexName: "INDEX_NAME",
objectID: "color-facets",
rule: Rule(
objectID: "color-facets",
conditions: [
Condition(
pattern: "{facet:color}",
),
],
consequence: Consequence(
params: ConsequenceParams(
automaticFacetFilters: [
AutomaticFacetFilter(
facet: "color",
),
],
),
),
),
);
response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"INDEX_NAME", "color-facets",
search.NewEmptyRule().SetObjectID("color-facets").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("{facet:color}")}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetAutomaticFacetFilters(search.ArrayOfAutomaticFacetFilterAsAutomaticFacetFilters(
[]search.AutomaticFacetFilter{*search.NewEmptyAutomaticFacetFilter().SetFacet("color")}))))))
if err != nil {
// handle the eventual error
panic(err)
}
UpdatedAtResponse response = client.saveRule(
"INDEX_NAME",
"color-facets",
new Rule()
.setObjectID("color-facets")
.setConditions(Arrays.asList(new Condition().setPattern("{facet:color}")))
.setConsequence(
new Consequence().setParams(
new ConsequenceParams().setAutomaticFacetFilters(
AutomaticFacetFilters.ofListOfAutomaticFacetFilter(Arrays.asList(new AutomaticFacetFilter().setFacet("color")))
)
)
)
);
const response = await client.saveRule({
indexName: 'indexName',
objectID: 'color-facets',
rule: {
objectID: 'color-facets',
conditions: [{ pattern: '{facet:color}' }],
consequence: { params: { automaticFacetFilters: [{ facet: 'color' }] } },
},
});
var response =
client.saveRule(
indexName = "INDEX_NAME",
objectID = "color-facets",
rule =
Rule(
objectID = "color-facets",
conditions = listOf(Condition(pattern = "{facet:color}")),
consequence =
Consequence(
params =
ConsequenceParams(
automaticFacetFilters =
AutomaticFacetFilters.ofListOfAutomaticFacetFilter(
listOf(AutomaticFacetFilter(facet = "color"))
)
)
),
),
)
$response = $client->saveRule(
'INDEX_NAME',
'color-facets',
['objectID' => 'color-facets',
'conditions' => [
['pattern' => '{facet:color}',
],
],
'consequence' => ['params' => ['automaticFacetFilters' => [
['facet' => 'color',
],
],
],
],
],
);
response = client.save_rule(
index_name="INDEX_NAME",
object_id="color-facets",
rule={
"objectID": "color-facets",
"conditions": [
{
"pattern": "{facet:color}",
},
],
"consequence": {
"params": {
"automaticFacetFilters": [
{
"facet": "color",
},
],
},
},
},
)
response = client.save_rule(
"INDEX_NAME",
"color-facets",
Algolia::Search::Rule.new(
algolia_object_id: "color-facets",
conditions: [Algolia::Search::Condition.new(pattern: "{facet:color}")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
automatic_facet_filters: [Algolia::Search::AutomaticFacetFilter.new(facet: "color")]
)
)
)
)
val response = Await.result(
client.saveRule(
indexName = "INDEX_NAME",
objectID = "color-facets",
rule = Rule(
objectID = "color-facets",
conditions = Some(
Seq(
Condition(
pattern = Some("{facet:color}")
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
automaticFacetFilters = Some(
AutomaticFacetFilters(
Seq(
AutomaticFacetFilter(
facet = "color"
)
)
)
)
)
)
)
)
),
Duration(100, "sec")
)
let response = try await client.saveRule(
indexName: "INDEX_NAME",
objectID: "color-facets",
rule: Rule(
objectID: "color-facets",
conditions: [SearchCondition(pattern: "{facet:color}")],
consequence: SearchConsequence(
params: SearchConsequenceParams(automaticFacetFilters: SearchAutomaticFacetFilters
.arrayOfSearchAutomaticFacetFilter([SearchAutomaticFacetFilter(facet: "color")]))
)
)
)
Numerical dynamic filtering
For the querycheap toaster 800w, use one rule to filter by product_type:toaster
and another to filter by price < 15.
Remove toaster and cheap from the query so the search uses 800w to find matching products.
With the example dataset,
the query returns the Essentials 800w toaster, priced at $14.99.
| Query | Results |
|---|---|
| cheap toaster 800w | Essentials 800w, Toaster, Daewoo, Black, 14.99 |
Create the rules in the dashboard
You can add these rules from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- Open Rules and select New rule > Visual Editor.
- Click Set query condition(s), select contains, enter
toaster, and click Apply. - In Consequences, select Filter results.
Select
product_typeas the attribute andtoasteras the value, then click Apply. - Click Add Consequence, expand More consequences, and select Remove word.
Enter
toasterin Word(s) and click Apply. - Review the results, click Review and Publish, and click Publish.
cheap for the condition and removed word, and filter on price < 15.- Open Rules, select New rule, and select Manual Editor.
- In Condition(s), keep Query selected, select Contains, and enter
toaster. - In Consequence(s), click Add consequence and select Add Query Parameter.
Enter
{"filters":"product_type:toaster"}. - Click Add consequence and select Remove Word.
Enter
toaster. - Save your changes.
cheap for the condition and removed word, and enter {"filters":"price < 15"} as the query parameter.Create the rules with the API
var response = await client.SaveRulesAsync(
"INDEX_NAME",
new List<Rule>
{
new Rule
{
ObjectID = "toaster",
Conditions = new List<Condition>
{
new Condition { Pattern = "toaster", Anchoring = Enum.Parse<Anchoring>("Contains") },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Query = new ConsequenceQuery(
new ConsequenceQueryObject { Remove = new List<string> { "toaster" } }
),
Filters = "product_type:toaster",
},
},
},
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 < 15",
},
},
},
}
);
final response = await client.saveRules(
indexName: "INDEX_NAME",
rules: [
Rule(
objectID: "toaster",
conditions: [
Condition(
pattern: "toaster",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
query: ConsequenceQueryObject(
remove: [
"toaster",
],
),
filters: "product_type:toaster",
),
),
),
Rule(
objectID: "cheap",
conditions: [
Condition(
pattern: "cheap",
anchoring: Anchoring.fromJson("contains"),
),
],
consequence: Consequence(
params: ConsequenceParams(
query: ConsequenceQueryObject(
remove: [
"cheap",
],
),
filters: "price < 15",
),
),
),
],
);
response, err := client.SaveRules(client.NewApiSaveRulesRequest(
"INDEX_NAME",
[]search.Rule{*search.NewEmptyRule().SetObjectID("toaster").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("toaster").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetRemove(
[]string{"toaster"}))).SetFilters("product_type:toaster"))), *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 < 15")))}))
if err != nil {
// handle the eventual error
panic(err)
}
UpdatedAtResponse response = client.saveRules(
"INDEX_NAME",
Arrays.asList(
new Rule()
.setObjectID("toaster")
.setConditions(Arrays.asList(new Condition().setPattern("toaster").setAnchoring(Anchoring.CONTAINS)))
.setConsequence(
new Consequence().setParams(
new ConsequenceParams()
.setQuery(new ConsequenceQueryObject().setRemove(Arrays.asList("toaster")))
.setFilters("product_type:toaster")
)
),
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 < 15")
)
)
)
);
const response = await client.saveRules({
indexName: 'INDEX_NAME',
rules: [
{
objectID: 'toaster',
conditions: [{ pattern: 'toaster', anchoring: 'contains' }],
consequence: { params: { query: { remove: ['toaster'] }, filters: 'product_type:toaster' } },
},
{
objectID: 'cheap',
conditions: [{ pattern: 'cheap', anchoring: 'contains' }],
consequence: { params: { query: { remove: ['cheap'] }, filters: 'price < 15' } },
},
],
});
var response =
client.saveRules(
indexName = "INDEX_NAME",
rules =
listOf(
Rule(
objectID = "toaster",
conditions =
listOf(
Condition(
pattern = "toaster",
anchoring = Anchoring.entries.first { it.value == "contains" },
)
),
consequence =
Consequence(
params =
ConsequenceParams(
query = ConsequenceQueryObject(remove = listOf("toaster")),
filters = "product_type:toaster",
)
),
),
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 < 15",
)
),
),
),
)
$response = $client->saveRules(
'INDEX_NAME',
[
['objectID' => 'toaster',
'conditions' => [
['pattern' => 'toaster',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['query' => ['remove' => [
'toaster',
],
],
'filters' => 'product_type:toaster',
],
],
],
['objectID' => 'cheap',
'conditions' => [
['pattern' => 'cheap',
'anchoring' => 'contains',
],
],
'consequence' => ['params' => ['query' => ['remove' => [
'cheap',
],
],
'filters' => 'price < 15',
],
],
],
],
);
response = client.save_rules(
index_name="INDEX_NAME",
rules=[
{
"objectID": "toaster",
"conditions": [
{
"pattern": "toaster",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"query": {
"remove": [
"toaster",
],
},
"filters": "product_type:toaster",
},
},
},
{
"objectID": "cheap",
"conditions": [
{
"pattern": "cheap",
"anchoring": "contains",
},
],
"consequence": {
"params": {
"query": {
"remove": [
"cheap",
],
},
"filters": "price < 15",
},
},
},
],
)
response = client.save_rules(
"INDEX_NAME",
[
Algolia::Search::Rule.new(
algolia_object_id: "toaster",
conditions: [Algolia::Search::Condition.new(pattern: "toaster", anchoring: "contains")],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
query: Algolia::Search::ConsequenceQueryObject.new(remove: ["toaster"]),
filters: "product_type:toaster"
)
)
),
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 < 15"
)
)
)
]
)
val response = Await.result(
client.saveRules(
indexName = "INDEX_NAME",
rules = Seq(
Rule(
objectID = "toaster",
conditions = Some(
Seq(
Condition(
pattern = Some("toaster"),
anchoring = Some(Anchoring.withName("contains"))
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
query = Some(
ConsequenceQueryObject(
remove = Some(Seq("toaster"))
)
),
filters = Some("product_type:toaster")
)
)
)
),
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 < 15")
)
)
)
)
)
),
Duration(100, "sec")
)
let response = try await client.saveRules(
indexName: "INDEX_NAME",
rules: [
Rule(objectID: "toaster", conditions: [SearchCondition(
pattern: "toaster",
anchoring: SearchAnchoring.contains
)], consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "product_type:toaster",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(remove: ["toaster"]))
))),
Rule(
objectID: "cheap",
conditions: [SearchCondition(pattern: "cheap", anchoring: SearchAnchoring.contains)],
consequence: SearchConsequence(params: SearchConsequenceParams(
filters: "price < 15",
query: SearchConsequenceQuery
.searchConsequenceQueryObject(SearchConsequenceQueryObject(remove: ["cheap"]))
))
),
]
)
Filter by brand
Users can enterapple headphones to search for Apple headphones.
The rule applies a filter for the Apple brand and removes the matched brand value from the query,
leaving headphones as the search term.
This extends one rule per facet
by also removing the matched brand value from the query.
With the example dataset, the query returns Airpods Max.
It excludes the Sony headphones, even though their color attribute contains Apple.
| Query | Results |
|---|---|
| apple headphones | Airpods Max, Headphones, Apple, Gray, 548.99 |
Create the rule in the dashboard
You can add this rule from the Algolia dashboard using either the Visual Editor or the Manual Editor.- Visual Editor
- Manual Editor
- Open Rules and select New rule > Visual Editor.
- Click Set query condition(s) and select contains.
Select Match any value of a facet and choose
brand. Clear the checkbox for matching plurals, synonyms, and typos, then click Apply. - In Consequences, expand More consequences and select Filter / Boost matching attribute.
Select
brandin Facet, choose Filter, and click Apply. - Click Add Consequence, expand More consequences, and select Remove word.
Select
{facet:brand}in Word(s) and click Apply. - Review the results, click Review and Publish, and click Publish.
- Create a rule with the Manual Editor.
- In Condition(s), click the box next to Query contains and select Add Facet “brand”. Clear Apply to plurals, synonyms and typos to ensure precise brand matching.
- In Consequence(s), click Add consequence and select Filter/Boost Matching Attributes. Click Filter and select Add Facet “brand”.
- Click Add consequence and select Remove word.
Select Remove
{facet:brand}. - Save your changes.
Create the rule with the API
var response = await client.SaveRuleAsync(
"INDEX_NAME",
"tagged-brand-rule",
new Rule
{
Conditions = new List<Condition>
{
new Condition
{
Pattern = "brand: {facet:brand}",
Anchoring = Enum.Parse<Anchoring>("Contains"),
Alternatives = false,
},
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
AutomaticFacetFilters = new AutomaticFacetFilters(
new List<AutomaticFacetFilter> { new AutomaticFacetFilter { Facet = "brand" } }
),
Query = new ConsequenceQuery(
new ConsequenceQueryObject
{
Remove = new List<string> { "brand:", "{facet:brand}" },
}
),
},
},
Description = "filter on brand: {brand}",
ObjectID = "tagged-brand-rule",
}
);
final response = await client.saveRule(
indexName: "INDEX_NAME",
objectID: "tagged-brand-rule",
rule: Rule(
conditions: [
Condition(
pattern: "brand: {facet:brand}",
anchoring: Anchoring.fromJson("contains"),
alternatives: false,
),
],
consequence: Consequence(
params: ConsequenceParams(
automaticFacetFilters: [
AutomaticFacetFilter(
facet: "brand",
),
],
query: ConsequenceQueryObject(
remove: [
"brand:",
"{facet:brand}",
],
),
),
),
description: "filter on brand: {brand}",
objectID: "tagged-brand-rule",
),
);
response, err := client.SaveRule(client.NewApiSaveRuleRequest(
"INDEX_NAME", "tagged-brand-rule",
search.NewEmptyRule().SetConditions(
[]search.Condition{
*search.NewEmptyCondition().SetPattern("brand: {facet:brand}").SetAnchoring(search.Anchoring("contains")).SetAlternatives(false),
}).
SetConsequence(
search.NewEmptyConsequence().SetParams(
search.NewEmptyConsequenceParams().SetAutomaticFacetFilters(search.ArrayOfAutomaticFacetFilterAsAutomaticFacetFilters(
[]search.AutomaticFacetFilter{
*search.NewEmptyAutomaticFacetFilter().SetFacet("brand"),
},
)).SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
search.NewEmptyConsequenceQueryObject().SetRemove(
[]string{"brand:", "{facet:brand}"}))))).SetDescription("filter on brand: {brand}").SetObjectID("tagged-brand-rule")))
if err != nil {
// handle the eventual error
panic(err)
}
UpdatedAtResponse response = client.saveRule(
"INDEX_NAME",
"tagged-brand-rule",
new Rule()
.setConditions(
Arrays.asList(new Condition().setPattern("brand: {facet:brand}").setAnchoring(Anchoring.CONTAINS).setAlternatives(false))
)
.setConsequence(
new Consequence().setParams(
new ConsequenceParams()
.setAutomaticFacetFilters(
AutomaticFacetFilters.ofListOfAutomaticFacetFilter(Arrays.asList(new AutomaticFacetFilter().setFacet("brand")))
)
.setQuery(new ConsequenceQueryObject().setRemove(Arrays.asList("brand:", "{facet:brand}")))
)
)
.setDescription("filter on brand: {brand}")
.setObjectID("tagged-brand-rule")
);
const response = await client.saveRule({
indexName: 'indexName',
objectID: 'tagged-brand-rule',
rule: {
conditions: [{ pattern: 'brand: {facet:brand}', anchoring: 'contains', alternatives: false }],
consequence: {
params: { automaticFacetFilters: [{ facet: 'brand' }], query: { remove: ['brand:', '{facet:brand}'] } },
},
description: 'filter on brand: {brand}',
objectID: 'tagged-brand-rule',
},
});
var response =
client.saveRule(
indexName = "INDEX_NAME",
objectID = "tagged-brand-rule",
rule =
Rule(
conditions =
listOf(
Condition(
pattern = "brand: {facet:brand}",
anchoring = Anchoring.entries.first { it.value == "contains" },
alternatives = false,
)
),
consequence =
Consequence(
params =
ConsequenceParams(
automaticFacetFilters =
AutomaticFacetFilters.ofListOfAutomaticFacetFilter(
listOf(AutomaticFacetFilter(facet = "brand"))
),
query = ConsequenceQueryObject(remove = listOf("brand:", "{facet:brand}")),
)
),
description = "filter on brand: {brand}",
objectID = "tagged-brand-rule",
),
)
$response = $client->saveRule(
'INDEX_NAME',
'tagged-brand-rule',
['conditions' => [
['pattern' => 'brand: {facet:brand}',
'anchoring' => 'contains',
'alternatives' => false,
],
],
'consequence' => ['params' => ['automaticFacetFilters' => [
['facet' => 'brand',
],
],
'query' => ['remove' => [
'brand:',
'{facet:brand}',
],
],
],
],
'description' => 'filter on brand: {brand}',
'objectID' => 'tagged-brand-rule',
],
);
response = client.save_rule(
index_name="INDEX_NAME",
object_id="tagged-brand-rule",
rule={
"conditions": [
{
"pattern": "brand: {facet:brand}",
"anchoring": "contains",
"alternatives": False,
},
],
"consequence": {
"params": {
"automaticFacetFilters": [
{
"facet": "brand",
},
],
"query": {
"remove": [
"brand:",
"{facet:brand}",
],
},
},
},
"description": "filter on brand: {brand}",
"objectID": "tagged-brand-rule",
},
)
response = client.save_rule(
"INDEX_NAME",
"tagged-brand-rule",
Algolia::Search::Rule.new(
conditions: [
Algolia::Search::Condition.new(pattern: "brand: {facet:brand}", anchoring: "contains", alternatives: false)
],
consequence: Algolia::Search::Consequence.new(
params: Algolia::Search::ConsequenceParams.new(
automatic_facet_filters: [Algolia::Search::AutomaticFacetFilter.new(facet: "brand")],
query: Algolia::Search::ConsequenceQueryObject.new(remove: ["brand:", "{facet:brand}"])
)
),
description: "filter on brand: {brand}",
algolia_object_id: "tagged-brand-rule"
)
)
val response = Await.result(
client.saveRule(
indexName = "INDEX_NAME",
objectID = "tagged-brand-rule",
rule = Rule(
conditions = Some(
Seq(
Condition(
pattern = Some("brand: {facet:brand}"),
anchoring = Some(Anchoring.withName("contains")),
alternatives = Some(false)
)
)
),
consequence = Consequence(
params = Some(
ConsequenceParams(
automaticFacetFilters = Some(
AutomaticFacetFilters(
Seq(
AutomaticFacetFilter(
facet = "brand"
)
)
)
),
query = Some(
ConsequenceQueryObject(
remove = Some(Seq("brand:", "{facet:brand}"))
)
)
)
)
),
description = Some("filter on brand: {brand}"),
objectID = "tagged-brand-rule"
)
),
Duration(100, "sec")
)
let response = try await client.saveRule(
indexName: "INDEX_NAME",
objectID: "tagged-brand-rule",
rule: Rule(
objectID: "tagged-brand-rule",
conditions: [SearchCondition(
pattern: "brand: {facet:brand}",
anchoring: SearchAnchoring.contains,
alternatives: false
)],
consequence: SearchConsequence(params: SearchConsequenceParams(
query: SearchConsequenceQuery.searchConsequenceQueryObject(SearchConsequenceQueryObject(remove: [
"brand:",
"{facet:brand}",
])),
automaticFacetFilters: SearchAutomaticFacetFilters
.arrayOfSearchAutomaticFacetFilter([SearchAutomaticFacetFilter(facet: "brand")])
)),
description: "filter on brand: {brand}"
)
)
Example dataset
The examples on this page use the following product catalog records:JSON
[
{
"product_name": "FM",
"product_type": "T-shirt",
"brand": "LondonLook",
"color": "Red",
"price": 21.99
},
{
"product_name": "Caveman",
"product_type": "T-shirt",
"brand": "Red or Dead",
"color": "Brown",
"price": 9.99
},
{
"product_name": "iPhone 13 Pro Max",
"product_type": "Phone",
"brand": "Apple",
"color": "Blue",
"price": 1824.61
},
{
"product_name": "Will Carpenter",
"product_type": "T-shirt",
"brand": "Red or Dead",
"color": "Black",
"price": 199.99
},
{
"product_name": "The Mandal 800w",
"product_type": "Toaster",
"brand": "Black & Decker",
"color": "Red",
"price": 149.99
},
{
"product_name": "Essentials 800w",
"product_type": "Toaster",
"brand": "Daewoo",
"color": "Black",
"price": 14.99
},
{
"product_name": "Airpods Max",
"product_type": "Headphones",
"brand": "Apple",
"color": "Gray",
"price": 548.99
},
{
"product_name": "WH-CH520",
"product_type": "Headphones",
"brand": "Sony",
"color": "Apple",
"price": 39.99
}
]