To help users find what they’re looking for straight from the search box, you can use Algolia Rules to automatically apply a filter to results if the query matches a facet value.
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 the color facet if the query contains the word “red”.
Using the example dataset, this rule ensures that only records with “red” in the color attribute are returned. It ignores “red” in other attributes such as brand.
Query
Results
red
FM, Clothing, LondonLook, Red, 21.99 The Mandal, Toaster, Black & Decker, Red, 149.99
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
In the Condition(s) section, with Query toggled on, select Contains in the drop-down menu, and enter “red” in the input field.
In the Consequence(s) section:
Click Add consequence 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":"color:red"}
Click Add consequence and select Remove Word.
Type or select “red” in the input field.
If you click Optional under Consequence(s), this will display non-red products after the red ones. For more information, see Using optional filters.
Save your changes.
Using the API
This example uses the filters 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.
response,err:=client.SaveRule(client.NewApiSaveRuleRequest("ALGOLIA_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")))))iferr!=nil{// handle the eventual errorpanic(err)}
response,err:=client.SaveRule(client.NewApiSaveRuleRequest("ALGOLIA_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")}))))))iferr!=nil{// handle the eventual errorpanic(err)}
Consider a user entering the query “cheap toaster 800w”. You can use two rules together to filter results by:
The product type: “Toaster”
“Cheapness”. You determine that a cheap product is any product that costs less than $15.
Using the example dataset, these rules ensure that any product that costs less than $15, has the product_type “Toaster” and includes the phrase “800w” is returned.
Consider a user who enters the query “apple headphones”.
They would expect to search for results that match the term “headphones”, but only where the brand attribute matches “apple”.
You can find this behavior on sites such as GitHub.
It’s a great alternative to filters for users who prefer to type rather than click.
Using the example dataset, this rule ensures that any record containing the phrase “headphones” that also has “Apple” in the brand attribute will be returned. It won’t return non-Apple brands, even if the phrase “Apple” appears in other attributes.
varresponse=awaitclient.SaveRuleAsync("ALGOLIA_INDEX_NAME","tagged-brand-rule",newRule{Conditions=newList<Condition>{newCondition{Pattern="brand: {facet:brand}",Anchoring=Enum.Parse<Anchoring>("Contains"),Alternatives=false,},},Consequence=newConsequence{Params=newConsequenceParams{AutomaticFacetFilters=newAutomaticFacetFilters(newList<AutomaticFacetFilter>{newAutomaticFacetFilter{Facet="brand"}}),Query=newConsequenceQuery(newConsequenceQueryObject{Remove=newList<string>{"brand:","{facet:brand}"},}),},},Description="filter on brand: {brand}",ObjectID="tagged-brand-rule",});
finalresponse=awaitclient.saveRule(indexName:"ALGOLIA_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",),);
1
2
3
4
5
6
7
8
9
10
11
12
13
response,err:=client.SaveRule(client.NewApiSaveRuleRequest("ALGOLIA_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")))iferr!=nil{// handle the eventual errorpanic(err)}
client.saveRule("ALGOLIA_INDEX_NAME","tagged-brand-rule",newRule().setConditions(Arrays.asList(newCondition().setPattern("brand: {facet:brand}").setAnchoring(Anchoring.CONTAINS).setAlternatives(false))).setConsequence(newConsequence().setParams(newConsequenceParams().setAutomaticFacetFilters(AutomaticFacetFilters.ofListOfAutomaticFacetFilter(Arrays.asList(newAutomaticFacetFilter().setFacet("brand")))).setQuery(newConsequenceQueryObject().setRemove(Arrays.asList("brand:","{facet:brand}"))))).setDescription("filter on brand: {brand}").setObjectID("tagged-brand-rule"));
1
2
3
4
5
6
7
8
9
10
11
12
constresponse=awaitclient.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',},});
varresponse=client.saveRule(indexName="ALGOLIA_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('ALGOLIA_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="ALGOLIA_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",},)
valresponse=Await.result(client.saveRule(indexName="ALGOLIA_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"))
letresponse=tryawaitclient.saveRule(indexName:"ALGOLIA_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
All the examples on this page use a product catalog index.
The appropriate attributes have been set as attributes for faceting.
The index has records that look like this:
[{"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}]