multi-match-query.asciidoc 15 KB

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