| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | [[query-dsl-bool-query]]=== Bool QueryA query that matches documents matching boolean combinations of otherqueries. The bool query maps to Lucene `BooleanQuery`. It is built usingone or more boolean clauses, each clause with a typed occurrence. Theoccurrence types are:[cols="<,<",options="header",]|=======================================================================|Occur |Description|`must` |The clause (query) must appear in matching documents and willcontribute to the score.|`filter` |The clause (query) must appear in matching documents. However unlike`must` the score of the query will be ignored. Filter clauses are executedin <<query-filter-context,filter context>>, meaning that scoring is ignoredand clauses are considered for caching.|`should` |The clause (query) should appear in the matching document. If the`bool` query is in a <<query-filter-context,query context>> and has a `must` or`filter` clause then a document will match the `bool` query even if none of the`should` queries match. In this case these clauses are only used to influencethe score. If the `bool` query is a <<query-filter-context,filter context>>or has neither `must` or `filter` then at least one of the `should` queriesmust match a document for it to match the `bool` query. This behavior may beexplicitly controlled by settings the<<query-dsl-minimum-should-match,`minimum_should_match`>> parameter.|`must_not` |The clause (query) must not appear in the matchingdocuments.  Clauses are executed in <<query-filter-context,filter context>> meaningthat scoring is ignored and clauses are considered for caching. Because scoring isignored, a score of `0` for all documents is returned.|=======================================================================[IMPORTANT].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 takes a _more-matches-is-better_ approach, so the score fromeach matching `must` or `should` clause will be added together to provide thefinal `_score` for each document.[source,js]--------------------------------------------------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    }  }}--------------------------------------------------// CONSOLE==== Scoring with `bool.filter`Queries specified under the `filter` element have no effect on scoring --scores are returned as `0`.  Scores are only affected by the query that hasbeen specified.  For instance, all three of the following queries returnall documents where the `status` field contains the term `active`.This first query assigns a score of `0` to all documents, as no scoringquery has been specified:[source,js]---------------------------------GET _search{  "query": {    "bool": {      "filter": {        "term": {          "status": "active"        }      }    }  }}---------------------------------// CONSOLEThis `bool` query has a `match_all` query, which assigns a score of `1.0` toall documents.[source,js]---------------------------------GET _search{  "query": {    "bool": {      "must": {        "match_all": {}      },      "filter": {        "term": {          "status": "active"        }      }    }  }}---------------------------------// CONSOLEThis `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 matchedby the filter.[source,js]---------------------------------GET _search{  "query": {    "constant_score": {      "filter": {        "term": {          "status": "active"        }      }    }  }}---------------------------------// CONSOLE==== Using named queries to see which clauses matchedIf you need to know which of the clauses in the bool query matched the documentsreturned from the query, you can use<<search-request-named-queries-and-filters,named queries>> to assign a name toeach clause.
 |