Boolean query

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:

OccurDescription

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. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

should

The clause (query) should appear in the matching document.

must_not

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 0 for all documents is returned.

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.

  1. POST _search
  2. {
  3. "query": {
  4. "bool" : {
  5. "must" : {
  6. "term" : { "user.id" : "kimchy" }
  7. },
  8. "filter": {
  9. "term" : { "tags" : "production" }
  10. },
  11. "must_not" : {
  12. "range" : {
  13. "age" : { "gte" : 10, "lte" : 20 }
  14. }
  15. },
  16. "should" : [
  17. { "term" : { "tags" : "env1" } },
  18. { "term" : { "tags" : "deployed" } }
  19. ],
  20. "minimum_should_match" : 1,
  21. "boost" : 1.0
  22. }
  23. }
  24. }

Using minimum_should_match

You can use the minimum_should_match parameter to specify the number or percentage of should clauses returned documents must match.

If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.

For other valid values, see the minimum_should_match parameter.

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 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:

  1. GET _search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "term": {
  7. "status": "active"
  8. }
  9. }
  10. }
  11. }
  12. }

This bool query has a match_all query, which assigns a score of 1.0 to all documents.

  1. GET _search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "term": {
  10. "status": "active"
  11. }
  12. }
  13. }
  14. }
  15. }

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.

  1. GET _search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "term": {
  7. "status": "active"
  8. }
  9. }
  10. }
  11. }
  12. }

Named queries

Each query accepts a _name in its top level definition. You can use named queries to track which queries matched returned documents. If named queries are used, the response includes a matched_queries property for each hit.

  1. GET /_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. { "match": { "name.first": { "query": "shay", "_name": "first" } } },
  7. { "match": { "name.last": { "query": "banon", "_name": "last" } } }
  8. ],
  9. "filter": {
  10. "terms": {
  11. "name.last": [ "banon", "kimchy" ],
  12. "_name": "test"
  13. }
  14. }
  15. }
  16. }
  17. }