Skip to main content

About this widget

A FilterState is a class that holds one or several filters, organized in groups. The FilterState can be modified at any moment by adding or removing filters, which will be applied to searches performed by the connected Searcher. You can use three filter types:
  • Filter.Facet
  • Filter.Numeric
  • Filter.Tag
A group of filters must be identified by a FilterGroupID. A FilterGroupID must have:
  • A name
  • A FilterOperator, which can either be FilterOperator.And or FilterOperator.Or
A FilterOperator indicates which type of boolean operator should be applied between each filter in the group. See also:

Examples

Add and remove filters from a FilterState.
Kotlin
val filterState = FilterState()
val filterGroupID = FilterGroupID("myGroup", FilterOperator.And)
val color = Attribute("color")
val red = Filter.Facet(color, "red")
val green = Filter.Facet(color, "green")
val blue = Filter.Facet(color, "blue")

filterState.add(filterGroupID, red, green, blue)
// "color:red AND color:green AND color:blue"
filterState.remove(filterGroupID, green)
// "color:red AND color:blue"
Notify and listen to changes.
Kotlin
val filterState = FilterState()
val filterGroupID = FilterGroupID("myGroup", FilterOperator.And)
val color = Attribute("color")
val facets = setOf(
    Filter.Facet(color, "red"),
    Filter.Facet(color, "green"),
    Filter.Facet(color, "blue")
)
filterState.notify {
    add(filterGroupID, facets)
}
filterState.filters.subscribe { filters ->
    assertEquals(facets, filters.getFacetFilters(filterGroupID))
}
Convert a FilterState to an SQL-like string expression which can be used with a Query.
Kotlin
val query = Query()

query.facetFilters = FilterGroupsConverter.SQL(FilterState().toFilterGroups())
I