percolate-query.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 stored 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": 5,
  75. "successful": 5,
  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 multiple documents
  129. The `percolate` query can match multiple documents simultaneously with the indexed percolator queries.
  130. Percolating multiple documents in a single request can improve performance as queries only need to be parsed and
  131. matched once instead of multiple times.
  132. The `_percolator_document_slot` field that is being returned with each matched percolator query is important when percolating
  133. multiple documents simultaneously. It indicates which documents matched with a particular percolator query. The numbers
  134. correlate with the slot in the `documents` array specified in the `percolate` query.
  135. [source,js]
  136. --------------------------------------------------
  137. GET /my-index/_search
  138. {
  139. "query" : {
  140. "percolate" : {
  141. "field" : "query",
  142. "documents" : [ <1>
  143. {
  144. "message" : "bonsai tree"
  145. },
  146. {
  147. "message" : "new tree"
  148. },
  149. {
  150. "message" : "the office"
  151. },
  152. {
  153. "message" : "office tree"
  154. }
  155. ]
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. // CONSOLE
  161. // TEST[continued]
  162. <1> The documents array contains 4 documents that are going to be percolated at the same time.
  163. [source,js]
  164. --------------------------------------------------
  165. {
  166. "took": 13,
  167. "timed_out": false,
  168. "_shards": {
  169. "total": 5,
  170. "successful": 5,
  171. "skipped" : 0,
  172. "failed": 0
  173. },
  174. "hits": {
  175. "total": 1,
  176. "max_score": 1.5606477,
  177. "hits": [
  178. {
  179. "_index": "my-index",
  180. "_type": "doc",
  181. "_id": "1",
  182. "_score": 1.5606477,
  183. "_source": {
  184. "query": {
  185. "match": {
  186. "message": "bonsai tree"
  187. }
  188. }
  189. },
  190. "fields" : {
  191. "_percolator_document_slot" : [0, 1, 3] <1>
  192. }
  193. }
  194. ]
  195. }
  196. }
  197. --------------------------------------------------
  198. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  199. <1> The `_percolator_document_slot` indicates that the first, second and last documents specified in the `percolate` query
  200. are matching with this query.
  201. [float]
  202. ==== Percolating an Existing Document
  203. In order to percolate a newly indexed document, the `percolate` query can be used. Based on the response
  204. from an index request, the `_id` and other meta information can be used to immediately percolate the newly added
  205. document.
  206. [float]
  207. ===== Example
  208. Based on the previous example.
  209. Index the document we want to percolate:
  210. [source,js]
  211. --------------------------------------------------
  212. PUT /my-index/doc/2
  213. {
  214. "message" : "A new bonsai tree in the office"
  215. }
  216. --------------------------------------------------
  217. // CONSOLE
  218. // TEST[continued]
  219. Index response:
  220. [source,js]
  221. --------------------------------------------------
  222. {
  223. "_index": "my-index",
  224. "_type": "doc",
  225. "_id": "2",
  226. "_version": 1,
  227. "_shards": {
  228. "total": 2,
  229. "successful": 1,
  230. "failed": 0
  231. },
  232. "result": "created",
  233. "_seq_no" : 0,
  234. "_primary_term" : 1
  235. }
  236. --------------------------------------------------
  237. // TESTRESPONSE
  238. Percolating an existing document, using the index response as basis to build to new search request:
  239. [source,js]
  240. --------------------------------------------------
  241. GET /my-index/_search
  242. {
  243. "query" : {
  244. "percolate" : {
  245. "field": "query",
  246. "index" : "my-index",
  247. "type" : "doc",
  248. "id" : "2",
  249. "version" : 1 <1>
  250. }
  251. }
  252. }
  253. --------------------------------------------------
  254. // CONSOLE
  255. // TEST[continued]
  256. <1> The version is optional, but useful in certain cases. We can ensure that we are trying to percolate
  257. the document we just have indexed. A change may be made after we have indexed, and if that is the
  258. case the search request would fail with a version conflict error.
  259. The search response returned is identical as in the previous example.
  260. [float]
  261. ==== Percolate query and highlighting
  262. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  263. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  264. the search request is used to highlight the hits.
  265. [float]
  266. ===== Example
  267. This example is based on the mapping of the first example.
  268. Save a query:
  269. [source,js]
  270. --------------------------------------------------
  271. PUT /my-index/doc/3?refresh
  272. {
  273. "query" : {
  274. "match" : {
  275. "message" : "brown fox"
  276. }
  277. }
  278. }
  279. --------------------------------------------------
  280. // CONSOLE
  281. // TEST[continued]
  282. Save another query:
  283. [source,js]
  284. --------------------------------------------------
  285. PUT /my-index/doc/4?refresh
  286. {
  287. "query" : {
  288. "match" : {
  289. "message" : "lazy dog"
  290. }
  291. }
  292. }
  293. --------------------------------------------------
  294. // CONSOLE
  295. // TEST[continued]
  296. Execute a search request with the `percolate` query and highlighting enabled:
  297. [source,js]
  298. --------------------------------------------------
  299. GET /my-index/_search
  300. {
  301. "query" : {
  302. "percolate" : {
  303. "field": "query",
  304. "document" : {
  305. "message" : "The quick brown fox jumps over the lazy dog"
  306. }
  307. }
  308. },
  309. "highlight": {
  310. "fields": {
  311. "message": {}
  312. }
  313. }
  314. }
  315. --------------------------------------------------
  316. // CONSOLE
  317. // TEST[continued]
  318. This will yield the following response.
  319. [source,js]
  320. --------------------------------------------------
  321. {
  322. "took": 7,
  323. "timed_out": false,
  324. "_shards": {
  325. "total": 5,
  326. "successful": 5,
  327. "skipped" : 0,
  328. "failed": 0
  329. },
  330. "hits": {
  331. "total": 2,
  332. "max_score": 0.5753642,
  333. "hits": [
  334. {
  335. "_index": "my-index",
  336. "_type": "doc",
  337. "_id": "4",
  338. "_score": 0.5753642,
  339. "_source": {
  340. "query": {
  341. "match": {
  342. "message": "lazy dog"
  343. }
  344. }
  345. },
  346. "highlight": {
  347. "message": [
  348. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  349. ]
  350. },
  351. "fields" : {
  352. "_percolator_document_slot" : [0]
  353. }
  354. },
  355. {
  356. "_index": "my-index",
  357. "_type": "doc",
  358. "_id": "3",
  359. "_score": 0.5753642,
  360. "_source": {
  361. "query": {
  362. "match": {
  363. "message": "brown fox"
  364. }
  365. }
  366. },
  367. "highlight": {
  368. "message": [
  369. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  370. ]
  371. },
  372. "fields" : {
  373. "_percolator_document_slot" : [0]
  374. }
  375. }
  376. ]
  377. }
  378. }
  379. --------------------------------------------------
  380. // TESTRESPONSE[s/"took": 7,/"took": "$body.took",/]
  381. <1> The terms from each query have been highlighted in the document.
  382. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  383. the document defined in the `percolate` query.
  384. When percolating multiple documents at the same time like the request below then the highlight response is different:
  385. [source,js]
  386. --------------------------------------------------
  387. GET /my-index/_search
  388. {
  389. "query" : {
  390. "percolate" : {
  391. "field": "query",
  392. "documents" : [
  393. {
  394. "message" : "bonsai tree"
  395. },
  396. {
  397. "message" : "new tree"
  398. },
  399. {
  400. "message" : "the office"
  401. },
  402. {
  403. "message" : "office tree"
  404. }
  405. ]
  406. }
  407. },
  408. "highlight": {
  409. "fields": {
  410. "message": {}
  411. }
  412. }
  413. }
  414. --------------------------------------------------
  415. // CONSOLE
  416. // TEST[continued]
  417. The slightly different response:
  418. [source,js]
  419. --------------------------------------------------
  420. {
  421. "took": 13,
  422. "timed_out": false,
  423. "_shards": {
  424. "total": 5,
  425. "successful": 5,
  426. "skipped" : 0,
  427. "failed": 0
  428. },
  429. "hits": {
  430. "total": 1,
  431. "max_score": 1.5606477,
  432. "hits": [
  433. {
  434. "_index": "my-index",
  435. "_type": "doc",
  436. "_id": "1",
  437. "_score": 1.5606477,
  438. "_source": {
  439. "query": {
  440. "match": {
  441. "message": "bonsai tree"
  442. }
  443. }
  444. },
  445. "fields" : {
  446. "_percolator_document_slot" : [0, 1, 3]
  447. },
  448. "highlight" : { <1>
  449. "0_message" : [
  450. "<em>bonsai</em> <em>tree</em>"
  451. ],
  452. "3_message" : [
  453. "office <em>tree</em>"
  454. ],
  455. "1_message" : [
  456. "new <em>tree</em>"
  457. ]
  458. }
  459. }
  460. ]
  461. }
  462. }
  463. --------------------------------------------------
  464. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  465. <1> The highlight fields have been prefixed with the document slot they belong to,
  466. in order to know which highlight field belongs to what document.
  467. [float]
  468. ==== Specifying multiple percolate queries
  469. It is possible to specify multiple `percolate` queries in a single search request:
  470. [source,js]
  471. --------------------------------------------------
  472. GET /my-index/_search
  473. {
  474. "query" : {
  475. "bool" : {
  476. "should" : [
  477. {
  478. "percolate" : {
  479. "field" : "query",
  480. "document" : {
  481. "message" : "bonsai tree"
  482. },
  483. "name": "query1" <1>
  484. }
  485. },
  486. {
  487. "percolate" : {
  488. "field" : "query",
  489. "document" : {
  490. "message" : "tulip flower"
  491. },
  492. "name": "query2" <1>
  493. }
  494. }
  495. ]
  496. }
  497. }
  498. }
  499. --------------------------------------------------
  500. // CONSOLE
  501. // TEST[continued]
  502. <1> The `name` parameter will be used to identify which percolator document slots belong to what `percolate` query.
  503. The `_percolator_document_slot` field name will be suffixed with what is specified in the `_name` parameter.
  504. If that isn't specified then the `field` parameter will be used, which in this case will result in ambiguity.
  505. The above search request returns a response similar to this:
  506. [source,js]
  507. --------------------------------------------------
  508. {
  509. "took": 13,
  510. "timed_out": false,
  511. "_shards": {
  512. "total": 5,
  513. "successful": 5,
  514. "skipped" : 0,
  515. "failed": 0
  516. },
  517. "hits": {
  518. "total": 1,
  519. "max_score": 0.5753642,
  520. "hits": [
  521. {
  522. "_index": "my-index",
  523. "_type": "doc",
  524. "_id": "1",
  525. "_score": 0.5753642,
  526. "_source": {
  527. "query": {
  528. "match": {
  529. "message": "bonsai tree"
  530. }
  531. }
  532. },
  533. "fields" : {
  534. "_percolator_document_slot_query1" : [0] <1>
  535. }
  536. }
  537. ]
  538. }
  539. }
  540. --------------------------------------------------
  541. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  542. <1> The `_percolator_document_slot_query1` percolator slot field indicates that these matched slots are from the `percolate`
  543. query with `_name` parameter set to `query1`.
  544. [float]
  545. ==== How it Works Under the Hood
  546. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  547. part of the document gets parsed into a Lucene query and is stored into the Lucene index. A binary representation
  548. of the query gets stored, but also the query's terms are analyzed and stored into an indexed field.
  549. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  550. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. After this
  551. a special query is built based on the terms in the in-memory index that select candidate percolator queries based on
  552. their indexed query terms. These queries are then evaluated by the in-memory index if they actually match.
  553. The selecting of candidate percolator queries matches is an important performance optimization during the execution
  554. of the `percolate` query as it can significantly reduce the number of candidate matches the in-memory index needs to
  555. evaluate. The reason the `percolate` query can do this is because during indexing of the percolator queries the query
  556. terms are being extracted and indexed with the percolator query. Unfortunately the percolator cannot extract terms from
  557. all queries (for example the `wildcard` or `geo_shape` query) and as a result of that in certain cases the percolator
  558. can't do the selecting optimization (for example if an unsupported query is defined in a required clause of a boolean query
  559. or the unsupported query is the only query in the percolator document). These queries are marked by the percolator and
  560. can be found by running the following search:
  561. [source,js]
  562. ---------------------------------------------------
  563. GET /_search
  564. {
  565. "query": {
  566. "term" : {
  567. "query.extraction_result" : "failed"
  568. }
  569. }
  570. }
  571. ---------------------------------------------------
  572. // CONSOLE
  573. NOTE: The above example assumes that there is a `query` field of type
  574. `percolator` in the mappings.