percolate-query.asciidoc 19 KB

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