WARNING: Version 5.6 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Bool Query
editBool Query
editA query that matches documents matching boolean combinations of other
queries. The bool query maps to Lucene BooleanQuery
. It is built using
one or more boolean clauses, each clause with a typed occurrence. The
occurrence types are:
Occur | Description |
---|---|
|
The clause (query) must appear in matching documents and will contribute to the score. |
|
The clause (query) must appear in matching documents. However unlike
|
|
The clause (query) should appear in the matching document. If the
|
|
The clause (query) must not appear in the matching
documents. Clauses are executed in filter context meaning
that scoring is ignored and clauses are considered for caching. Because scoring is
ignored, a score of |
Bool query in filter context
If this query is used in a filter context and it has should
clauses then at least one should
clause is required to match.
The bool query also supports disable_coord
parameter (defaults to
false
) which changes how the classic
similarity calculates the bool
query’s score. Basically the coord similarity computes a score factor based
on the fraction of all query terms that a document contains. See Lucene
BooleanQuery
for more details.
The bool
query takes a more-matches-is-better approach, so the score from
each matching must
or should
clause will be added together to provide the
final _score
for each document.
POST _search { "query": { "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 } } }
Scoring with bool.filter
editQueries specified under the filter
element have no effect on scoring — scores are returned as 0
. Scores are only affected by the query that has
been specified. For instance, all three of the following queries return
all documents where the status
field contains the term active
.
This first query assigns a score of 0
to all documents, as no scoring
query has been specified:
GET _search { "query": { "bool": { "filter": { "term": { "status": "active" } } } } }
This bool
query has a match_all
query, which assigns a score of 1.0
to
all documents.
GET _search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "term": { "status": "active" } } } } }
This constant_score
query behaves in exactly the same way as the second example above.
The constant_score
query assigns a score of 1.0
to all documents matched
by the filter.
GET _search { "query": { "constant_score": { "filter": { "term": { "status": "active" } } } } }
Using named queries to see which clauses matched
editIf you need to know which of the clauses in the bool query matched the documents returned from the query, you can use named queries to assign a name to each clause.