bool-query.asciidoc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. [[query-dsl-bool-query]]
  2. === Bool Query
  3. A query that matches documents matching boolean combinations of other
  4. queries. The bool query maps to Lucene `BooleanQuery`. It is built using
  5. one or more boolean clauses, each clause with a typed occurrence. The
  6. occurrence types are:
  7. [cols="<,<",options="header",]
  8. |=======================================================================
  9. |Occur |Description
  10. |`must` |The clause (query) must appear in matching documents and will
  11. contribute to the score.
  12. |`filter` |The clause (query) must appear in matching documents. However unlike
  13. `must` the score of the query will be ignored.
  14. |`should` |The clause (query) should appear in the matching document. In
  15. a boolean query with no `must` clauses, one or more `should` clauses
  16. must match a document. The minimum number of should clauses to match can
  17. be set using the
  18. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  19. parameter.
  20. |`must_not` |The clause (query) must not appear in the matching
  21. documents.
  22. |=======================================================================
  23. [IMPORTANT]
  24. .Bool query in filter context
  25. ========================================================================
  26. If this query is used in a filter context and it has `should`
  27. clauses then at least one `should` clause is required to match.
  28. ========================================================================
  29. The bool query also supports `disable_coord` parameter (defaults to
  30. `false`). Basically the coord similarity computes a score factor based
  31. on the fraction of all query terms that a document contains. See Lucene
  32. `BooleanQuery` for more details.
  33. The `bool` query takes a _more-matches-is-better_ approach, so the score from
  34. each matching `must` or `should` clause will be added together to provide the
  35. final `_score` for each document.
  36. [source,js]
  37. --------------------------------------------------
  38. {
  39. "bool" : {
  40. "must" : {
  41. "term" : { "user" : "kimchy" }
  42. },
  43. "filter": {
  44. "term" : { "tag" : "tech" }
  45. },
  46. "must_not" : {
  47. "range" : {
  48. "age" : { "from" : 10, "to" : 20 }
  49. }
  50. },
  51. "should" : [
  52. {
  53. "term" : { "tag" : "wow" }
  54. },
  55. {
  56. "term" : { "tag" : "elasticsearch" }
  57. }
  58. ],
  59. "minimum_should_match" : 1,
  60. "boost" : 1.0
  61. }
  62. }
  63. --------------------------------------------------