Elasticsearch / OpenSearch query DSL
last updated 2026-12
update 2026 - first written for Elasticsearch 0.20 (2013), rewritten for Elasticsearch 8/9 and OpenSearch 2/3 - all the same on both, see Elasticsearch / OpenSearch. the separate filters (filtered, and/or/not, missing, …) are gone - it's all queries now, run in query or filter context
the query DSL is a language for specifying queries in JSON. the snippets below go into the query element of a search request:
curl -XGET 'localhost:9200/twitter/_search' -H 'Content-Type: application/json' -d '{
"query" : {
"match" : { "message" : "this is a test" }
}
}'
this is by far not exhaustive documentation, it's just stuff i use the most; see the ES docs or OpenSearch docs for more. especially the boosting and scoring functionality (function_score) is not documented here to a proper extent
queries
match, multi_match
the match queries accept, analyze, and construct a query out of text/numeric/date. the match family of queries does not go through a "query parsing" process. it doesn't support field name prefixes, wildcard characters, or other "advanced" features
here, message is the name of the field to match in:
{
"match" : {
"message" : "this is a test"
}
}
by default, terms are OR'ed; to AND them:
{
"match" : {
"message" : {
"query" : "this is a test",
"operator" : "and"
}
}
}
to match a phrase:
{
"match_phrase" : {
"message" : "this is a test"
}
}
or using the last word as prefix (the "as you type" search):
{
"match_phrase_prefix" : {
"message" : "this is a test"
}
}
to match in multiple fields, with optional boosting, use:
{
"multi_match" : {
"query" : "this is a test",
"fields" : [ "subject^2", "message" ]
}
}
where matches in subject are "twice as important" as matches in message. wildcards work in field names ("fields" : ["title", "*_name"])
typos are forgiven by "fuzziness" : "AUTO" on match and multi_match
bool
the bool query provides a Boolean combination of queries with typed occurrence:
- must - clause must appear in matching documents, and adds to the score
- filter - must appear too, but doesn't affect the score (see filter context)
- should - should appear; if there is no
mustorfilterclause, at least oneshouldclause must be matched; you can also specify theminimum_should_matchparameter - must_not - must not appear; doesn't affect the score either
{
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter" : {
"term" : { "tag" : "tech" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{
"term" : { "tag" : "wow" }
},
{
"term" : { "tag" : "elasticsearch" }
}
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
each clause takes a single query or an array of them
boosting
boosting can be used to promote or demote search results:
{
"boosting" : {
"positive" : {
"term" : {
"field1" : "value1"
}
},
"negative" : {
"term" : {
"field2" : "value2"
}
},
"negative_boost" : 0.2
}
}
ids
match by ID:
{
"ids" : {
"values" : ["1", "4", "100"]
}
}
query_string
uses the query parser in order to parse its content (the Lucene syntax - AND, OR, NOT, field:value, "phrases", wild*, [1 TO 5], …)
{
"query_string" : {
"default_field" : "content",
"query" : "this AND that OR thus"
}
}
a syntax error in the query string fails the whole request - for a search box where users type, simple_query_string is safer (same idea, +, |, - operators, never fails, ignores what it can't parse)
parameters
query- actual query to be parseddefault_field- default field for query terms (if no prefix field specified); default is theindex.query.default_fieldsetting, which defaults to*(all fields that can be searched)fields- run query against multiple fields (provided as array):"fields" : ["content", "name"]- optionally with boosting:
"fields" : ["content", "name^5"] - wildcards may be used for fields:
"fields" : ["city.*"]if the document contains the objectcity - to check for existence or nonexistence of fields, use:
_exists_:field1andNOT _exists_:field2
default_operator- default operator used (if none explicitly specified); e.g. with default operatorOR, the query "capital of Hungary" is translated to "capital OR of OR Hungary"; default isORallow_leading_wildcard- are*or?allowed as the first character? defaulttrueboost- boost value of the query; default1.0minimum_should_match- percent value ("20%") controlling how many "should" clauses in the resulting boolean query should matchlenient- iftrue, format based failures (like providing text to a numeric field) are ignored
range
matches documents by a provided range, on numbers, dates, or strings (keyword fields, compared by term order)
{
"range" : {
"age" : {
"gte" : 10,
"lt" : 20,
"boost" : 2.0
}
}
}
gt= greater thangte= greater than or equallt= less thanlte= less than or equal
dates may use date math, rounded with /, and a format for the given values:
{
"range" : {
"post_date" : {
"gte" : "now-1d/d",
"lt" : "now/d"
}
}
}
{
"range" : {
"post_date" : {
"gte" : "01/01/2009",
"lte" : "2009",
"format" : "dd/MM/yyyy||yyyy"
}
}
}
term, terms
matches documents that have fields that contain a term (not analyzed) - use it on keyword, numeric, date fields, not on text (where Kimchy was indexed as kimchy, and a term query for Kimchy finds nothing)
{ "term" : { "user" : "kimchy" } }
{ "term" : { "user" : { "value" : "kimchy", "boost" : 2.0 } } }
the terms query simply accepts multiple terms, matching documents containing any of the terms:
{ "terms" : { "user" : ["kimchy", "elasticsearch"] } }
to match all of the terms, use a bool query with a term per value in filter (or terms_set, if the count comes from the document)
prefix, wildcard
on keyword fields, the same as term - not analyzed:
{ "prefix" : { "user" : "ki" } }
{ "wildcard" : { "user" : "k*y" } }
a leading * has to go through every term in the index - avoid it on big indices
exists
matches documents where a specific field has a value (not null, not []); the old missing is exists in a must_not:
{
"exists" : { "field" : "user" }
}
{
"bool" : {
"must_not" : {
"exists" : { "field" : "user" }
}
}
}
filter context
any query can be run in filter context - then it only says yes or no, doesn't compute a score, and the result can be cached. caching the result of a filter doesn't require a lot of memory, and will cause other queries executing against the same filter (same parameters) to be blazingly fast. caching is automatic, for filters used often enough - there's no _cache flag any more
use it for everything that isn't about relevance - exact values, ranges, dates, existence:
- the
filterandmust_notclauses of aboolquery - the
filterof aconstant_scorequery post_filterand thefilteraggregation in a search
combining filters
AND, OR and NOT are all a bool query - filter for AND, should for OR, must_not for NOT:
{
"bool" : {
"must" : {
"match" : { "name" : "shay" }
},
"filter" : [
{
"range" : {
"postDate" : {
"gte" : "2010-03-01",
"lt" : "2010-04-01"
}
}
},
{
"prefix" : { "user" : "ki" }
}
]
}
}
OR in filter context - a bool of should clauses, inside a filter:
{
"bool" : {
"must" : {
"match" : { "name" : "shay" }
},
"filter" : {
"bool" : {
"should" : [
{ "term" : { "user" : "kimchy" } },
{ "term" : { "tag" : "wow" } }
]
}
}
}
}
query_string, or any other query, works as a filter too:
{
"bool" : {
"filter" : {
"query_string" : {
"default_field" : "message",
"query" : "this AND that OR thus"
}
}
}
}
constant_score
wraps a filter and gives every match the same score (boost, default 1.0) - for when there's nothing to rank by:
{
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}
{
"constant_score" : {
"filter" : {
"range" : {
"age" : {
"gte" : 10,
"lt" : 20
}
}
},
"boost" : 1.2
}
}