Configuring custom filters for Elasticsearch
Use custom filters as an advanced setting in your agents to enable more advanced search techniques. Configure custom filters to use them as global filters that apply to all user queries.
The following code snippet shows an example of a custom filter:
[
{
"bool": {
"must": [
{
"term": {
"title": "A title"
}
}
],
}
}
]
If you also configure a custom query body as an advanced Elasticsearch setting, include the $FILTER variable in the query body to apply the custom filters.
The following code snippet shows an example of a query body with the $FILTER variable:
{
"query": {
"bool": {
"should": [
{
"text_expansion": {
"ml.tokens": {
"model_id": ".elser_model_2_linux-x86_64",
"model_text": "$QUERY"
}
}
}
],
"filter": "$FILTER"
}
}
}
Examples of filter object
Explore the use cases for filtering search results with custom filters. The following examples assume that the title, text and id fields are available in your Elasticsearch index. Among them, title and text are text type fields, while id is a keyword type field.
AND
[
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
This filter object filters the search results by using the following conditions:
titlecontains "A_keyword_in_title", ANDtextcontains "A_keyword_in_text", ANDidis equal to "A_specific_ID".
OR
[
{
"bool": {
"should": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
}
}
]
This filter object filters the search results by using the following conditions:
titlecontains "A_keyword_in_title", ORtextcontains "A_keyword_in_text", ORidis equal to "A_specific_ID".
NOT
[
{
"bool": {
"must_not": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
}
}
]
This filter object filters the search results by using the following conditions:
titledoes not contain "A_keyword_in_title", ANDtextdoes not contain "A_keyword_in_text", ANDidis not equal to "A_specific_ID".
(a AND b) OR c
[
{
"bool": {
"should": [
{
"match": {
"id": "A_specific_ID"
}
}
{
"bool": {
"filter": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
}
]
}
}
]
}
}
]
This filter object filters the search results by using the following conditions:
titlecontains "A_keyword_in_title" ANDtextcontains "A_keyword_in_text", ORidis not equal to "A_specific_ID"
For more information about the bool query, see Boolean query in Elasticsearch documentation.