About this widget
AFilterState
consists of one or several filters, organized in groups.
Each group can contain one or more of these filter types:
Facet.Filter
Facet.Numeric
Facet.Tag
FilterState
provides a simple interface to deal with filter grouping and their respective boolean operator.
For more details, you can read more about filter grouping and boolean operators.
Filter groups can be:
-
Conjunctive groups
Search results will only contain hits that match all the filters in a conjunctive filter group. In other words, it represents a boolean and relationship between filters in this group.
For example, if
FilterState
contains a conjunctive group of filterssize:42
andcategory:shirt
, records that match both filters will be returned. A conjunctive group may contain filters of any type at the same time. -
Disjunctive groups
Search results will contain hits that match any the filters in a disjunctive filter group. In other words, it represents a boolean or relationship between filters in this group.
For example, if
FilterState
contains a disjunctive group of filterscolor:red
andcolor:blue
, records that match any of these filters will be returned. Disjunctive group may only contain filters of the same type. For example, you can’t put a facet filter and a tag filter in the same disjunctive group.
FilterState
behave as if they’re combined by an AND
operator.
That means that a record matches the set of filters in FilterState
only if it satisfies all the groups simultaneously.
Examples
To access filter groups,FilterState
provides a subscript syntax.
Each group is identified by its name (string).
You can access groups by using [and: "groupName"]
for conjunctive groups and [or: "groupName"]
for disjunctive groups.
Groups are automatically created when you add the first filter,
and are deleted when they’re empty.
Swift
Swift
FilterState
provide convenient functionalities such as filter removal:
Swift
Swift
Transform to SQL syntax string
InstantSearch provides a convenient way to transform aFilterState
into a valid SQL-like string expression
which can be used with Query
.
Swift
CustomStringConvertible conformity
FilterState
conforms to the CustomStringConvertible
protocol which provides the same valid SQL-like string with the description
property.
Therefore, the previous snippet can be reduced to this:
Swift