🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / InstantSearch Android / Widgets

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.

There are three types of filters:

  • Filter.Facet
  • Filter.Numeric
  • Filter.Tag

You can read more about each filter type in our filtering guide.

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 filters in the group. For advanced filtering, you can read more about filter grouping and boolean operators.

Examples

Add and remove filters from a FilterState.

1
2
3
4
5
6
7
8
9
10
11
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.

1
2
3
val query = Query()

query.filters = FilterGroupsConverter.SQL(FilterState().toFilterGroups())
Did you find this page helpful?