bool-query.asciidoc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.
  11. |`should` |The clause (query) should appear in the matching document. In
  12. a boolean query with no `must` clauses, one or more `should` clauses
  13. must match a document. The minimum number of should clauses to match can
  14. be set using the
  15. <<query-dsl-minimum-should-match,`minimum_should_match`>>
  16. parameter.
  17. |`must_not` |The clause (query) must not appear in the matching
  18. documents.
  19. |=======================================================================
  20. IMPORTANT: If this query is used in a filter context and it has `should`
  21. clauses then at least one `should` clause is required to match.
  22. The bool query also supports `disable_coord` parameter (defaults to
  23. `false`). Basically the coord similarity computes a score factor based
  24. on the fraction of all query terms that a document contains. See Lucene
  25. `BooleanQuery` for more details.
  26. The `bool` query takes a _more-matches-is-better_ approach, so the score from
  27. each matching `must` or `should` clause will be added together to provide the
  28. final `_score` for each document.
  29. [source,js]
  30. --------------------------------------------------
  31. {
  32. "bool" : {
  33. "must" : {
  34. "term" : { "user" : "kimchy" }
  35. },
  36. "must_not" : {
  37. "range" : {
  38. "age" : { "from" : 10, "to" : 20 }
  39. }
  40. },
  41. "should" : [
  42. {
  43. "term" : { "tag" : "wow" }
  44. },
  45. {
  46. "term" : { "tag" : "elasticsearch" }
  47. }
  48. ],
  49. "minimum_should_match" : 1,
  50. "boost" : 1.0
  51. }
  52. }
  53. --------------------------------------------------