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.
import com.algolia.instantsearch.filter.Filter
val filterState = FilterState()
val filterGroupID = FilterGroupID("myGroup", FilterOperator.And)
val color = "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.
import com.algolia.instantsearch.filter.Filter
val filterState = FilterState()
val filterGroupID = FilterGroupID("myGroup", FilterOperator.And)
val color = "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 search parameters.
import com.algolia.client.model.search.SearchParamsObject
val query = SearchParamsObject()
val filtersString = FilterGroupsConverter.SQL(filterState.toFilterGroups())
val queryWithFilters = query.copy(filters = filtersString)
Last modified on February 9, 2026