percolate-query.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. [[query-dsl-percolate-query]]
  2. === Percolate query
  3. ++++
  4. <titleabbrev>Percolate</titleabbrev>
  5. ++++
  6. The `percolate` query can be used to match queries
  7. stored in an index. The `percolate` query itself
  8. contains the document that will be used as query
  9. to match with the stored queries.
  10. [discrete]
  11. === Sample Usage
  12. Create an index with two fields:
  13. [source,console]
  14. --------------------------------------------------
  15. PUT /my-index-000001
  16. {
  17. "mappings": {
  18. "properties": {
  19. "message": {
  20. "type": "text"
  21. },
  22. "query": {
  23. "type": "percolator"
  24. }
  25. }
  26. }
  27. }
  28. --------------------------------------------------
  29. The `message` field is the field used to preprocess the document defined in
  30. the `percolator` query before it gets indexed into a temporary index.
  31. The `query` field is used for indexing the query documents. It will hold a
  32. json object that represents an actual Elasticsearch query. The `query` field
  33. has been configured to use the <<percolator,percolator field type>>. This field
  34. type understands the query dsl and stores the query in such a way that it can be
  35. used later on to match documents defined on the `percolate` query.
  36. Register a query in the percolator:
  37. [source,console]
  38. --------------------------------------------------
  39. PUT /my-index-000001/_doc/1?refresh
  40. {
  41. "query": {
  42. "match": {
  43. "message": "bonsai tree"
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. // TEST[continued]
  49. Match a document to the registered percolator queries:
  50. [source,console]
  51. --------------------------------------------------
  52. GET /my-index-000001/_search
  53. {
  54. "query": {
  55. "percolate": {
  56. "field": "query",
  57. "document": {
  58. "message": "A new bonsai tree in the office"
  59. }
  60. }
  61. }
  62. }
  63. --------------------------------------------------
  64. // TEST[continued]
  65. The above request will yield the following response:
  66. [source,console-result]
  67. --------------------------------------------------
  68. {
  69. "took": 13,
  70. "timed_out": false,
  71. "_shards": {
  72. "total": 1,
  73. "successful": 1,
  74. "skipped" : 0,
  75. "failed": 0
  76. },
  77. "hits": {
  78. "total" : {
  79. "value": 1,
  80. "relation": "eq"
  81. },
  82. "max_score": 0.26152915,
  83. "hits": [
  84. { <1>
  85. "_index": "my-index-000001",
  86. "_id": "1",
  87. "_score": 0.26152915,
  88. "_source": {
  89. "query": {
  90. "match": {
  91. "message": "bonsai tree"
  92. }
  93. }
  94. },
  95. "fields" : {
  96. "_percolator_document_slot" : [0] <2>
  97. }
  98. }
  99. ]
  100. }
  101. }
  102. --------------------------------------------------
  103. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  104. <1> The query with id `1` matches our document.
  105. <2> The `_percolator_document_slot` field indicates which document has matched with this query.
  106. Useful when percolating multiple document simultaneously.
  107. TIP: To provide a simple example, this documentation uses one index `my-index-000001` for both the percolate queries and documents.
  108. This set-up can work well when there are just a few percolate queries registered. However, with heavier usage it is recommended
  109. to store queries and documents in separate indices. Please see <<how-it-works, How it Works Under the Hood>> for more details.
  110. [discrete]
  111. ==== Parameters
  112. The following parameters are required when percolating a document:
  113. [horizontal]
  114. `field`:: The field of type `percolator` that holds the indexed queries. This is a required parameter.
  115. `name`:: The suffix to be used for the `_percolator_document_slot` field in case multiple `percolate` queries have been specified.
  116. This is an optional parameter.
  117. `document`:: The source of the document being percolated.
  118. `documents`:: Like the `document` parameter, but accepts multiple documents via a json array.
  119. `document_type`:: The type / mapping of the document being percolated. This parameter is deprecated and will be removed in Elasticsearch 8.0.
  120. Instead of specifying the source of the document being percolated, the source can also be retrieved from an already
  121. stored document. The `percolate` query will then internally execute a get request to fetch that document.
  122. In that case the `document` parameter can be substituted with the following parameters:
  123. [horizontal]
  124. `index`:: The index the document resides in. This is a required parameter.
  125. `type`:: The type of the document to fetch. This parameter is deprecated and will be removed in Elasticsearch 8.0.
  126. `id`:: The id of the document to fetch. This is a required parameter.
  127. `routing`:: Optionally, routing to be used to fetch document to percolate.
  128. `preference`:: Optionally, preference to be used to fetch document to percolate.
  129. `version`:: Optionally, the expected version of the document to be fetched.
  130. [discrete]
  131. ==== Percolating in a filter context
  132. In case you are not interested in the score, better performance can be expected by wrapping
  133. the percolator query in a `bool` query's filter clause or in a `constant_score` query:
  134. [source,console]
  135. --------------------------------------------------
  136. GET /my-index-000001/_search
  137. {
  138. "query": {
  139. "constant_score": {
  140. "filter": {
  141. "percolate": {
  142. "field": "query",
  143. "document": {
  144. "message": "A new bonsai tree in the office"
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. --------------------------------------------------
  152. // TEST[continued]
  153. At index time terms are extracted from the percolator query and the percolator
  154. can often determine whether a query matches just by looking at those extracted
  155. terms. However, computing scores requires to deserialize each matching query
  156. and run it against the percolated document, which is a much more expensive
  157. operation. Hence if computing scores is not required the `percolate` query
  158. should be wrapped in a `constant_score` query or a `bool` query's filter clause.
  159. Note that the `percolate` query never gets cached by the query cache.
  160. [discrete]
  161. ==== Percolating multiple documents
  162. The `percolate` query can match multiple documents simultaneously with the indexed percolator queries.
  163. Percolating multiple documents in a single request can improve performance as queries only need to be parsed and
  164. matched once instead of multiple times.
  165. The `_percolator_document_slot` field that is being returned with each matched percolator query is important when percolating
  166. multiple documents simultaneously. It indicates which documents matched with a particular percolator query. The numbers
  167. correlate with the slot in the `documents` array specified in the `percolate` query.
  168. [source,console]
  169. --------------------------------------------------
  170. GET /my-index-000001/_search
  171. {
  172. "query": {
  173. "percolate": {
  174. "field": "query",
  175. "documents": [ <1>
  176. {
  177. "message": "bonsai tree"
  178. },
  179. {
  180. "message": "new tree"
  181. },
  182. {
  183. "message": "the office"
  184. },
  185. {
  186. "message": "office tree"
  187. }
  188. ]
  189. }
  190. }
  191. }
  192. --------------------------------------------------
  193. // TEST[continued]
  194. <1> The documents array contains 4 documents that are going to be percolated at the same time.
  195. [source,console-result]
  196. --------------------------------------------------
  197. {
  198. "took": 13,
  199. "timed_out": false,
  200. "_shards": {
  201. "total": 1,
  202. "successful": 1,
  203. "skipped" : 0,
  204. "failed": 0
  205. },
  206. "hits": {
  207. "total" : {
  208. "value": 1,
  209. "relation": "eq"
  210. },
  211. "max_score": 0.7093853,
  212. "hits": [
  213. {
  214. "_index": "my-index-000001",
  215. "_id": "1",
  216. "_score": 0.7093853,
  217. "_source": {
  218. "query": {
  219. "match": {
  220. "message": "bonsai tree"
  221. }
  222. }
  223. },
  224. "fields" : {
  225. "_percolator_document_slot" : [0, 1, 3] <1>
  226. }
  227. }
  228. ]
  229. }
  230. }
  231. --------------------------------------------------
  232. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  233. <1> The `_percolator_document_slot` indicates that the first, second and last documents specified in the `percolate` query
  234. are matching with this query.
  235. [discrete]
  236. ==== Percolating an Existing Document
  237. In order to percolate a newly indexed document, the `percolate` query can be used. Based on the response
  238. from an index request, the `_id` and other meta information can be used to immediately percolate the newly added
  239. document.
  240. [discrete]
  241. ===== Example
  242. Based on the previous example.
  243. Index the document we want to percolate:
  244. [source,console]
  245. --------------------------------------------------
  246. PUT /my-index-000001/_doc/2
  247. {
  248. "message" : "A new bonsai tree in the office"
  249. }
  250. --------------------------------------------------
  251. // TEST[continued]
  252. Index response:
  253. [source,console-result]
  254. --------------------------------------------------
  255. {
  256. "_index": "my-index-000001",
  257. "_id": "2",
  258. "_version": 1,
  259. "_shards": {
  260. "total": 2,
  261. "successful": 1,
  262. "failed": 0
  263. },
  264. "result": "created",
  265. "_seq_no" : 1,
  266. "_primary_term" : 1
  267. }
  268. --------------------------------------------------
  269. Percolating an existing document, using the index response as basis to build to new search request:
  270. [source,console]
  271. --------------------------------------------------
  272. GET /my-index-000001/_search
  273. {
  274. "query": {
  275. "percolate": {
  276. "field": "query",
  277. "index": "my-index-000001",
  278. "id": "2",
  279. "version": 1 <1>
  280. }
  281. }
  282. }
  283. --------------------------------------------------
  284. // TEST[continued]
  285. <1> The version is optional, but useful in certain cases. We can ensure that we are trying to percolate
  286. the document we just have indexed. A change may be made after we have indexed, and if that is the
  287. case the search request would fail with a version conflict error.
  288. The search response returned is identical as in the previous example.
  289. [discrete]
  290. ==== Percolate query and highlighting
  291. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  292. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  293. the search request is used to highlight the hits.
  294. [discrete]
  295. ===== Example
  296. This example is based on the mapping of the first example.
  297. Save a query:
  298. [source,console]
  299. --------------------------------------------------
  300. PUT /my-index-000001/_doc/3?refresh
  301. {
  302. "query": {
  303. "match": {
  304. "message": "brown fox"
  305. }
  306. }
  307. }
  308. --------------------------------------------------
  309. // TEST[continued]
  310. Save another query:
  311. [source,console]
  312. --------------------------------------------------
  313. PUT /my-index-000001/_doc/4?refresh
  314. {
  315. "query": {
  316. "match": {
  317. "message": "lazy dog"
  318. }
  319. }
  320. }
  321. --------------------------------------------------
  322. // TEST[continued]
  323. Execute a search request with the `percolate` query and highlighting enabled:
  324. [source,console]
  325. --------------------------------------------------
  326. GET /my-index-000001/_search
  327. {
  328. "query": {
  329. "percolate": {
  330. "field": "query",
  331. "document": {
  332. "message": "The quick brown fox jumps over the lazy dog"
  333. }
  334. }
  335. },
  336. "highlight": {
  337. "fields": {
  338. "message": {}
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. // TEST[continued]
  344. This will yield the following response.
  345. [source,console-result]
  346. --------------------------------------------------
  347. {
  348. "took": 7,
  349. "timed_out": false,
  350. "_shards": {
  351. "total": 1,
  352. "successful": 1,
  353. "skipped" : 0,
  354. "failed": 0
  355. },
  356. "hits": {
  357. "total" : {
  358. "value": 2,
  359. "relation": "eq"
  360. },
  361. "max_score": 0.26152915,
  362. "hits": [
  363. {
  364. "_index": "my-index-000001",
  365. "_id": "3",
  366. "_score": 0.26152915,
  367. "_source": {
  368. "query": {
  369. "match": {
  370. "message": "brown fox"
  371. }
  372. }
  373. },
  374. "highlight": {
  375. "message": [
  376. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  377. ]
  378. },
  379. "fields" : {
  380. "_percolator_document_slot" : [0]
  381. }
  382. },
  383. {
  384. "_index": "my-index-000001",
  385. "_id": "4",
  386. "_score": 0.26152915,
  387. "_source": {
  388. "query": {
  389. "match": {
  390. "message": "lazy dog"
  391. }
  392. }
  393. },
  394. "highlight": {
  395. "message": [
  396. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  397. ]
  398. },
  399. "fields" : {
  400. "_percolator_document_slot" : [0]
  401. }
  402. }
  403. ]
  404. }
  405. }
  406. --------------------------------------------------
  407. // TESTRESPONSE[s/"took": 7,/"took": "$body.took",/]
  408. <1> The terms from each query have been highlighted in the document.
  409. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  410. the document defined in the `percolate` query.
  411. When percolating multiple documents at the same time like the request below then the highlight response is different:
  412. [source,console]
  413. --------------------------------------------------
  414. GET /my-index-000001/_search
  415. {
  416. "query": {
  417. "percolate": {
  418. "field": "query",
  419. "documents": [
  420. {
  421. "message": "bonsai tree"
  422. },
  423. {
  424. "message": "new tree"
  425. },
  426. {
  427. "message": "the office"
  428. },
  429. {
  430. "message": "office tree"
  431. }
  432. ]
  433. }
  434. },
  435. "highlight": {
  436. "fields": {
  437. "message": {}
  438. }
  439. }
  440. }
  441. --------------------------------------------------
  442. // TEST[continued]
  443. The slightly different response:
  444. [source,console-result]
  445. --------------------------------------------------
  446. {
  447. "took": 13,
  448. "timed_out": false,
  449. "_shards": {
  450. "total": 1,
  451. "successful": 1,
  452. "skipped" : 0,
  453. "failed": 0
  454. },
  455. "hits": {
  456. "total" : {
  457. "value": 1,
  458. "relation": "eq"
  459. },
  460. "max_score": 0.7093853,
  461. "hits": [
  462. {
  463. "_index": "my-index-000001",
  464. "_id": "1",
  465. "_score": 0.7093853,
  466. "_source": {
  467. "query": {
  468. "match": {
  469. "message": "bonsai tree"
  470. }
  471. }
  472. },
  473. "fields" : {
  474. "_percolator_document_slot" : [0, 1, 3]
  475. },
  476. "highlight" : { <1>
  477. "0_message" : [
  478. "<em>bonsai</em> <em>tree</em>"
  479. ],
  480. "3_message" : [
  481. "office <em>tree</em>"
  482. ],
  483. "1_message" : [
  484. "new <em>tree</em>"
  485. ]
  486. }
  487. }
  488. ]
  489. }
  490. }
  491. --------------------------------------------------
  492. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  493. <1> The highlight fields have been prefixed with the document slot they belong to,
  494. in order to know which highlight field belongs to what document.
  495. [discrete]
  496. ==== Specifying multiple percolate queries
  497. It is possible to specify multiple `percolate` queries in a single search request:
  498. [source,console]
  499. --------------------------------------------------
  500. GET /my-index-000001/_search
  501. {
  502. "query": {
  503. "bool": {
  504. "should": [
  505. {
  506. "percolate": {
  507. "field": "query",
  508. "document": {
  509. "message": "bonsai tree"
  510. },
  511. "name": "query1" <1>
  512. }
  513. },
  514. {
  515. "percolate": {
  516. "field": "query",
  517. "document": {
  518. "message": "tulip flower"
  519. },
  520. "name": "query2" <1>
  521. }
  522. }
  523. ]
  524. }
  525. }
  526. }
  527. --------------------------------------------------
  528. // TEST[continued]
  529. <1> The `name` parameter will be used to identify which percolator document slots belong to what `percolate` query.
  530. The `_percolator_document_slot` field name will be suffixed with what is specified in the `_name` parameter.
  531. If that isn't specified then the `field` parameter will be used, which in this case will result in ambiguity.
  532. The above search request returns a response similar to this:
  533. [source,console-result]
  534. --------------------------------------------------
  535. {
  536. "took": 13,
  537. "timed_out": false,
  538. "_shards": {
  539. "total": 1,
  540. "successful": 1,
  541. "skipped" : 0,
  542. "failed": 0
  543. },
  544. "hits": {
  545. "total" : {
  546. "value": 1,
  547. "relation": "eq"
  548. },
  549. "max_score": 0.26152915,
  550. "hits": [
  551. {
  552. "_index": "my-index-000001",
  553. "_id": "1",
  554. "_score": 0.26152915,
  555. "_source": {
  556. "query": {
  557. "match": {
  558. "message": "bonsai tree"
  559. }
  560. }
  561. },
  562. "fields" : {
  563. "_percolator_document_slot_query1" : [0] <1>
  564. }
  565. }
  566. ]
  567. }
  568. }
  569. --------------------------------------------------
  570. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  571. <1> The `_percolator_document_slot_query1` percolator slot field indicates that these matched slots are from the `percolate`
  572. query with `_name` parameter set to `query1`.
  573. [discrete]
  574. [[how-it-works]]
  575. ==== How it Works Under the Hood
  576. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  577. part of the document gets parsed into a Lucene query and is stored into the Lucene index. A binary representation
  578. of the query gets stored, but also the query's terms are analyzed and stored into an indexed field.
  579. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  580. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. After this
  581. a special query is built based on the terms in the in-memory index that select candidate percolator queries based on
  582. their indexed query terms. These queries are then evaluated by the in-memory index if they actually match.
  583. The selecting of candidate percolator queries matches is an important performance optimization during the execution
  584. of the `percolate` query as it can significantly reduce the number of candidate matches the in-memory index needs to
  585. evaluate. The reason the `percolate` query can do this is because during indexing of the percolator queries the query
  586. terms are being extracted and indexed with the percolator query. Unfortunately the percolator cannot extract terms from
  587. all queries (for example the `wildcard` or `geo_shape` query) and as a result of that in certain cases the percolator
  588. can't do the selecting optimization (for example if an unsupported query is defined in a required clause of a boolean query
  589. or the unsupported query is the only query in the percolator document). These queries are marked by the percolator and
  590. can be found by running the following search:
  591. [source,console]
  592. ---------------------------------------------------
  593. GET /_search
  594. {
  595. "query": {
  596. "term" : {
  597. "query.extraction_result" : "failed"
  598. }
  599. }
  600. }
  601. ---------------------------------------------------
  602. NOTE: The above example assumes that there is a `query` field of type
  603. `percolator` in the mappings.
  604. Given the design of percolation, it often makes sense to use separate indices for the percolate queries and documents
  605. being percolated, as opposed to a single index as we do in examples. There are a few benefits to this approach:
  606. - Because percolate queries contain a different set of fields from the percolated documents, using two separate indices
  607. allows for fields to be stored in a denser, more efficient way.
  608. - Percolate queries do not scale in the same way as other queries, so percolation performance may benefit from using
  609. a different index configuration, like the number of primary shards.
  610. [[percolate-query-notes]]
  611. ==== Notes
  612. ===== Allow expensive queries
  613. Percolate queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
  614. is set to false.