0Pricing
Elasticsearch & Full Text Search Systems · Lesson

Filtering, Ranges, and Query Context

Master the difference between query and filter context and use range and term filters to narrow results efficiently.

Filtering, Ranges, and Query Context is a free Elasticsearch & Full Text Search Systems lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elasticsearch & Full Text Search Systems learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Contexts

Every clause in Elasticsearch runs in one of two contexts: query context (How well does this match? produces a score) or filter context (Does this match? yes/no, no score).

Why Filters Are Fast

Filters do not compute relevance scores and their results are cached, so repeated filters are very cheap. Use filters whenever you do not need ranking from that clause.

The bool filter Clause

Inside a bool query, the filter array runs in filter context while must runs in query context.

GET /products/_search
{ "query": { "bool": {
  "must":   [ { "match": { "name": "laptop" } } ],
  "filter": [ { "term": { "in_stock": true } } ]
}}}

Range Queries

The range query selects values within bounds using gte, gt, lte, lt. It is commonly placed in filter context.

{ "range": { "price": { "gte": 100, "lte": 500 } } }

Date Ranges

Range works on dates too, including date math like now-7d for relative windows.

{ "range": { "created": { "gte": "now-7d/d" } } }

terms Filter

The terms query matches any of several exact values, like a SQL IN list.

{ "terms": { "category": [ "books", "music" ] } }

exists Filter

The exists query keeps only documents that have a non-null value for a field.

{ "exists": { "field": "discount" } }

Combining Many Filters

Stack multiple filters in the filter array; they are ANDed together and all benefit from caching.

"filter": [
  { "term":  { "in_stock": true } },
  { "range": { "price": { "lte": 1000 } } }
]

must_not for Exclusion

Use must_not inside bool to exclude documents. It also runs in filter context.

"must_not": [ { "term": { "discontinued": true } } ]

Sorting Without Scores

When you rely on filters alone, every document scores the same, so add an explicit sort (for example by price or date) to order results.

"sort": [ { "price": "asc" } ]

Performance Rule of Thumb

Put the scoring text match in must and push every yes/no condition into filter. This keeps scoring focused and lets caching speed up the rest.

Quick Check

When should a clause go in filter context?

Recap

You learned the query vs filter context distinction, used range, terms, and exists filters inside bool, applied must_not for exclusion, and added explicit sorting when relying on filters.

Frequently asked questions

Is the “Filtering, Ranges, and Query Context” lesson free?

Yes — the full text of “Filtering, Ranges, and Query Context” is free to read here on the web, and the Elasticsearch & Full Text Search Systems course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elasticsearch & Full Text Search Systems course, upgrade to CoddyKit PRO.

What will I learn in “Filtering, Ranges, and Query Context”?

Master the difference between query and filter context and use range and term filters to narrow results efficiently. You practise Elasticsearch & Full Text Search Systems with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Elasticsearch & Full Text Search Systems?

No prior experience is required. Elasticsearch & Full Text Search Systems on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Filtering, Ranges, and Query Context” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Elasticsearch & Full Text Search Systems lesson?

Yes. Every Elasticsearch & Full Text Search Systems lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Query DSL
  2. Term and Match Queries
  3. Combining Queries with Bool
  4. Filtering, Ranges, and Query Context
← Back to Elasticsearch & Full Text Search Systems