2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-bool-query]]
|
2015-05-05 02:27:52 -04:00
|
|
|
== Bool Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
A 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:
|
|
|
|
|
|
|
|
[cols="<,<",options="header",]
|
|
|
|
|=======================================================================
|
|
|
|
|Occur |Description
|
2015-05-13 06:04:56 -04:00
|
|
|
|`must` |The clause (query) must appear in matching documents and will
|
|
|
|
contribute to the score.
|
|
|
|
|
|
|
|
|`filter` |The clause (query) must appear in matching documents. However unlike
|
|
|
|
`must` the score of the query will be ignored.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|`should` |The clause (query) should appear in the matching document. In
|
|
|
|
a boolean query with no `must` clauses, one or more `should` clauses
|
|
|
|
must match a document. The minimum number of should clauses to match can
|
|
|
|
be set using the
|
|
|
|
<<query-dsl-minimum-should-match,`minimum_should_match`>>
|
|
|
|
parameter.
|
|
|
|
|
|
|
|
|`must_not` |The clause (query) must not appear in the matching
|
|
|
|
documents.
|
|
|
|
|=======================================================================
|
|
|
|
|
2015-05-05 02:27:52 -04:00
|
|
|
IMPORTANT: If this query is used in a filter context and it has `should`
|
|
|
|
clauses then at least one `should` clause is required to match.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
The bool query also supports `disable_coord` parameter (defaults to
|
|
|
|
`false`). 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.
|
|
|
|
|
2014-11-24 10:07:18 -05:00
|
|
|
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.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"term" : { "user" : "kimchy" }
|
|
|
|
},
|
2015-05-13 06:04:56 -04:00
|
|
|
"filter": {
|
|
|
|
"term" : { "tag" : "tech" }
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
"must_not" : {
|
|
|
|
"range" : {
|
|
|
|
"age" : { "from" : 10, "to" : 20 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"should" : [
|
|
|
|
{
|
|
|
|
"term" : { "tag" : "wow" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"term" : { "tag" : "elasticsearch" }
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"minimum_should_match" : 1,
|
|
|
|
"boost" : 1.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|