liveBook · Manning

Filter clause

As explained in Elasticsearch in Action, Second Edition

A filter clause is an Elasticsearch bool-query clause that restricts matching documents without affecting relevance scores.

When a product search must return only matching documents without ranking them by the restriction, place the condition in a bool query’s filter clause. It behaves like must for matching, but runs in filter context: results receive no scores, and Elasticsearch can cache them for better performance. section 11.3.7 section 11.3

Filter TVs by price

A filter can combine a term condition with a price range: match the exact product value TV, then require a price from 500 through 1,000. The returned documents have a score of 0.0 because no relevance scoring is required. section 11.3.7

Use filter clauses for fixed search constraints:

  • Combine a must clause for relevance with a filter for exact constraints, such as LG products priced from 500 through 1,000. section 11.3.7
  • Combine filter with must_not and should when a search has exclusions, optional preferences, and a non-scoring price boundary. section 11.3.8
  • Keep relevance-bearing text queries in must or should, because filter conditions do not affect document ranking. section 11.3.6 section 11.3.7
Listing 11.12 The filter clause
GET products/_search
{
  "query": {
    "bool": {
      "filter": [{    #1
          "term": {
            "product.keyword": "TV"
          },
          {
            "range": {
              "price": {
                "gte": 500,
                "lte": 1000
              }
            }
          }
        }
      ]
    }
  }
}

Inspect the focused REST example combining an exact product condition and a price range inside filter. section 11.3.7

Listing 11.14 All the clauses combined
GET products/_search
{
  "query": {
    "bool": {
      "must": [{
          "match": {
            "brand": "LG"
          }
        }],
      "must_not": [{
          "term": {
            "colour": "silver"
          }
        }],
      "should": [{
          "match": {
            "energy_rating": "A++"
          }
        },
        {
          "term": {
            "type": "Fridge Freezer"
          }
        }],
      "filter": [{
          "range": {
            "price": {
              "gte": 500,
              "lte": 1000
            }
          }
        }
      ]
    }
  }
}

Inspect a complete bool query that combines must, must_not, should, and filter clauses for a realistic product search. section 11.3.8

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains matching, filter context, zero scores, caching, and combination with must, but defers deeper execution internals.
Examples
Uses product, brand, color, type, energy rating, and price constraints in Elasticsearch product-search requests.
What it assumes
Assumes familiarity with bool queries and leaf queries while distinguishing query context from filter context.
What you can do after
Apply it via Elasticsearch bool queries to constrain results without changing relevance scores.
How it connects
Connects filter clauses to bool queries, must clauses, must_not clauses, should clauses, query context, and filter context.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.