multi-match-query.asciidoc 17 KB

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