percolate-query.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_type" : "doc",
  59. "document" : {
  60. "message" : "A new bonsai tree in the office"
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // CONSOLE
  67. // TEST[continued]
  68. The above request will yield the following response:
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. "took": 13,
  73. "timed_out": false,
  74. "_shards": {
  75. "total": 5,
  76. "successful": 5,
  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` and that holds the indexed queries. This is a required parameter.
  107. `document_type`:: The type / mapping of the document being percolated. This is a required parameter.
  108. `document`:: The source of the document being percolated.
  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. "created": true,
  151. "result": "created",
  152. "_seq_no" : 0,
  153. "_primary_term" : 1
  154. }
  155. --------------------------------------------------
  156. // TESTRESPONSE
  157. Percolating an existing document, using the index response as basis to build to new search request:
  158. [source,js]
  159. --------------------------------------------------
  160. GET /my-index/_search
  161. {
  162. "query" : {
  163. "percolate" : {
  164. "field": "query",
  165. "document_type" : "doc",
  166. "index" : "my-index",
  167. "type" : "doc",
  168. "id" : "2",
  169. "version" : 1 <1>
  170. }
  171. }
  172. }
  173. --------------------------------------------------
  174. // CONSOLE
  175. // TEST[continued]
  176. <1> The version is optional, but useful in certain cases. We can then ensure that we are try to percolate
  177. the document we just have indexed. A change may be made after we have indexed, and if that is the
  178. case the then the search request would fail with a version conflict error.
  179. The search response returned is identical as in the previous example.
  180. [float]
  181. ==== Percolate query and highlighting
  182. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  183. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  184. the search request is used to highlight the hits.
  185. [float]
  186. ===== Example
  187. This example is based on the mapping of the first example.
  188. Save a query:
  189. [source,js]
  190. --------------------------------------------------
  191. PUT /my-index/doc/3?refresh
  192. {
  193. "query" : {
  194. "match" : {
  195. "message" : "brown fox"
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. // CONSOLE
  201. // TEST[continued]
  202. Save another query:
  203. [source,js]
  204. --------------------------------------------------
  205. PUT /my-index/doc/4?refresh
  206. {
  207. "query" : {
  208. "match" : {
  209. "message" : "lazy dog"
  210. }
  211. }
  212. }
  213. --------------------------------------------------
  214. // CONSOLE
  215. // TEST[continued]
  216. Execute a search request with the `percolate` query and highlighting enabled:
  217. [source,js]
  218. --------------------------------------------------
  219. GET /my-index/_search
  220. {
  221. "query" : {
  222. "percolate" : {
  223. "field": "query",
  224. "document_type" : "doc",
  225. "document" : {
  226. "message" : "The quick brown fox jumps over the lazy dog"
  227. }
  228. }
  229. },
  230. "highlight": {
  231. "fields": {
  232. "message": {}
  233. }
  234. }
  235. }
  236. --------------------------------------------------
  237. // CONSOLE
  238. // TEST[continued]
  239. This will yield the following response.
  240. [source,js]
  241. --------------------------------------------------
  242. {
  243. "took": 7,
  244. "timed_out": false,
  245. "_shards": {
  246. "total": 5,
  247. "successful": 5,
  248. "failed": 0
  249. },
  250. "hits": {
  251. "total": 2,
  252. "max_score": 0.5753642,
  253. "hits": [
  254. {
  255. "_index": "my-index",
  256. "_type": "doc",
  257. "_id": "4",
  258. "_score": 0.5753642,
  259. "_source": {
  260. "query": {
  261. "match": {
  262. "message": "lazy dog"
  263. }
  264. }
  265. },
  266. "highlight": {
  267. "message": [
  268. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  269. ]
  270. }
  271. },
  272. {
  273. "_index": "my-index",
  274. "_type": "doc",
  275. "_id": "3",
  276. "_score": 0.5753642,
  277. "_source": {
  278. "query": {
  279. "match": {
  280. "message": "brown fox"
  281. }
  282. }
  283. },
  284. "highlight": {
  285. "message": [
  286. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  287. ]
  288. }
  289. }
  290. ]
  291. }
  292. }
  293. --------------------------------------------------
  294. // TESTRESPONSE[s/"took": 7,/"took": "$body.took",/]
  295. <1> The terms from each query have been highlighted in the document.
  296. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  297. the document defined in the `percolate` query.
  298. [float]
  299. ==== How it Works Under the Hood
  300. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  301. part of the documents gets parsed into a Lucene query and are stored into the Lucene index. A binary representation
  302. of the query gets stored, but also the query's terms are analyzed and stored into an indexed field.
  303. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  304. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. After this
  305. a special query is build based on the terms in the in-memory index that select candidate percolator queries based on
  306. their indexed query terms. These queries are then evaluated by the in-memory index if they actually match.
  307. The selecting of candidate percolator queries matches is an important performance optimization during the execution
  308. of the `percolate` query as it can significantly reduce the number of candidate matches the in-memory index needs to
  309. evaluate. The reason the `percolate` query can do this is because during indexing of the percolator queries the query
  310. terms are being extracted and indexed with the percolator query. Unfortunately the percolator cannot extract terms from
  311. all queries (for example the `wildcard` or `geo_shape` query) and as a result of that in certain cases the percolator
  312. can't do the selecting optimization (for example if an unsupported query is defined in a required clause of a boolean query
  313. or the unsupported query is the only query in the percolator document). These queries are marked by the percolator and
  314. can be found by running the following search:
  315. [source,js]
  316. ---------------------------------------------------
  317. GET /_search
  318. {
  319. "query": {
  320. "term" : {
  321. "query.extraction_result" : "failed"
  322. }
  323. }
  324. }
  325. ---------------------------------------------------
  326. // CONSOLE
  327. NOTE: The above example assumes that there is a `query` field of type
  328. `percolator` in the mappings.