percolate-query.asciidoc 19 KB

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