percolate-query.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. curl -XPUT "http://localhost:9200/my-index" -d'
  13. {
  14. "mappings": {
  15. "doctype": {
  16. "properties": {
  17. "message": {
  18. "type": "string"
  19. }
  20. }
  21. },
  22. "queries": {
  23. "properties": {
  24. "query": {
  25. "type": "percolator"
  26. }
  27. }
  28. }
  29. }
  30. }'
  31. --------------------------------------------------
  32. The `doctype` mapping is the mapping used to preprocess
  33. the document defined in the `percolator` query before it
  34. gets indexed into a temporary index.
  35. The `queries` mapping is the mapping used for indexing
  36. the query documents. The `query` field will hold a json
  37. object that represents an actual Elasticsearch query. The
  38. `query` field has been configured to use the
  39. <<percolator,percolator field type>>. This field type understands
  40. the query dsl and stored the query in such a way that it
  41. can be used later on to match documents defined on the `percolate` query.
  42. Register a query in the percolator:
  43. [source,js]
  44. --------------------------------------------------
  45. curl -XPUT 'localhost:9200/my-index/queries/1' -d '{
  46. "query" : {
  47. "match" : {
  48. "message" : "bonsai tree"
  49. }
  50. }
  51. }'
  52. --------------------------------------------------
  53. Match a document to the registered percolator queries:
  54. [source,js]
  55. --------------------------------------------------
  56. curl -XGET 'localhost:9200/my-index/_search' -d '{
  57. "query" : {
  58. "percolate" : {
  59. "field" : "query",
  60. "document_type" : "doctype",
  61. "document" : {
  62. "message" : "A new bonsai tree in the office"
  63. }
  64. }
  65. }
  66. }'
  67. --------------------------------------------------
  68. The above request will yield the following response:
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. "took": 5,
  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.5716521,
  82. "hits": [
  83. { <1>
  84. "_index": "my-index",
  85. "_type": "queries",
  86. "_id": "1",
  87. "_score": 0.5716521,
  88. "_source": {
  89. "query": {
  90. "match": {
  91. "message": "bonsai tree"
  92. }
  93. }
  94. }
  95. }
  96. ]
  97. }
  98. }
  99. --------------------------------------------------
  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 a 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. curl -XPUT "http://localhost:9200/my-index/message/1" -d'
  130. {
  131. "message" : "A new bonsai tree in the office"
  132. }'
  133. --------------------------------------------------
  134. Index response:
  135. [source,js]
  136. --------------------------------------------------
  137. {
  138. "_index": "my-index",
  139. "_type": "message",
  140. "_id": "1",
  141. "_version": 1,
  142. "_shards": {
  143. "total": 2,
  144. "successful": 1,
  145. "failed": 0
  146. },
  147. "created": true
  148. }
  149. --------------------------------------------------
  150. Percolating an existing document, using the index response as basis to build to new search request:
  151. [source,js]
  152. --------------------------------------------------
  153. curl -XGET "http://localhost:9200/my-index/_search" -d'
  154. {
  155. "query" : {
  156. "percolate" : {
  157. "field": "query",
  158. "document_type" : "doctype",
  159. "index" : "my-index",
  160. "type" : "message",
  161. "id" : "1",
  162. "version" : 1 <1>
  163. }
  164. }
  165. }'
  166. --------------------------------------------------
  167. <1> The version is optional, but useful in certain cases. We can then ensure that we are try to percolate
  168. the document we just have indexed. A change may be made after we have indexed, and if that is the
  169. case the then the search request would fail with a version conflict error.
  170. The search response returned is identical as in the previous example.
  171. [float]
  172. ==== Percolate query and highlighting
  173. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  174. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  175. the search request is used to highlight the hits.
  176. [float]
  177. ===== Example
  178. This example is based on the mapping of the first example.
  179. Save a query:
  180. [source,js]
  181. --------------------------------------------------
  182. curl -XPUT "http://localhost:9200/my-index/queries/1" -d'
  183. {
  184. "query" : {
  185. "match" : {
  186. "message" : "brown fox"
  187. }
  188. }
  189. }'
  190. --------------------------------------------------
  191. Save another query:
  192. [source,js]
  193. --------------------------------------------------
  194. curl -XPUT "http://localhost:9200/my-index/queries/2" -d'
  195. {
  196. "query" : {
  197. "match" : {
  198. "message" : "lazy dog"
  199. }
  200. }
  201. }'
  202. --------------------------------------------------
  203. Execute a search request with the `percolate` query and highlighting enabled:
  204. [source,js]
  205. --------------------------------------------------
  206. curl -XGET "http://localhost:9200/my-index/_search" -d'
  207. {
  208. "query" : {
  209. "percolate" : {
  210. "field": "query",
  211. "document_type" : "doctype",
  212. "document" : {
  213. "message" : "The quick brown fox jumps over the lazy dog"
  214. }
  215. }
  216. },
  217. "highlight": {
  218. "fields": {
  219. "message": {}
  220. }
  221. }
  222. }'
  223. --------------------------------------------------
  224. This will yield the following response.
  225. [source,js]
  226. --------------------------------------------------
  227. {
  228. "took": 83,
  229. "timed_out": false,
  230. "_shards": {
  231. "total": 5,
  232. "successful": 5,
  233. "failed": 0
  234. },
  235. "hits": {
  236. "total": 2,
  237. "max_score": 0.5446649,
  238. "hits": [
  239. {
  240. "_index": "my-index",
  241. "_type": "queries",
  242. "_id": "2",
  243. "_score": 0.5446649,
  244. "_source": {
  245. "query": {
  246. "match": {
  247. "message": "lazy dog"
  248. }
  249. }
  250. },
  251. "highlight": {
  252. "message": [
  253. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  254. ]
  255. }
  256. },
  257. {
  258. "_index": "my-index",
  259. "_type": "queries",
  260. "_id": "1",
  261. "_score": 0.5446649,
  262. "_source": {
  263. "query": {
  264. "match": {
  265. "message": "brown fox"
  266. }
  267. }
  268. },
  269. "highlight": {
  270. "message": [
  271. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  272. ]
  273. }
  274. }
  275. ]
  276. }
  277. }
  278. --------------------------------------------------
  279. Instead of the query in the search request highlighting the percolator hits, the percolator queries are highlighting
  280. the document defined in the `percolate` query.
  281. [float]
  282. ==== How it Works Under the Hood
  283. When indexing a document into an index that has the <<percolator,percolator field type>> mapping configured, the query
  284. part of the documents gets parsed into a Lucene query and is kept in memory until that percolator document is removed.
  285. So, all the active percolator queries are kept in memory.
  286. At search time, the document specified in the request gets parsed into a Lucene document and is stored in a in-memory
  287. temporary Lucene index. This in-memory index can just hold this one document and it is optimized for that. Then all the queries
  288. that are registered to the index that the search request is targeted for, are going to be executed on this single document
  289. in-memory index. This happens on each shard the search request needs to execute.
  290. By using `routing` or additional queries the amount of percolator queries that need to be executed can be reduced and thus
  291. the time the search API needs to run can be decreased.