Catalog My learning Blog
Elasticsearch Tutorial: How Full-Text Search Works · Phase 2 · Lesson 12 · 3 min read

Elasticsearch Filter vs Query Context

On this page
  1. Same results, different scores
  2. Query context vs filter context
  3. Why filters are faster: no scoring, cached bitsets
  4. When to use filter vs must
  5. Using filters for tenant and permission checks
  6. Check yourself

Same results, different scores

The product search from Day 8 written two ways. With yes/no conditions in filter:

Code
total 2
  102    1.162   Wireless Keyboard
  101    1.068   Wireless Mouse

With the same conditions moved into must:

JSON
{ "bool": { "must": [
    { "match": { "description": "wireless" } },
    { "term":  { "category": "electronics" } },
    { "range": { "price": { "lte": 50 } } },
    { "term":  { "in_stock": true } } ] } }
Code
total 2
  102    3.437   Wireless Keyboard
  101    3.342   Wireless Mouse

The same two documents, but the scores tripled. Each must clause adds its own score: being "in electronics" and "in stock" counts as relevance, even though every result shares those properties. Here the order happened to survive. With more varied data, rare categories or narrow price ranges get IDF boosts and reshuffle results for reasons unrelated to what the user searched for.

Query context vs filter context

Think of a door with a bouncer and a judge. The bouncer (filter context) only decides who gets in: right category, in stock, under the price limit. The judge (query context) then scores how well each person who got in matches what the user asked for. Asking the judge to also score "is in stock" adds noise to the ranking and work to every search.

Query context (`must`, `should`, top-level `match`)Filter context (`filter`, `must_not`)
QuestionHow well does it match?Does it match?
ScoringComputedSkipped
CachingNoFrequently used filters cached as bitsets
Use forFull-text search termsCategories, ranges, dates, statuses, permissions, tenants
Query clauses compute relevance scores; filter clauses only include or exclude documents, skip scoring, and can be reused from a cache.
Query clauses compute relevance scores; filter clauses only include or exclude documents, skip scoring, and can be reused from a cache.

Why filters are faster: no scoring, cached bitsets

  • No scoring work: for every candidate document, scoring costs computation; filters just answer yes or no.
  • Caching: Elasticsearch caches the results of filters that are reused often on large segments, as compact bitsets. A filter like {"term": {"tenant_id": "acme"}} used on every request becomes nearly free.
  • Early elimination: restrictive filters shrink the set of documents that scoring has to visit.

The request cache also caches whole responses for size: 0 aggregation requests on unchanged shards, which helps dashboards.

When to use filter vs must

  • If it's a yes/no business rule, it's a filter: category, status, in_stock, price ranges, date windows, tenant_id, permissions.
  • If the user typed it, it's a query: the search box text.
  • Purely structured searches (no text) can be a bool with only filter clauses, or wrapped in constant_score; every hit gets the same score, so sort by a field instead.
  • Round "now" in date filters ("gte": "now-7d/d") so the filter is identical for a whole day and can be cached; now-7d changes every millisecond.

Using filters for tenant and permission checks

Mandatory restrictions (a customer only seeing their own orders, a tenant only their own data) belong in a filter that the application adds to every query, not in user-controlled query text. For stronger isolation, Elasticsearch's document-level security can enforce this per role (a paid feature, Day 26), or use separate indices per tenant.

Check yourself

Each must clause adds its own score, so conditions that every result shares were counted as extra relevance, shifting scores without improving the ranking. In filter context they would have only narrowed the results.

A filter's result is a fixed yes/no set of documents for a segment, which can be reused; scores depend on the query and aren't cached.

now-7d changes every millisecond, so each request is a different filter that can't be reused from cache. Rounding to the day with /d keeps the filter identical across requests all day, so it's cached.

They're strict yes or no conditions that shouldn't affect relevance, and enforcing them in filter context makes them fast and cacheable.

Yes, a bool query with only filter clauses, or constant_score, returns matches with equal scores, which is fastest for listing pages.