multi-match-query.asciidoc 15 KB

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