Elasticsearch Elasticsearch Filter Query

By Opster Team

Updated: Aug 25, 2023

| 2 min read

If you want to learn about Elasticsearch filters, check out this guide

Quick Links

Introduction

One of the most useful aspects of Elasticsearch is its Query DSL (Domain Specific Language), which is used for defining queries. The Query DSL is built on top of the Elasticsearch REST API. It allows developers to build complex queries and is highly flexible. Among the wide range of query types available in Elasticsearch, the Filter Query stands out due to its efficiency and speed.

Understanding Elasticsearch filter query

Filter queries are usually meant to be used for what is called structured search. Structured search usually takes the form of a sequence of queries that either match or don’t match a given set of documents, i.e. there is no scoring involved. The main goal of filter queries in Elasticsearch is to pre-filter the document set on which a full-text query will run, so that the more expensive ranking process runs on as few documents as possible.

Filter queries are cacheable, meaning Elasticsearch can remember the result of a filter and reuse it for subsequent queries. This feature significantly improves the performance of repeated queries on static data.

Implementing Elasticsearch filter query

To implement a filter query, you can use the `bool` query, which combines multiple query clauses. The `bool` query has four types of clauses: `must`, `filter`, `should`, and `must_not`. The `filter` clause is used to filter the results of the query.

Here is an example of a filter query that returns all documents where the `age` field is greater than 30:

json
GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gt": 30
          }
        }
      }
    }
  }
}

In this example, the `range` query is used inside the `filter` clause to filter the results. The `range` query supports several operators, including `gt` (greater than), `gte` (greater than or equal to), `lt` (less than), and `lte` (less than or equal to).

Optimizing Elasticsearch filter query

While filter queries are already efficient, there are several ways to further optimize them for better performance:

1. Use filter context: The filter context is used in filter queries to filter the results of a query. It does not score the results but does cache them, making it faster and more efficient than the query context, which uses clauses like `should` and `must`.

2. Avoid scripts: Scripts are flexible but can be slow and consume a lot of resources. Whenever possible, use simple queries instead of scripts.

3. Use the `terms` filter: The `terms` filter is used to filter the results by multiple values. It is more efficient than using multiple `term` filters.