Developer and Text Tools

Elasticsearch Query DSL Made Clear: bool, term, range and Aggregations

How bool queries, match versus term, ranges and aggregations work in Elasticsearch, common DSL mistakes, and how to turn Lucene query strings into DSL.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
Elasticsearch Query DSL Made Clear: bool, term, range and Aggregations

Elasticsearch searches are written in JSON, and the Query DSL rewards precision. One extra level of nesting, a term query on the wrong field type or an aggregation with two types, and the cluster returns an error or, more confusingly, zero hits. This guide explains the core building blocks and shows how the Elasticsearch Query DSL Builder composes and checks them.

The bool query

Almost every real search is a bool query with four lists. Clauses in must have to match and contribute to the relevance score. Clauses in filter have to match too, but they do not score, and Elasticsearch can cache them, so they are the right place for dates, status values and IDs. should clauses are optional when must or filter is present and only raise the score; on their own, at least one of them has to match. must_not removes documents and never scores.

A useful habit is to ask of each condition: should this change the ranking? If not, it belongs in filter. A search for recent error logs, for example, has a match on the message in must and the time range and log levels in filter.

match versus term

This is the most common source of empty results. Text fields are analysed when documents are indexed: "Wireless Headphones" is stored as the tokens wireless and headphones. A match query analyses your input the same way, so it finds the document. A term query does not analyse anything, so the term "Wireless Headphones" looks for one exact token that does not exist.

Use term, terms and range on keyword, numeric, date and boolean fields. Most dynamic mappings add a keyword sub-field, so title.keyword holds the exact original value. When you tell the builder your field types, it warns as soon as a term query targets a text field.

Ranges and dates

A range query uses gte, gt, lte and lt. Writing ">=" as a key is a frequent mistake and is rejected. Date ranges accept date math such as now-24h or now-7d/d, which rounds to the start of the day. A format parameter tells Elasticsearch how to read dates written as plain strings.

Paging and sorting

size sets how many hits come back and from skips results. Together they cannot pass 10,000, the default result window. For deeper paging, use search_after with a point in time. Sorting on a text field fails because text is not stored for sorting; sort on the keyword sub-field instead. When you only want aggregations, set size to 0; the response is smaller and the request can be cached.

Aggregations

Aggregations summarise the matching documents. Bucket aggregations such as terms, date_histogram, histogram and range split documents into groups, and they can hold sub-aggregations. Metric aggregations such as avg, sum, max, cardinality and percentiles compute numbers and cannot have children. Each aggregation must have exactly one type.

date_histogram changed a few versions ago. The old interval parameter was removed; use calendar_interval for calendar units such as 1d, 1w or 1M, which respect month lengths and daylight saving, or fixed_interval for exact durations such as 12h. calendar_interval only accepts a single unit, so 2d must be written as a fixed interval. The validator reports each of these cases with the line number.

From Lucene strings to DSL

Kibana's search bar and the q parameter use the Lucene query syntax: status:active AND (type:book OR type:ebook) -deleted:true. It is quick to type but hard to combine with other clauses or to reuse in code. The builder parses it with the correct precedence, where AND binds tighter than OR and a leading minus means must_not, and writes the matching bool query. Quoted phrases become match_phrase, square and curly brackets become inclusive and exclusive ranges, a trailing star becomes a prefix query, and _exists_:field becomes an exists query. Fields you marked as keyword become term queries.

Validating a request you already have

Paste a request body into validate mode, with or without the GET index/_search line. The tool uses its own JSON parser, so it can point to the exact line of a trailing comma or a duplicate key. It then checks top-level keys, query types, the number of fields in match and term, range operators, empty bool lists, the result window and every aggregation. Paste the output of GET index/_mapping into the field box and the checks also cover term on text fields, aggregations on text and metrics on non-numeric fields.

Running the query

When the tree looks right, copy the Dev Tools request into Kibana, or copy the curl command and add your credentials. The builder never contacts a cluster, so it cannot tell how many documents will match; it makes sure the request is well formed and that the common traps are avoided before you run it.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages