Filters
There are a number of ways to apply filters to your search results. Filters can be applied on:
Applying Filters on the Client
Filters can be applied on the client by using the filters
property on the configure
component on Instantsearch.
Setup
To apply filters on the client, you must first setup the filter in either the facet_attributes
and filter_attributes
configuration in your application settings.
Below is an example of a configuration that has a facet_attributes
and filter_attributes
setup.
{
facet_attributes: [
{ attribute: 'author', field: 'author.keyword', type: 'string' },
{ attribute: 'price', field: 'price', type: 'numeric' },
],
filter_attributes: [
{ attribute: 'publisher', field: 'publisher.keyword', type: 'string' },
{ attribute: 'genre', field: 'genre.keyword', type: 'string' },
{ attribute: 'date', field: 'date.field', type: 'date' },
{ attribute: 'nestedField', field: 'price', type: 'string', nestedPath: 'nested' }
],
result_attributes: [],
search_attributes: []
}
import { Configure } from 'react-instantsearch';
<Configure
filters="price:[90 TO *]"
/>
Filters can be applied on the client by using the filters
property on the configure
component on Instantsearch.
Use the attribute
property to specify the field you want to filter on.
Nested fields are not supported in the filters
property. To filter on nested fields, use facetFilters
or numericFilters
property.
Filter Syntax
Numeric & Facet Filters
Numeric & Facet filters are typically used by widgets.
You can also add numeric filters or facet Filters to the search by using the numericFilters
or facetFilters
property on the configure
component on Instantsearch.
import { Configure } from 'react-instantsearch';
<Configure
facetFilters={["author:John"]}
numericFilters={["price>10", "price<20"]}
/>
Applying Filters on the Server
Filters can be applied on the server by using the getBaseFilters
function within the handleRequest
function.
Examples could include:
- Filtering out documents that are not published
- Filtering out documents that the user doesn't have access to
Below is an example of filtering documents that are not published.
Attributes cannot be used within getBaseFilters
function. You must use the field
property instead. This is because the field
property is the actual field name in Elasticsearch.
The function must return an array of filters. Each filter is an object that follows the Elasticsearch Query DSL (opens in a new tab).
Below is an example of filtering documents that are not published using the status
field.
const results = await apiClient.handleRequest(req.body, {
getBaseFilters: () => {
return [
{
bool: {
must: {
term: {
status: "published",
},
},
},
},
];
},
});