ElasticSearch Query DSL
ES's Query DSL is a language for specifying queries in JSON.
This is by far not an exhaustive documentation, it's just stuff I use the most; see official documentation for more. Especially the boosting and scoring functionality is not documented here to proper extent.
Contents
Queries
match, multi_match
The match queries accept, analyze, and construct query out of text/numeric/date. The match family of queries does not go through a "query parsing" process. It does not support field name prefixes, wildcard characters, or other "advance" features.
Here, message is name of the field to match in (can be also _all):
{ "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 matched in message.
bool
The bool query provides a Boolean combination of queries with typed occurrence:
- must - clause must appear in matching documents,
- should - should appear; is no must clause is provided, at least one should clause must be matched; you can also specify minimum_number_should_match parameter,
- must_not appear.
{ "bool" : { "must" : { "term" : { "user" : "kimchy" } }, "must_not" : { "range" : { "age" : { "from" : 10, "to" : 20 } } }, "should" : [ { "term" : { "tag" : "wow" } }, { "term" : { "tag" : "elasticsearch" } } ], "minimum_number_should_match" : 1, "boost" : 1.0 } }
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" : { "type" : "my_type", "values" : ["1", "4", "100"] } }
Note: type field is optional, and may contain array of values.
field
Query only on a specified field (equivalent of query_string with default_field):
{ "field" : { "name.first" : "+something -else" } }
filtered
Filters results of a query; may be much faster than querying, as no scoring is done, and may be cached:
{ "filtered" : { "query" : { "term" : { "tag" : "wow" } }, "filter" : { "range" : { "age" : { "from" : 10, "to" : 20 } } } } }