combined-fields-query.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [[query-dsl-combined-fields-query]]
  2. === Combined fields
  3. ++++
  4. <titleabbrev>Combined fields</titleabbrev>
  5. ++++
  6. The `combined_fields` query supports searching multiple text fields as if their
  7. contents had been indexed into one combined field. The query takes a term-centric
  8. view of the input string: first it analyzes the query string into individual terms,
  9. then looks for each term in any of the fields. This query is particularly
  10. useful when a match could span multiple text fields, for example the `title`,
  11. `abstract`, and `body` of an article:
  12. [source,console]
  13. ----
  14. GET /_search
  15. {
  16. "query": {
  17. "combined_fields" : {
  18. "query": "database systems",
  19. "fields": [ "title", "abstract", "body"],
  20. "operator": "and"
  21. }
  22. }
  23. }
  24. ----
  25. The `combined_fields` query takes a principled approach to scoring based on the
  26. simple BM25F formula described in
  27. http://www.staff.city.ac.uk/~sb317/papers/foundations_bm25_review.pdf[The Probabilistic Relevance Framework: BM25 and Beyond].
  28. When scoring matches, the query combines term and collection statistics across
  29. fields to score each match as if the specified fields had been indexed into a
  30. single, combined field. This scoring is a best attempt; `combined_fields` makes
  31. some approximations and scores will not obey the BM25F model perfectly.
  32. // tag::max-clause-limit[]
  33. [WARNING]
  34. .Field number limit
  35. ===================================================
  36. By default, there is a limit to the number of clauses a query can contain. This
  37. limit is defined by the
  38. <<indices-query-bool-max-clause-count,`indices.query.bool.max_clause_count`>>
  39. setting, which defaults to `4096`. For `combined_fields` queries, the number of
  40. clauses is calculated as the number of fields multiplied by the number of terms.
  41. ===================================================
  42. // end::max-clause-limit[]
  43. ==== Per-field boosting
  44. Field boosts are interpreted according to the combined field model. For example,
  45. if the `title` field has a boost of 2, the score is calculated as if each term
  46. in the title appeared twice in the synthetic combined field.
  47. [source,console]
  48. ----
  49. GET /_search
  50. {
  51. "query": {
  52. "combined_fields" : {
  53. "query" : "distributed consensus",
  54. "fields" : [ "title^2", "body" ] <1>
  55. }
  56. }
  57. }
  58. ----
  59. <1> Individual fields can be boosted with the caret (`^`) notation.
  60. NOTE: The `combined_fields` query requires that field boosts are greater than
  61. or equal to 1.0. Field boosts are allowed to be fractional.
  62. [[combined-field-top-level-params]]
  63. ==== Top-level parameters for `combined_fields`
  64. `fields`::
  65. (Required, array of strings) List of fields to search. Field wildcard patterns
  66. are allowed. Only <<text,`text`>> fields are supported, and they must all have
  67. the same search <<analyzer,`analyzer`>>.
  68. `query`::
  69. +
  70. --
  71. (Required, string) Text to search for in the provided `<fields>`.
  72. The `combined_fields` query <<analysis,analyzes>> the provided text before
  73. performing a search.
  74. --
  75. `auto_generate_synonyms_phrase_query`::
  76. +
  77. --
  78. (Optional, Boolean) If `true`, <<query-dsl-match-query-phrase,match phrase>>
  79. queries are automatically created for multi-term synonyms. Defaults to `true`.
  80. See <<query-dsl-match-query-synonyms,Use synonyms with match query>> for an
  81. example.
  82. --
  83. `operator`::
  84. +
  85. --
  86. (Optional, string) Boolean logic used to interpret text in the `query` value.
  87. Valid values are:
  88. `or` (Default)::
  89. For example, a `query` value of `database systems` is interpreted as `database
  90. OR systems`.
  91. `and`::
  92. For example, a `query` value of `database systems` is interpreted as `database
  93. AND systems`.
  94. --
  95. `minimum_should_match`::
  96. +
  97. --
  98. (Optional, string) Minimum number of clauses that must match for a document to
  99. be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
  100. parameter>> for valid values and more information.
  101. --
  102. `zero_terms_query`::
  103. +
  104. --
  105. (Optional, string) Indicates whether no documents are returned if the `analyzer`
  106. removes all tokens, such as when using a `stop` filter. Valid values are:
  107. `none` (Default)::
  108. No documents are returned if the `analyzer` removes all tokens.
  109. `all`::
  110. Returns all documents, similar to a <<query-dsl-match-all-query,`match_all`>>
  111. query.
  112. See <<query-dsl-match-query-zero>> for an example.
  113. --
  114. ===== Comparison to `multi_match` query
  115. The `combined_fields` query provides a principled way of matching and scoring
  116. across multiple <<text, `text`>> fields. To support this, it requires that all
  117. fields have the same search <<analyzer,`analyzer`>>.
  118. If you want a single query that handles fields of different types like
  119. keywords or numbers, then the <<query-dsl-multi-match-query,`multi_match`>>
  120. query may be a better fit. It supports both text and non-text fields, and
  121. accepts text fields that do not share the same analyzer.
  122. The main `multi_match` modes `best_fields` and `most_fields` take a
  123. field-centric view of the query. In contrast, `combined_fields` is
  124. term-centric: `operator` and `minimum_should_match` are applied per-term,
  125. instead of per-field. Concretely, a query like
  126. [source,console]
  127. ----
  128. GET /_search
  129. {
  130. "query": {
  131. "combined_fields" : {
  132. "query": "database systems",
  133. "fields": [ "title", "abstract"],
  134. "operator": "and"
  135. }
  136. }
  137. }
  138. ----
  139. is executed as:
  140. [source,txt]
  141. ----
  142. +(combined("database", fields:["title" "abstract"]))
  143. +(combined("systems", fields:["title", "abstract"]))
  144. ----
  145. In other words, each term must be present in at least one field for a
  146. document to match.
  147. The `cross_fields` `multi_match` mode also takes a term-centric approach and
  148. applies `operator` and `minimum_should_match per-term`. The main advantage of
  149. `combined_fields` over `cross_fields` is its robust and interpretable approach
  150. to scoring based on the BM25F algorithm.
  151. [NOTE]
  152. .Custom similarities
  153. ===================================================
  154. The `combined_fields` query currently only supports the BM25 similarity,
  155. which is the default unless a <<index-modules-similarity, custom similarity>>
  156. is configured. <<similarity, Per-field similarities>> are also not allowed.
  157. Using `combined_fields` in either of these cases will result in an error.
  158. ===================================================