percolate-query.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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": "string"
  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
  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": 5,
  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. <1> The query with id `1` matches our document.
  108. [float]
  109. ==== Parameters
  110. The following parameters are required when percolating a document:
  111. [horizontal]
  112. `field`:: The field of type `percolator` and that holds the indexed queries. This is a required parameter.
  113. `document_type`:: The type / mapping of the document being percolated. This is a required parameter.
  114. `document`:: The source of the document being percolated.
  115. Instead of specifying a the source of the document being percolated, the source can also be retrieved from an already
  116. stored document. The `percolate` query will then internally execute a get request to fetch that document.
  117. In that case the `document` parameter can be substituted with the following parameters:
  118. [horizontal]
  119. `index`:: The index the document resides in. This is a required parameter.
  120. `type`:: The type of the document to fetch. This is a required parameter.
  121. `id`:: The id of the document to fetch. This is a required parameter.
  122. `routing`:: Optionally, routing to be used to fetch document to percolate.
  123. `preference`:: Optionally, preference to be used to fetch document to percolate.
  124. `version`:: Optionally, the expected version of the document to be fetched.
  125. [float]
  126. ==== Percolating an Existing Document
  127. In order to percolate a newly indexed document, the `percolate` query can be used. Based on the response
  128. from an index request, the `_id` and other meta information can be used to immediately percolate the newly added
  129. document.
  130. [float]
  131. ===== Example
  132. Based on the previous example.
  133. Index the document we want to percolate:
  134. [source,js]
  135. --------------------------------------------------
  136. PUT /my-index/message/1
  137. {
  138. "message" : "A new bonsai tree in the office"
  139. }
  140. --------------------------------------------------
  141. // CONSOLE
  142. // TEST[continued]
  143. Index response:
  144. [source,js]
  145. --------------------------------------------------
  146. {
  147. "_index": "my-index",
  148. "_type": "message",
  149. "_id": "1",
  150. "_version": 1,
  151. "_shards": {
  152. "total": 2,
  153. "successful": 1,
  154. "failed": 0
  155. },
  156. "created": true
  157. }
  158. --------------------------------------------------
  159. Percolating an existing document, using the index response as basis to build to new search request:
  160. [source,js]
  161. --------------------------------------------------
  162. GET /my-index/_search
  163. {
  164. "query" : {
  165. "percolate" : {
  166. "field": "query",
  167. "document_type" : "doctype",
  168. "index" : "my-index",
  169. "type" : "message",
  170. "id" : "1",
  171. "version" : 1 <1>
  172. }
  173. }
  174. }
  175. --------------------------------------------------
  176. // CONSOLE
  177. // TEST[continued]
  178. <1> The version is optional, but useful in certain cases. We can then ensure that we are try to percolate
  179. the document we just have indexed. A change may be made after we have indexed, and if that is the
  180. case the then the search request would fail with a version conflict error.
  181. The search response returned is identical as in the previous example.
  182. [float]
  183. ==== Percolate query and highlighting
  184. The `percolate` query is handled in a special way when it comes to highlighting. The queries hits are used
  185. to highlight the document that is provided in the `percolate` query. Whereas with regular highlighting the query in
  186. the search request is used to highlight the hits.
  187. [float]
  188. ===== Example
  189. This example is based on the mapping of the first example.
  190. Save a query:
  191. [source,js]
  192. --------------------------------------------------
  193. PUT /my-index/queries/1
  194. {
  195. "query" : {
  196. "match" : {
  197. "message" : "brown fox"
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. // CONSOLE
  203. // TEST[continued]
  204. Save another query:
  205. [source,js]
  206. --------------------------------------------------
  207. PUT /my-index/queries/2
  208. {
  209. "query" : {
  210. "match" : {
  211. "message" : "lazy dog"
  212. }
  213. }
  214. }
  215. --------------------------------------------------
  216. // CONSOLE
  217. // TEST[continued]
  218. Execute a search request with the `percolate` query and highlighting enabled:
  219. [source,js]
  220. --------------------------------------------------
  221. GET /my-index/_search
  222. {
  223. "query" : {
  224. "percolate" : {
  225. "field": "query",
  226. "document_type" : "doctype",
  227. "document" : {
  228. "message" : "The quick brown fox jumps over the lazy dog"
  229. }
  230. }
  231. },
  232. "highlight": {
  233. "fields": {
  234. "message": {}
  235. }
  236. }
  237. }
  238. --------------------------------------------------
  239. // CONSOLE
  240. // TEST[continued]
  241. This will yield the following response.
  242. [source,js]
  243. --------------------------------------------------
  244. {
  245. "took": 83,
  246. "timed_out": false,
  247. "_shards": {
  248. "total": 5,
  249. "successful": 5,
  250. "failed": 0
  251. },
  252. "hits": {
  253. "total": 2,
  254. "max_score": 0.5446649,
  255. "hits": [
  256. {
  257. "_index": "my-index",
  258. "_type": "queries",
  259. "_id": "2",
  260. "_score": 0.5446649,
  261. "_source": {
  262. "query": {
  263. "match": {
  264. "message": "lazy dog"
  265. }
  266. }
  267. },
  268. "highlight": {
  269. "message": [
  270. "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" <1>
  271. ]
  272. }
  273. },
  274. {
  275. "_index": "my-index",
  276. "_type": "queries",
  277. "_id": "1",
  278. "_score": 0.5446649,
  279. "_source": {
  280. "query": {
  281. "match": {
  282. "message": "brown fox"
  283. }
  284. }
  285. },
  286. "highlight": {
  287. "message": [
  288. "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" <1>
  289. ]
  290. }
  291. }
  292. ]
  293. }
  294. }
  295. --------------------------------------------------
  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 is kept in memory until that percolator document is removed.
  302. So, all the active percolator queries are kept in memory.
  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. Then all the queries
  305. that are registered to the index that the search request is targeted for, are going to be executed on this single document
  306. in-memory index. This happens on each shard the search request needs to execute.
  307. By using `routing` or additional queries the amount of percolator queries that need to be executed can be reduced and thus
  308. the time the search API needs to run can be decreased.