percolate-query.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. }
  96. ]
  97. }
  98. }
  99. --------------------------------------------------
  100. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  101. <1> The query with id `1` matches our document.
  102. [float]
  103. ==== Parameters
  104. The following parameters are required when percolating a document:
  105. [horizontal]
  106. `field`:: The field of type `percolator` that holds the indexed queries. This is a required parameter.
  107. `document`:: The source of the document being percolated.
  108. `document_type`:: The type / mapping of the document being percolated. This setting is deprecated and only required for indices created before 6.0
  109. Instead of specifying the source of the document being percolated, the source can also be retrieved from an already
  110. stored document. The `percolate` query will then internally execute a get request to fetch that document.
  111. In that case the `document` parameter can be substituted with the following parameters:
  112. [horizontal]
  113. `index`:: The index the document resides in. This is a required parameter.
  114. `type`:: The type of the document to fetch. This is a required parameter.
  115. `id`:: The id of the document to fetch. This is a required parameter.
  116. `routing`:: Optionally, routing to be used to fetch document to percolate.
  117. `preference`:: Optionally, preference to be used to fetch document to percolate.
  118. `version`:: Optionally, the expected version of the document to be fetched.
  119. [float]
  120. ==== Percolating an Existing Document
  121. In order to percolate a newly indexed document, the `percolate` query can be used. Based on the response
  122. from an index request, the `_id` and other meta information can be used to immediately percolate the newly added
  123. document.
  124. [float]
  125. ===== Example
  126. Based on the previous example.
  127. Index the document we want to percolate:
  128. [source,js]
  129. --------------------------------------------------
  130. PUT /my-index/doc/2
  131. {
  132. "message" : "A new bonsai tree in the office"
  133. }
  134. --------------------------------------------------
  135. // CONSOLE
  136. // TEST[continued]
  137. Index response:
  138. [source,js]
  139. --------------------------------------------------
  140. {
  141. "_index": "my-index",
  142. "_type": "doc",
  143. "_id": "2",
  144. "_version": 1,
  145. "_shards": {
  146. "total": 2,
  147. "successful": 1,
  148. "failed": 0
  149. },
  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 ensure that we are trying 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. "skipped" : 0,
  246. "failed": 0
  247. },
  248. "hits": {
  249. "total": 2,
  250. "max_score": 0.5753642,
  251. "hits": [
  252. {
  253. "_index": "my-index",
  254. "_type": "doc",
  255. "_id": "4",
  256. "_score": 0.5753642,
  257. "_source": {
  258. "query": {
  259. "match": {
  260. "message": "lazy dog"
  261. }
  262. }
  263. },
  264. "highlight": {
  265. "message": [
  266. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  267. ]
  268. }
  269. },
  270. {
  271. "_index": "my-index",
  272. "_type": "doc",
  273. "_id": "3",
  274. "_score": 0.5753642,
  275. "_source": {
  276. "query": {
  277. "match": {
  278. "message": "brown fox"
  279. }
  280. }
  281. },
  282. "highlight": {
  283. "message": [
  284. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  285. ]
  286. }
  287. }
  288. ]
  289. }
  290. }
  291. --------------------------------------------------
  292. // TESTRESPONSE[s/"took": 7,/"took": "$body.took",/]
  293. <1> The terms from each query have been highlighted in the document.
  294. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  295. the document defined in the `percolate` query.
  296. [float]
  297. ==== How it Works Under the Hood
  298. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  299. part of the document gets parsed into a Lucene query and is stored into the Lucene index. A binary representation
  300. of the query gets stored, but also the query's terms are analyzed and stored into an indexed field.
  301. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  302. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. After this
  303. a special query is built based on the terms in the in-memory index that select candidate percolator queries based on
  304. their indexed query terms. These queries are then evaluated by the in-memory index if they actually match.
  305. The selecting of candidate percolator queries matches is an important performance optimization during the execution
  306. of the `percolate` query as it can significantly reduce the number of candidate matches the in-memory index needs to
  307. evaluate. The reason the `percolate` query can do this is because during indexing of the percolator queries the query
  308. terms are being extracted and indexed with the percolator query. Unfortunately the percolator cannot extract terms from
  309. all queries (for example the `wildcard` or `geo_shape` query) and as a result of that in certain cases the percolator
  310. can't do the selecting optimization (for example if an unsupported query is defined in a required clause of a boolean query
  311. or the unsupported query is the only query in the percolator document). These queries are marked by the percolator and
  312. can be found by running the following search:
  313. [source,js]
  314. ---------------------------------------------------
  315. GET /_search
  316. {
  317. "query": {
  318. "term" : {
  319. "query.extraction_result" : "failed"
  320. }
  321. }
  322. }
  323. ---------------------------------------------------
  324. // CONSOLE
  325. NOTE: The above example assumes that there is a `query` field of type
  326. `percolator` in the mappings.