percolate-query.asciidoc 11 KB

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