percolate-query.asciidoc 10 KB

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