'optionalFilters' => [ 'attribute:value', ['attribute1:value', 'attribute2:value'], ]
Can be used in these methods:
search, browseObjects, generateSecuredApiKey, addApiKey, updateApiKey search, browse, generate_secured_api_key, add_api_key, update_api_key search, browseObjects, generateSecuredApiKey, addApiKey, updateApiKey search, browse_objects, generate_secured_api_key, add_api_key, update_api_key search, browse search, browseObjects, generateSecuredApiKey, addApiKey, updateApiKey searchAsync, browseAsync Search, Browse, GenerateSecuredApiKey, AddApiKey, UpdateApiKey Search, browse, generateSecuredApiKey, addApiKey, updateApiKey Search, BrowseObjects, GenerateSecuredAPIKey, AddAPIKey, UpdateAPIKey search, browse index, generateSecuredApiKey, add key, update key
About this parameter
Create filters for ranking purposes, where records that match the filter are ranked higher, or lower in the case of a negative optional filter.
Optional filtering behaves like normal filters, meaning that it returns records that match both the query and the filters. However, it also returns records that do not match the filters. The effect is on the ranking: records matching the filters are ranked higher than records that do not match the filters. If you use negative filters, items that match your filter are ranked lower than other records.
The boolean syntax is the same as facetFilters.
Promoting results: See how you can use
optionalFiltersto promote filters and facets.
Ranking Formula: This setting will only work if the Filters criterion is part of the ranking. Filters is by default part of the ranking; so if you’ve removed it, and yet wish to use option filters, you’ll need to add it back to the ranking formula.
Negative optional filters: It is possible to boost item that do not match your optional filter with the
-syntax. For example, if you want to promote items that do not belong to the category books, you can add a
Escape characters: If your facet value starts the
-character, you must escape it to prevent then engine from interpreting your filter as a negative filter. For example, if you want to add a filter on the category
-Movie, your filter should have an
For customers on the Community, Essential and Plus legacy plans who signed up before December 15, 2018, optional filters are limited to only one per query. For all other plans, optional filters are unlimited.
Apply optional filters on a search query
In this example, we boost all books written by John Doe.
1 2 3 4 5 6 $results = $index->search('query', [ 'optionalFilters' => [ "category:Book", "author:John Doe" ] ]);
Apply negative optional filters on a search query
In this example, we boost all books that are not written by John Doe.
1 2 3 4 5 6 $results = $index->search('query', [ 'optionalFilters' => [ "category:Book", "author:-John Doe" ] ]);