1
0

percolate-query.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 mappings:
  10. [source,js]
  11. --------------------------------------------------
  12. PUT /my-index
  13. {
  14. "mappings": {
  15. "doctype": {
  16. "properties": {
  17. "message": {
  18. "type": "text"
  19. }
  20. }
  21. },
  22. "queries": {
  23. "properties": {
  24. "query": {
  25. "type": "percolator"
  26. }
  27. }
  28. }
  29. }
  30. }
  31. --------------------------------------------------
  32. // CONSOLE
  33. The `doctype` mapping is the mapping used to preprocess
  34. the document defined in the `percolator` query before it
  35. gets indexed into a temporary index.
  36. The `queries` mapping is the mapping used for indexing
  37. the query documents. The `query` field will hold a json
  38. object that represents an actual Elasticsearch query. The
  39. `query` field has been configured to use the
  40. <<percolator,percolator field type>>. This field type understands
  41. the query dsl and stored the query in such a way that it
  42. can be used later on to match documents defined on the `percolate` query.
  43. Register a query in the percolator:
  44. [source,js]
  45. --------------------------------------------------
  46. PUT /my-index/queries/1?refresh
  47. {
  48. "query" : {
  49. "match" : {
  50. "message" : "bonsai tree"
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // CONSOLE
  56. // TEST[continued]
  57. Match a document to the registered percolator queries:
  58. [source,js]
  59. --------------------------------------------------
  60. GET /my-index/_search
  61. {
  62. "query" : {
  63. "percolate" : {
  64. "field" : "query",
  65. "document_type" : "doctype",
  66. "document" : {
  67. "message" : "A new bonsai tree in the office"
  68. }
  69. }
  70. }
  71. }
  72. --------------------------------------------------
  73. // CONSOLE
  74. // TEST[continued]
  75. The above request will yield the following response:
  76. [source,js]
  77. --------------------------------------------------
  78. {
  79. "took": 13,
  80. "timed_out": false,
  81. "_shards": {
  82. "total": 5,
  83. "successful": 5,
  84. "failed": 0
  85. },
  86. "hits": {
  87. "total": 1,
  88. "max_score": 0.5716521,
  89. "hits": [
  90. { <1>
  91. "_index": "my-index",
  92. "_type": "queries",
  93. "_id": "1",
  94. "_score": 0.5716521,
  95. "_source": {
  96. "query": {
  97. "match": {
  98. "message": "bonsai tree"
  99. }
  100. }
  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. [float]
  110. ==== Parameters
  111. The following parameters are required when percolating a document:
  112. [horizontal]
  113. `field`:: The field of type `percolator` and that holds the indexed queries. This is a required parameter.
  114. `document_type`:: The type / mapping of the document being percolated. This is a required parameter.
  115. `document`:: The source of the document being percolated.
  116. Instead of specifying the source of the document being percolated, the source can also be retrieved from an already
  117. stored document. The `percolate` query will then internally execute a get request to fetch that document.
  118. In that case the `document` parameter can be substituted with the following parameters:
  119. [horizontal]
  120. `index`:: The index the document resides in. This is a required parameter.
  121. `type`:: The type of the document to fetch. This is a required parameter.
  122. `id`:: The id of the document to fetch. This is a required parameter.
  123. `routing`:: Optionally, routing to be used to fetch document to percolate.
  124. `preference`:: Optionally, preference to be used to fetch document to percolate.
  125. `version`:: Optionally, the expected version of the document to be fetched.
  126. [float]
  127. ==== Percolating an Existing Document
  128. In order to percolate a newly indexed document, the `percolate` query can be used. Based on the response
  129. from an index request, the `_id` and other meta information can be used to immediately percolate the newly added
  130. document.
  131. [float]
  132. ===== Example
  133. Based on the previous example.
  134. Index the document we want to percolate:
  135. [source,js]
  136. --------------------------------------------------
  137. PUT /my-index/message/1
  138. {
  139. "message" : "A new bonsai tree in the office"
  140. }
  141. --------------------------------------------------
  142. // CONSOLE
  143. // TEST[continued]
  144. Index response:
  145. [source,js]
  146. --------------------------------------------------
  147. {
  148. "_index": "my-index",
  149. "_type": "message",
  150. "_id": "1",
  151. "_version": 1,
  152. "_shards": {
  153. "total": 2,
  154. "successful": 1,
  155. "failed": 0
  156. },
  157. "created": true,
  158. "result": "created",
  159. "_seq_no" : 1
  160. }
  161. --------------------------------------------------
  162. // TESTRESPONSE
  163. Percolating an existing document, using the index response as basis to build to new search request:
  164. [source,js]
  165. --------------------------------------------------
  166. GET /my-index/_search
  167. {
  168. "query" : {
  169. "percolate" : {
  170. "field": "query",
  171. "document_type" : "doctype",
  172. "index" : "my-index",
  173. "type" : "message",
  174. "id" : "1",
  175. "version" : 1 <1>
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. // CONSOLE
  181. // TEST[continued]
  182. <1> The version is optional, but useful in certain cases. We can then ensure that we are try to percolate
  183. the document we just have indexed. A change may be made after we have indexed, and if that is the
  184. case the then the search request would fail with a version conflict error.
  185. The search response returned is identical as in the previous example.
  186. [float]
  187. ==== Percolate query and highlighting
  188. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  189. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  190. the search request is used to highlight the hits.
  191. [float]
  192. ===== Example
  193. This example is based on the mapping of the first example.
  194. Save a query:
  195. [source,js]
  196. --------------------------------------------------
  197. PUT /my-index/queries/1?refresh
  198. {
  199. "query" : {
  200. "match" : {
  201. "message" : "brown fox"
  202. }
  203. }
  204. }
  205. --------------------------------------------------
  206. // CONSOLE
  207. // TEST[continued]
  208. Save another query:
  209. [source,js]
  210. --------------------------------------------------
  211. PUT /my-index/queries/2?refresh
  212. {
  213. "query" : {
  214. "match" : {
  215. "message" : "lazy dog"
  216. }
  217. }
  218. }
  219. --------------------------------------------------
  220. // CONSOLE
  221. // TEST[continued]
  222. Execute a search request with the `percolate` query and highlighting enabled:
  223. [source,js]
  224. --------------------------------------------------
  225. GET /my-index/_search
  226. {
  227. "query" : {
  228. "percolate" : {
  229. "field": "query",
  230. "document_type" : "doctype",
  231. "document" : {
  232. "message" : "The quick brown fox jumps over the lazy dog"
  233. }
  234. }
  235. },
  236. "highlight": {
  237. "fields": {
  238. "message": {}
  239. }
  240. }
  241. }
  242. --------------------------------------------------
  243. // CONSOLE
  244. // TEST[continued]
  245. This will yield the following response.
  246. [source,js]
  247. --------------------------------------------------
  248. {
  249. "took": 7,
  250. "timed_out": false,
  251. "_shards": {
  252. "total": 5,
  253. "successful": 5,
  254. "failed": 0
  255. },
  256. "hits": {
  257. "total": 2,
  258. "max_score": 0.5446649,
  259. "hits": [
  260. {
  261. "_index": "my-index",
  262. "_type": "queries",
  263. "_id": "2",
  264. "_score": 0.5446649,
  265. "_source": {
  266. "query": {
  267. "match": {
  268. "message": "lazy dog"
  269. }
  270. }
  271. },
  272. "highlight": {
  273. "message": [
  274. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  275. ]
  276. }
  277. },
  278. {
  279. "_index": "my-index",
  280. "_type": "queries",
  281. "_id": "1",
  282. "_score": 0.5446649,
  283. "_source": {
  284. "query": {
  285. "match": {
  286. "message": "brown fox"
  287. }
  288. }
  289. },
  290. "highlight": {
  291. "message": [
  292. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  293. ]
  294. }
  295. }
  296. ]
  297. }
  298. }
  299. --------------------------------------------------
  300. // TESTRESPONSE[s/"took": 7,/"took": "$body.took",/]
  301. <1> The terms from each query have been highlighted in the document.
  302. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  303. the document defined in the `percolate` query.
  304. [float]
  305. ==== How it Works Under the Hood
  306. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  307. part of the documents gets parsed into a Lucene query and are stored into the Lucene index. A binary representation
  308. of the query gets stored, but also the query's terms are analyzed and stored into an indexed field.
  309. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  310. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. After this
  311. a special query is build based on the terms in the in-memory index that select candidate percolator queries based on
  312. their indexed query terms. These queries are then evaluated by the in-memory index if they actually match.
  313. The selecting of candidate percolator queries matches is an important performance optimization during the execution
  314. of the `percolate` query as it can significantly reduce the number of candidate matches the in-memory index needs to
  315. evaluate. The reason the `percolate` query can do this is because during indexing of the percolator queries the query
  316. terms are being extracted and indexed with the percolator query. Unfortunately the percolator cannot extract terms from
  317. all queries (for example the `wildcard` or `geo_shape` query) and as a result of that in certain cases the percolator
  318. can't do the selecting optimization (for example if an unsupported query is defined in a required clause of a boolean query
  319. or the unsupported query is the only query in the percolator document). These queries are marked by the percolator and
  320. can be found by running the following search:
  321. [source,js]
  322. ---------------------------------------------------
  323. GET /_search
  324. {
  325. "query": {
  326. "term" : {
  327. "query.extraction_result" : "failed"
  328. }
  329. }
  330. }
  331. ---------------------------------------------------
  332. // CONSOLE
  333. NOTE: The above example assumes that there is a `query` field of type
  334. `percolator` in the mappings.