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, you must include the $FILTER variable in the query body to use 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:

  • title contains "A_keyword_in_title", AND
  • text contains "A_keyword_in_text", AND
  • id is 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:

  • title contains "A_keyword_in_title", OR
  • text contains "A_keyword_in_text", OR
  • id is 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:

  • title does not contain "A_keyword_in_title", AND
  • text does not contain "A_keyword_in_text", AND
  • id is 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:

  • title contains "A_keyword_in_title" AND text contains "A_keyword_in_text", OR
  • id is not equal to "A_specific_ID"

For more information about the bool query, see Boolean query Icon for redirecting to external pages. in Elasticsearch documentation.