multi-match-query.asciidoc 17 KB

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