2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-bool-query]]
|
2015-06-03 19:59:22 -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
|
2016-12-29 05:05:28 -05:00
|
|
|
`must` the score of the query will be ignored. Filter clauses are executed
|
|
|
|
in <<query-filter-context,filter context>>, meaning that scoring is ignored
|
|
|
|
and clauses are considered for caching.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|`should` |The clause (query) should appear in the matching document. In
|
2016-03-31 04:48:32 -04:00
|
|
|
a boolean query with no `must` or `filter` clauses, one or more `should` clauses
|
2013-08-28 19:24:34 -04:00
|
|
|
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
|
2017-01-10 11:26:14 -05:00
|
|
|
documents. Clauses are executed in <<query-filter-context,filter context>> meaning
|
|
|
|
that scoring is ignored and clauses are considered for caching. Because scoring is
|
|
|
|
ignored, a score of `0` for all documents is returned.
|
2013-08-28 19:24:34 -04:00
|
|
|
|=======================================================================
|
|
|
|
|
2015-06-10 15:46:23 -04:00
|
|
|
[IMPORTANT]
|
|
|
|
.Bool query in filter context
|
|
|
|
========================================================================
|
|
|
|
If this query is used in a filter context and it has `should`
|
2015-05-05 02:27:52 -04:00
|
|
|
clauses then at least one `should` clause is required to match.
|
2015-06-10 15:46:23 -04:00
|
|
|
========================================================================
|
2015-05-05 02:27:52 -04:00
|
|
|
|
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
|
2016-04-29 10:42:03 -04:00
|
|
|
each matching `must` or `should` clause will be added together to provide the
|
2014-11-24 10:07:18 -05:00
|
|
|
final `_score` for each document.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-04-29 10:42:03 -04:00
|
|
|
POST _search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-04-29 10:42:03 -04:00
|
|
|
"query": {
|
2013-08-28 19:24:34 -04:00
|
|
|
"bool" : {
|
2016-04-29 10:42:03 -04:00
|
|
|
"must" : {
|
|
|
|
"term" : { "user" : "kimchy" }
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"term" : { "tag" : "tech" }
|
|
|
|
},
|
|
|
|
"must_not" : {
|
|
|
|
"range" : {
|
|
|
|
"age" : { "from" : 10, "to" : 20 }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"should" : [
|
|
|
|
{ "term" : { "tag" : "wow" } },
|
|
|
|
{ "term" : { "tag" : "elasticsearch" } }
|
|
|
|
],
|
|
|
|
"minimum_should_match" : 1,
|
|
|
|
"boost" : 1.0
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2016-04-29 10:42:03 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-01-13 05:55:56 -05:00
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
==== Scoring with `bool.filter`
|
2016-01-13 05:55:56 -05:00
|
|
|
|
|
|
|
Queries specified under the `filter` element have no effect on scoring --
|
|
|
|
scores are returned as `0`. Scores are only affected by the query that has
|
2016-04-29 10:42:03 -04:00
|
|
|
been specified. For instance, all three of the following queries return
|
|
|
|
all documents where the `status` field contains the term `active`.
|
2016-01-13 05:55:56 -05:00
|
|
|
|
|
|
|
This first query assigns a score of `0` to all documents, as no scoring
|
2016-04-29 10:42:03 -04:00
|
|
|
query has been specified:
|
2016-01-13 05:55:56 -05:00
|
|
|
|
2016-05-24 05:58:43 -04:00
|
|
|
[source,js]
|
2016-01-13 05:55:56 -05:00
|
|
|
---------------------------------
|
|
|
|
GET _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"filter": {
|
|
|
|
"term": {
|
|
|
|
"status": "active"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
---------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-01-13 05:55:56 -05:00
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
This `bool` query has a `match_all` query, which assigns a score of `1.0` to
|
2016-01-13 05:55:56 -05:00
|
|
|
all documents.
|
|
|
|
|
2016-05-24 05:58:43 -04:00
|
|
|
[source,js]
|
2016-01-13 05:55:56 -05:00
|
|
|
---------------------------------
|
|
|
|
GET _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"bool": {
|
2016-04-29 10:42:03 -04:00
|
|
|
"must": {
|
2016-01-13 05:55:56 -05:00
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"filter": {
|
|
|
|
"term": {
|
|
|
|
"status": "active"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
---------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-01-13 05:55:56 -05:00
|
|
|
|
2016-04-29 10:42:03 -04:00
|
|
|
This `constant_score` query behaves in exactly the same way as the second example above.
|
2016-01-13 05:55:56 -05:00
|
|
|
The `constant_score` query assigns a score of `1.0` to all documents matched
|
2016-04-29 10:42:03 -04:00
|
|
|
by the filter.
|
2016-01-13 05:55:56 -05:00
|
|
|
|
2016-05-24 05:58:43 -04:00
|
|
|
[source,js]
|
2016-01-13 05:55:56 -05:00
|
|
|
---------------------------------
|
|
|
|
GET _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"constant_score": {
|
|
|
|
"filter": {
|
|
|
|
"term": {
|
|
|
|
"status": "active"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
---------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-03-22 15:07:57 -04:00
|
|
|
|
|
|
|
==== Using named queries to see which clauses matched
|
|
|
|
|
|
|
|
If you need to know which of the clauses in the bool query matched the documents
|
|
|
|
returned from the query, you can use
|
|
|
|
<<search-request-named-queries-and-filters,named queries>> to assign a name to
|
|
|
|
each clause.
|