> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# FilterState

> Provides interface for managing applied filters.

## 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`](/doc/api-reference/widgets/instantsearch/android).

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:

* [Get started with imperative UI](/doc/guides/building-search-ui/getting-started/how-to/programmatically/android)
* [Filtering](/doc/guides/managing-results/refine-results/filtering)
* [Filtering and boolean operators](/doc/guides/managing-results/refine-results/filtering/in-depth/combining-boolean-operators)

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-android/blob/master/examples/android/src/main/kotlin/com/algolia/instantsearch/examples/android/showcase/androidview/search/SearchAsYouTypeShowcase.kt">
  Browse the FilterState example code on GitHub.
</Card>

## Examples

Add and remove filters from a `FilterState`.

```kotlin Kotlin icon=code theme={"system"}
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.

```kotlin Kotlin icon=code theme={"system"}
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](/doc/guides/managing-results/refine-results/filtering/in-depth/combining-boolean-operators)
which can be used with search parameters.

```kotlin Kotlin icon=code theme={"system"}
import com.algolia.client.model.search.SearchParamsObject

val query = SearchParamsObject()

val filtersString = FilterGroupsConverter.SQL(filterState.toFilterGroups())
val queryWithFilters = query.copy(filters = filtersString)
```
