setOptionalFilter method

AlgoliaQuery setOptionalFilter(
  1. String value
)

OptionalFilters

Create filters for ranking purposes, where records that match the filter are ranked highest.

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.

Usage notes:

  • The boolean syntax is the same as facetFilters, except for negative syntax. If you use negative syntax, for example categories:-Book, it will not work; it will be transformed into categories:Book.
  • 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.

Warning:

Optional filters are not available on legacy plans (Starter, Growth, Pro). If you are on one of these plans and want to benefit from this feature, you will need to move to a new plan. Additionally, Community, Essential, and Plus plans are limited to only one optionalFilters per query, while Business and Enterprise plans are unlimited.

Source: Learn more

Implementation

AlgoliaQuery setOptionalFilter(String value) {
  final optionalFilters = List<String>.from(_parameters['optionalFilters']);
  assert(optionalFilters.where((String item) => value == item).isEmpty,
      'OptionalFilters $value already exists in this query');
  optionalFilters.add(value);
  return _copyWithParameters(
      <String, dynamic>{'optionalFilters': optionalFilters});
}