facetFilter method
FacetFilters
Filter hits by facet value.
Usage notes:
- Format: The general format for referencing a facet value is
${attributeName}:${value}
. This attribute/value combination represents a filter on a given facet value. - Multiple filters: If you specify multiple filters, they are interpreted as a conjunction (AND). If you want to use a disjunction (OR), use a nested array.
['category:Book', 'author:John Doe']
translates ascategory:Book AND author:'John Doe'
.[['category:Book', 'category:Movie'], 'author:John Doe']
translates as(category:Book OR category:Movie) AND author:'John Doe'
.
- Negation is supported by prefixing the value with a minus sign
(-)
, sometimes called a dash. For example:['category:Book', 'category:-Movie']
translates ascategory:Book AND NOT category:Movie
. - Escape characters: On the other hand, if your facet value starts with a
-
, meaning it contains the-
, then you can escape the character with a\
to prevent the engine from interpreting this as a negative facet filter. For example, filtering oncategory:\-Movie
will filter on all records that have a category equal to “-Movie”.
Source: Learn more
Implementation
AlgoliaQuery facetFilter(dynamic value) {
assert(value is String || value is List<String>,
'value must be either String | List<String> but was found `${value.runtimeType}`');
final facetFilters = List<dynamic>.from(_parameters['facetFilters']);
assert(facetFilters.where((dynamic item) => value == item).isEmpty,
'FacetFilters $value already exists in this query');
facetFilters.add(value);
return _copyWithParameters(<String, dynamic>{'facetFilters': facetFilters});
}