API Reference / API Parameters / optionalFilters
Type: list of strings
Engine default: []
Parameter syntax
'optionalFilters' => [
    'attribute:value',
    ['attribute1:value', 'attribute2:value'],
]

Can be used in these methods:

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.

Usage notes:

  • The boolean syntax is the same as facetFilters.

  • Promoting results: See how you can use optionalFilters to 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 category:-Books filter.

  • 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 \ before the - character: category:\-movie.

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.

Examples

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"
  ]
]);

Did you find this page helpful?