bool-query.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. [[query-dsl-bool-query]]
  2. === Boolean query
  3. ++++
  4. <titleabbrev>Boolean</titleabbrev>
  5. ++++
  6. A query that matches documents matching boolean combinations of other
  7. queries. The bool query maps to Lucene `BooleanQuery`. It is built using
  8. one or more boolean clauses, each clause with a typed occurrence. The
  9. occurrence types are:
  10. [cols="<,<",options="header",]
  11. |=======================================================================
  12. |Occur |Description
  13. |`must` |The clause (query) must appear in matching documents and will
  14. contribute to the score.
  15. |`filter` |The clause (query) must appear in matching documents. However unlike
  16. `must` the score of the query will be ignored. Filter clauses are executed
  17. in <<query-filter-context,filter context>>, meaning that scoring is ignored
  18. and clauses are considered for caching.
  19. |`should` |The clause (query) should appear in the matching document.
  20. |`must_not` |The clause (query) must not appear in the matching
  21. documents. Clauses are executed in <<query-filter-context,filter context>> meaning
  22. that scoring is ignored and clauses are considered for caching. Because scoring is
  23. ignored, a score of `0` for all documents is returned.
  24. |=======================================================================
  25. The `bool` query takes a _more-matches-is-better_ approach, so the score from
  26. each matching `must` or `should` clause will be added together to provide the
  27. final `_score` for each document.
  28. [source,console]
  29. --------------------------------------------------
  30. POST _search
  31. {
  32. "query": {
  33. "bool" : {
  34. "must" : {
  35. "term" : { "user.id" : "kimchy" }
  36. },
  37. "filter": {
  38. "term" : { "tags" : "production" }
  39. },
  40. "must_not" : {
  41. "range" : {
  42. "age" : { "gte" : 10, "lte" : 20 }
  43. }
  44. },
  45. "should" : [
  46. { "term" : { "tags" : "env1" } },
  47. { "term" : { "tags" : "deployed" } }
  48. ],
  49. "minimum_should_match" : 1,
  50. "boost" : 1.0
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. [[bool-min-should-match]]
  56. ==== Using `minimum_should_match`
  57. You can use the `minimum_should_match` parameter to specify the number or
  58. percentage of `should` clauses returned documents _must_ match.
  59. If the `bool` query includes at least one `should` clause and no `must` or
  60. `filter` clauses, the default value is `1`.
  61. Otherwise, the default value is `0`.
  62. For other valid values, see the
  63. <<query-dsl-minimum-should-match, `minimum_should_match` parameter>>.
  64. [[score-bool-filter]]
  65. ==== Scoring with `bool.filter`
  66. Queries specified under the `filter` element have no effect on scoring --
  67. scores are returned as `0`. Scores are only affected by the query that has
  68. been specified. For instance, all three of the following queries return
  69. all documents where the `status` field contains the term `active`.
  70. This first query assigns a score of `0` to all documents, as no scoring
  71. query has been specified:
  72. [source,console]
  73. ---------------------------------
  74. GET _search
  75. {
  76. "query": {
  77. "bool": {
  78. "filter": {
  79. "term": {
  80. "status": "active"
  81. }
  82. }
  83. }
  84. }
  85. }
  86. ---------------------------------
  87. This `bool` query has a `match_all` query, which assigns a score of `1.0` to
  88. all documents.
  89. [source,console]
  90. ---------------------------------
  91. GET _search
  92. {
  93. "query": {
  94. "bool": {
  95. "must": {
  96. "match_all": {}
  97. },
  98. "filter": {
  99. "term": {
  100. "status": "active"
  101. }
  102. }
  103. }
  104. }
  105. }
  106. ---------------------------------
  107. This `constant_score` query behaves in exactly the same way as the second example above.
  108. The `constant_score` query assigns a score of `1.0` to all documents matched
  109. by the filter.
  110. [source,console]
  111. ---------------------------------
  112. GET _search
  113. {
  114. "query": {
  115. "constant_score": {
  116. "filter": {
  117. "term": {
  118. "status": "active"
  119. }
  120. }
  121. }
  122. }
  123. }
  124. ---------------------------------
  125. [[named-queries]]
  126. ==== Named queries
  127. Each query accepts a `_name` in its top level definition. You can use named
  128. queries to track which queries matched returned documents. If named queries are
  129. used, the response includes a `matched_queries` property for each hit.
  130. [source,console]
  131. ----
  132. GET /_search
  133. {
  134. "query": {
  135. "bool": {
  136. "should": [
  137. { "match": { "name.first": { "query": "shay", "_name": "first" } } },
  138. { "match": { "name.last": { "query": "banon", "_name": "last" } } }
  139. ],
  140. "filter": {
  141. "terms": {
  142. "name.last": [ "banon", "kimchy" ],
  143. "_name": "test"
  144. }
  145. }
  146. }
  147. }
  148. }
  149. ----