combined-fields-query.asciidoc 6.1 KB

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