multi-match-query.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. [[query-dsl-multi-match-query]]
  2. === Multi Match Query
  3. The `multi_match` query builds on the <<query-dsl-match-query,`match` query>>
  4. to allow multi-field queries:
  5. [source,js]
  6. --------------------------------------------------
  7. {
  8. "multi_match" : {
  9. "query": "this is a test", <1>
  10. "fields": [ "subject", "message" ] <2>
  11. }
  12. }
  13. --------------------------------------------------
  14. <1> The query string.
  15. <2> The fields to be queried.
  16. [float]
  17. === `fields` and per-field boosting
  18. Fields can be specified with wildcards, eg:
  19. [source,js]
  20. --------------------------------------------------
  21. {
  22. "multi_match" : {
  23. "query": "Will Smith",
  24. "fields": [ "title", "*_name" ] <1>
  25. }
  26. }
  27. --------------------------------------------------
  28. <1> Query the `title`, `first_name` and `last_name` fields.
  29. Individual fields can be boosted with the caret (`^`) notation:
  30. [source,js]
  31. --------------------------------------------------
  32. {
  33. "multi_match" : {
  34. "query" : "this is a test",
  35. "fields" : [ "subject^3", "message" ] <1>
  36. }
  37. }
  38. --------------------------------------------------
  39. <1> The `subject` field is three times as important as the `message` field.
  40. [float]
  41. === `use_dis_max`
  42. deprecated[1.1.0,Use `type:best_fields` or `type:most_fields` instead. See <<multi-match-types>>]
  43. By default, the `multi_match` query generates a `match` clause per field, then wraps them
  44. in a `dis_max` query. By setting `use_dis_max` to `false`, they will be wrapped in a
  45. `bool` query instead.
  46. [[multi-match-types]]
  47. [float]
  48. === Types of `multi_match` query:
  49. added[1.1.0]
  50. The way the `multi_match` query is executed internally depends on the `type`
  51. parameter, which can be set to:
  52. [horizontal]
  53. `best_fields`:: (*default*) Finds documents which match any field, but
  54. uses the `_score` from the best field. See <<type-best-fields>>.
  55. `most_fields`:: Finds documents which match any field and combines
  56. the `_score` from each field. See <<type-most-fields>>.
  57. `cross_fields`:: Treats fields with the same `analyzer` as though they
  58. were one big field. Looks for each word in *any*
  59. field. See <<type-cross-fields>>.
  60. `phrase`:: Runs a `match_phrase` query on each field and combines
  61. the `_score` from each field. See <<type-phrase>>.
  62. `phrase_prefix`:: Runs a `match_phrase_prefix` query on each field and
  63. combines the `_score` from each field. See <<type-phrase>>.
  64. [[type-best-fields]]
  65. ==== `best_fields`
  66. The `best_fields` type is most useful when you are searching for multiple
  67. words best found in the same field. For instance ``brown fox'' in a single
  68. field is more meaningful than ``brown'' in one field and ``fox'' in the other.
  69. The `best_fields` type generates a <<query-dsl-match-query,`match` query>> for
  70. each field and wraps them in a <<query-dsl-dis-max-query,`dis_max`>> query, to
  71. find the single best matching field. For instance, this query:
  72. [source,js]
  73. --------------------------------------------------
  74. {
  75. "multi_match" : {
  76. "query": "brown fox",
  77. "type": "best_fields",
  78. "fields": [ "subject", "message" ],
  79. "tie_breaker": 0.3
  80. }
  81. }
  82. --------------------------------------------------
  83. would be executed as:
  84. [source,js]
  85. --------------------------------------------------
  86. {
  87. "dis_max": {
  88. "queries": [
  89. { "match": { "subject": "brown fox" }},
  90. { "match": { "message": "brown fox" }}
  91. ],
  92. "tie_breaker": 0.3
  93. }
  94. }
  95. --------------------------------------------------
  96. Normally the `best_fields` type uses the score of the *single* best matching
  97. field, but if `tie_breaker` is specified, then it calculates the score as
  98. follows:
  99. * the score from the best matching field
  100. * plus `tie_breaker * _score` for all other matching fields
  101. Also, accepts `analyzer`, `boost`, `operator`, `minimum_should_match`,
  102. `fuzziness`, `prefix_length`, `max_expansions`, `rewrite`, `zero_terms_query`
  103. and `cutoff_frequency`, as explained in <<query-dsl-match-query, match query>>.
  104. [IMPORTANT]
  105. [[operator-min]]
  106. .`operator` and `minimum_should_match`
  107. ==================================================
  108. The `best_fields` and `most_fields` types are _field-centric_ -- they generate
  109. a `match` query *per field*. This means that the `operator` and
  110. `minimum_should_match` parameters are applied to each field individually,
  111. which is probably not what you want.
  112. Take this query for example:
  113. [source,js]
  114. --------------------------------------------------
  115. {
  116. "multi_match" : {
  117. "query": "Will Smith",
  118. "type": "best_fields",
  119. "fields": [ "first_name", "last_name" ],
  120. "operator": "and" <1>
  121. }
  122. }
  123. --------------------------------------------------
  124. <1> All terms must be present.
  125. This query is executed as:
  126. (+first_name:will +first_name:smith)
  127. | (+last_name:will +last_name:smith)
  128. In other words, *all terms* must be present *in a single field* for a document
  129. to match.
  130. See <<type-cross-fields>> for a better solution.
  131. ==================================================
  132. [[type-most-fields]]
  133. ==== `most_fields`
  134. The `most_fields` type is most useful when querying multiple fields that
  135. contain the same text analyzed in different ways. For instance, the main
  136. field may contain synonyms, stemming and terms without diacritics. A second
  137. field may contain the original terms, and a third field might contain
  138. shingles. By combining scores from all three fields we can match as many
  139. documents as possible with the main field, but use the second and third fields
  140. to push the most similar results to the top of the list.
  141. This query:
  142. [source,js]
  143. --------------------------------------------------
  144. {
  145. "multi_match" : {
  146. "query": "quick brown fox",
  147. "type": "most_fields",
  148. "fields": [ "title", "title.original", "title.shingles" ]
  149. }
  150. }
  151. --------------------------------------------------
  152. would be executed as:
  153. [source,js]
  154. --------------------------------------------------
  155. {
  156. "bool": {
  157. "should": [
  158. { "match": { "title": "quick brown fox" }},
  159. { "match": { "title.original": "quick brown fox" }},
  160. { "match": { "title.shingles": "quick brown fox" }}
  161. ]
  162. }
  163. }
  164. --------------------------------------------------
  165. The score from each `match` clause is added together, then divided by the
  166. number of `match` clauses.
  167. Also, accepts `analyzer`, `boost`, `operator`, `minimum_should_match`,
  168. `fuzziness`, `prefix_length`, `max_expansions`, `rewrite`, `zero_terms_query`
  169. and `cutoff_frequency`, as explained in <<query-dsl-match-query,match query>>, but
  170. *see <<operator-min>>*.
  171. [[type-phrase]]
  172. ==== `phrase` and `phrase_prefix`
  173. The `phrase` and `phrase_prefix` types behave just like <<type-best-fields>>,
  174. but they use a `match_phrase` or `match_phrase_prefix` query instead of a
  175. `match` query.
  176. This query:
  177. [source,js]
  178. --------------------------------------------------
  179. {
  180. "multi_match" : {
  181. "query": "quick brown f",
  182. "type": "phrase_prefix",
  183. "fields": [ "subject", "message" ]
  184. }
  185. }
  186. --------------------------------------------------
  187. [source,js]
  188. --------------------------------------------------
  189. {
  190. "dis_max": {
  191. "queries": [
  192. { "match_phrase_prefix": { "subject": "quick brown f" }},
  193. { "match_phrase_prefix": { "message": "quick brown f" }}
  194. ]
  195. }
  196. }
  197. --------------------------------------------------
  198. Also, accepts `analyzer`, `boost`, `slop` and `zero_terms_query` as explained
  199. in <<query-dsl-match-query>>. Type `phrase_prefix` additionally accepts
  200. `max_expansions`.
  201. [[type-cross-fields]]
  202. ==== `cross_fields`
  203. The `cross_fields` type is particularly useful with structured documents where
  204. multiple fields *should* match. For instance, when querying the `first_name`
  205. and `last_name` fields for ``Will Smith'', the best match is likely to have
  206. ``Will'' in one field and ``Smith'' in the other.
  207. ****
  208. This sounds like a job for <<type-most-fields>> but there are two problems
  209. with that approach. The first problem is that `operator` and
  210. `minimum_should_match` are applied per-field, instead of per-term (see
  211. <<operator-min,explanation above>>).
  212. The second problem is to do with relevance: the different term frequencies in
  213. the `first_name` and `last_name` fields can produce unexpected results.
  214. For instance, imagine we have two people: ``Will Smith'' and ``Smith Jones''.
  215. ``Smith'' as a last name is very common (and so is of low importance) but
  216. ``Smith'' as a first name is very uncommon (and so is of great importance).
  217. If we do a search for ``Will Smith'', the ``Smith Jones'' document will
  218. probably appear above the better matching ``Will Smith'' because the score of
  219. `first_name:smith` has trumped the combined scores of `first_name:will` plus
  220. `last_name:smith`.
  221. ****
  222. One way of dealing with these types of queries is simply to index the
  223. `first_name` and `last_name` fields into a single `full_name` field. Of
  224. course, this can only be done at index time.
  225. The `cross_field` type tries to solve these problems at query time by taking a
  226. _term-centric_ approach. It first analyzes the query string into individual
  227. terms, then looks for each term in any of the fields, as though they were one
  228. big field.
  229. A query like:
  230. [source,js]
  231. --------------------------------------------------
  232. {
  233. "multi_match" : {
  234. "query": "Will Smith",
  235. "type": "cross_fields",
  236. "fields": [ "first_name", "last_name" ],
  237. "operator": "and"
  238. }
  239. }
  240. --------------------------------------------------
  241. is executed as:
  242. +(first_name:will last_name:will)
  243. +(first_name:smith last_name:smith)
  244. In other words, *all terms* must be present *in at least one field* for a
  245. document to match. (Compare this to
  246. <<operator-min,the logic used for `best_fields` and `most_fields`>>.)
  247. That solves one of the two problems. The problem of differing term frequencies
  248. is solved by _blending_ the term frequencies for all fields in order to even
  249. out the differences. In other words, `first_name:smith` will be treated as
  250. though it has the same weight as `last_name:smith`. (Actually,
  251. `first_name:smith` is given a tiny advantage over `last_name:smith`, just to
  252. make the order of results more stable.)
  253. If you run the above query through the <<search-validate>>, it returns this
  254. explanation:
  255. +blended("will", fields: [first_name, last_name])
  256. +blended("smith", fields: [first_name, last_name])
  257. Also, accepts `analyzer`, `boost`, `operator`, `minimum_should_match`,
  258. `zero_terms_query` and `cutoff_frequency`, as explained in
  259. <<query-dsl-match-query, match query>>.
  260. ===== `cross_field` and analysis
  261. The `cross_field` type can only work in term-centric mode on fields that have
  262. the same analyzer. Fields with the same analyzer are grouped together as in
  263. the example above. If there are multiple groups, they are combined with a
  264. `bool` query.
  265. For instance, if we have a `first` and `last` field which have
  266. the same analyzer, plus a `first.edge` and `last.edge` which
  267. both use an `edge_ngram` analyzer, this query:
  268. [source,js]
  269. --------------------------------------------------
  270. {
  271. "multi_match" : {
  272. "query": "Jon",
  273. "type": "cross_fields",
  274. "fields": [
  275. "first", "first.edge",
  276. "last", "last.edge"
  277. ]
  278. }
  279. }
  280. --------------------------------------------------
  281. would be executed as:
  282. blended("jon", fields: [first, last])
  283. | (
  284. blended("j", fields: [first.edge, last.edge])
  285. blended("jo", fields: [first.edge, last.edge])
  286. blended("jon", fields: [first.edge, last.edge])
  287. )
  288. In other words, `first` and `last` would be grouped together and
  289. treated as a single field, and `first.edge` and `last.edge` would be
  290. grouped together and treated as a single field.
  291. Having multiple groups is fine, but when combined with `operator` or
  292. `minimum_should_match`, it can suffer from the <<operator-min,same problem>>
  293. as `most_fields` or `best_fields`.
  294. You can easily rewrite this query yourself as two separate `cross_type`
  295. queries combined with a `bool` query, and apply the `minimum_should_match`
  296. parameter to just one of them:
  297. [source,js]
  298. --------------------------------------------------
  299. {
  300. "bool": {
  301. "should": [
  302. {
  303. "multi_match" : {
  304. "query": "Will Smith",
  305. "type": "cross_fields",
  306. "fields": [ "first", "last" ],
  307. "minimum_should_match": "50%" <1>
  308. }
  309. },
  310. {
  311. "multi_match" : {
  312. "query": "Will Smith",
  313. "type": "cross_fields",
  314. "fields": [ "*.edge" ]
  315. }
  316. }
  317. ]
  318. }
  319. }
  320. --------------------------------------------------
  321. <1> Either `will` or `smith` must be present in either of the `first`
  322. or `last` fields
  323. You can force all fields into the same group by specifying the `analyzer`
  324. parameter in the query.
  325. [source,js]
  326. --------------------------------------------------
  327. {
  328. "multi_match" : {
  329. "query": "Jon",
  330. "type": "cross_fields",
  331. "analyzer": "standard", <1>
  332. "fields": [ "first", "last", "*.edge" ]
  333. }
  334. }
  335. --------------------------------------------------
  336. <1> Use the `standard` analyzer for all fields.
  337. which will be executed as:
  338. blended("will", fields: [first, first.edge, last.edge, last])
  339. blended("smith", fields: [first, first.edge, last.edge, last])
  340. ===== `tie_breaker`
  341. By default, each per-term `blended` query will use the best score returned by
  342. any field in a group, then these scores are added together to give the final
  343. score. The `tie_breaker` parameter can change the default behaviour of the
  344. per-term `blended` queries. It accepts:
  345. [horizontal]
  346. `0.0`:: Take the single best score out of (eg) `first_name:will`
  347. and `last_name:will` (*default*)
  348. `1.0`:: Add together the scores for (eg) `first_name:will` and
  349. `last_name:will`
  350. `0.0 < n < 1.0`:: Take the single best score plus +tie_breaker+ multiplied
  351. by each of the scores from other matching fields.