Skip to main content

TermFilter

When you want to apply filters to your search and don't want them to be displayed as facets. TermFilter applies a text filter to a particular field to the search.

Adding a filter to Searchkit Config

Within the Searchkit config, there is a filters array which allows you to configure one or more filters for your search. Below is an example of adding a term filter to search.

const config = {
host: 'http://localhost:9200',
index: 'movies',
hits: {
fields: ['actors', 'writers'],
},
filters: [
new TermFilter({
identifier: 'type',
field: 'type',
label: 'type',
}),
],
};

const request = Searchkit(config);
const response = await request
.setFilters([{identifier: 'type', value: 'movies'}])
.execute({
hits: {
size: 10,
from: 0,
},
});

Options

OptionDescription
fieldfield to be used for term filter, preferably a field that is raw, not tokenized
identifierRequired to be unique. Used to apply filters on field
labelUI label for appliedFilters node.
displayOptional. Used on UI to specify what component to handle filter in SelectedFilters

The filters that you will apply will also be returned in the response under summary.appliedFilters.

{
"hits": {
"items": [
// 10 items
],
"page": {
"from": 0,
"pageNumber": 0,
"size": 10,
"total": 11,
"totalPages": 2
}
},
"sortedBy": undefined,
"summary": {
"appliedFilters": [
{
"display": "TermFilter",
"id": "type_movies",
"identifier": "location",
"label": "Location",
"value": "movies"
}
],
"disabledFilters": [],
"query": "",
"sortOptions": [],
"total": 11
}
}