Elasticsearch Elasticsearch Term Filter

By Opster Team

Updated: Aug 24, 2023

| 2 min read

Quick Links


The term filter is a fundamental component of Elasticsearch’s querying functionality. It allows users to filter documents based on exact matches in a specific field. This article will delve into the intricacies of the term filter, its usage, and best practices for optimizing its performance.

Understanding the term filter

The term filter is a non-scoring or filtering query that is used to filter the data based on exact values. It is often used in a filter context, where the query is used to filter the data, but the score of the query is not computed. The term filter is case-sensitive and does not analyze the input text, but there’s also a `case_insensitive` boolean parameter that can change this behavior. This means that the filter will only match the exact terms stored in the field.

Usage of term filter

The term filter can be used in various scenarios where exact matching is required. For instance, it can be used to filter documents based on a specific status, category, or any other field with exact values. Here is a basic example of a term filter:

  "query": {
    "term": {
      "status": "active"

In this example, the term filter will return all documents where the status field is exactly “active”.

Combining term filters

Multiple term filters can be combined using the bool query to create more complex filters. The bool query can include should, must, must_not, and filter clauses. Here is an example of a bool/filter query with multiple term filters:

  "query": {
    "bool": {
      "filter": [
        { "term": { "status": "active" } },
        { "term": { "category": "electronics" } }

In this example, the query will return all documents that are both “active” and belong to the “electronics” category.

Performance optimization

While the term filter is highly efficient, there are several practices that can further optimize its performance:

1. Avoid High Cardinality Fields: High cardinality fields have a large number of unique values, which can slow down term filters. It’s best to use term filters on fields with lower cardinality.

2. Use Keyword Fields: Since the term filter does not analyze the input text, it’s best used with keyword fields, which store data as a single, unanalyzed string.

3. Leverage Filter Context: Using the term filter in a filter context can improve performance as it skips the scoring phase.

Handling missing values

If a document does not have a value for the field specified in the term filter, it will not match the filter. To handle such scenarios, you can use the exists query to check if a field exists or is missing in the document.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?