rollup-search.asciidoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[rollup-search]]
  4. === Rollup search
  5. ++++
  6. <titleabbrev>Rollup search</titleabbrev>
  7. ++++
  8. Enables searching rolled-up data using the standard query DSL.
  9. experimental[]
  10. [[rollup-search-request]]
  11. ==== {api-request-title}
  12. `GET <index>/_rollup_search`
  13. [[rollup-search-desc]]
  14. ==== {api-description-title}
  15. The rollup search endpoint is needed because, internally, rolled-up documents
  16. utilize a different document structure than the original data. The rollup search
  17. endpoint rewrites standard query DSL into a format that matches the rollup
  18. documents, then takes the response and rewrites it back to what a client would
  19. expect given the original query.
  20. [[rollup-search-path-params]]
  21. ==== {api-path-parms-title}
  22. `<index>`::
  23. (Required, string) Index, indices or index-pattern to execute a rollup search
  24. against. This can include both rollup and non-rollup indices.
  25. Rules for the `index` parameter:
  26. - At least one index/index-pattern must be specified. This can be either a
  27. rollup or non-rollup index. Omitting the index parameter, or using `_all`, is
  28. not permitted.
  29. - Multiple non-rollup indices may be specified
  30. - Only one rollup index may be specified. If more than one are supplied, an
  31. exception occurs.
  32. - Index patterns may be used, but if they match more than one rollup index an
  33. exception occurs.
  34. [[rollup-search-request-body]]
  35. ==== {api-request-body-title}
  36. The request body supports a subset of features from the regular Search API. It
  37. supports:
  38. - `query` param for specifying an DSL query, subject to some limitations
  39. (see <<rollup-search-limitations>> and <<rollup-agg-limitations>>
  40. - `aggregations` param for specifying aggregations
  41. Functionality that is not available:
  42. - `size`: Because rollups work on pre-aggregated data, no search hits can be
  43. returned and so size must be set to zero or omitted entirely.
  44. - `highlighter`, `suggestors`, `post_filter`, `profile`, `explain`: These are
  45. similarly disallowed.
  46. [[rollup-search-example]]
  47. ==== {api-examples-title}
  48. ===== Historical-only search example
  49. Imagine we have an index named `sensor-1` full of raw data, and we have created
  50. a {rollup-job} with the following configuration:
  51. [source,console]
  52. --------------------------------------------------
  53. PUT _rollup/job/sensor
  54. {
  55. "index_pattern": "sensor-*",
  56. "rollup_index": "sensor_rollup",
  57. "cron": "*/30 * * * * ?",
  58. "page_size" :1000,
  59. "groups" : {
  60. "date_histogram": {
  61. "field": "timestamp",
  62. "fixed_interval": "1h",
  63. "delay": "7d"
  64. },
  65. "terms": {
  66. "fields": ["node"]
  67. }
  68. },
  69. "metrics": [
  70. {
  71. "field": "temperature",
  72. "metrics": ["min", "max", "sum"]
  73. },
  74. {
  75. "field": "voltage",
  76. "metrics": ["avg"]
  77. }
  78. ]
  79. }
  80. --------------------------------------------------
  81. // TEST[setup:sensor_index]
  82. This rolls up the `sensor-*` pattern and stores the results in `sensor_rollup`.
  83. To search this rolled up data, we need to use the `_rollup_search` endpoint.
  84. However, you'll notice that we can use regular query DSL to search the rolled-up
  85. data:
  86. [source,console]
  87. --------------------------------------------------
  88. GET /sensor_rollup/_rollup_search
  89. {
  90. "size": 0,
  91. "aggregations": {
  92. "max_temperature": {
  93. "max": {
  94. "field": "temperature"
  95. }
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // TEST[setup:sensor_prefab_data]
  101. // TEST[s/_rollup_search/_rollup_search?filter_path=took,timed_out,terminated_early,_shards,hits,aggregations/]
  102. The query is targeting the `sensor_rollup` data, since this contains the rollup
  103. data as configured in the job. A `max` aggregation has been used on the
  104. `temperature` field, yielding the following response:
  105. [source,console-result]
  106. ----
  107. {
  108. "took" : 102,
  109. "timed_out" : false,
  110. "terminated_early" : false,
  111. "_shards" : ... ,
  112. "hits" : {
  113. "total" : {
  114. "value": 0,
  115. "relation": "eq"
  116. },
  117. "max_score" : 0.0,
  118. "hits" : [ ]
  119. },
  120. "aggregations" : {
  121. "max_temperature" : {
  122. "value" : 202.0
  123. }
  124. }
  125. }
  126. ----
  127. // TESTRESPONSE[s/"took" : 102/"took" : $body.$_path/]
  128. // TESTRESPONSE[s/"_shards" : \.\.\. /"_shards" : $body.$_path/]
  129. The response is exactly as you'd expect from a regular query + aggregation; it
  130. provides some metadata about the request (`took`, `_shards`, etc), the search
  131. hits (which is always empty for rollup searches), and the aggregation response.
  132. Rollup searches are limited to functionality that was configured in the
  133. {rollup-job}. For example, we are not able to calculate the average temperature
  134. because `avg` was not one of the configured metrics for the `temperature` field.
  135. If we try to execute that search:
  136. [source,console]
  137. --------------------------------------------------
  138. GET sensor_rollup/_rollup_search
  139. {
  140. "size": 0,
  141. "aggregations": {
  142. "avg_temperature": {
  143. "avg": {
  144. "field": "temperature"
  145. }
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. // TEST[continued]
  151. // TEST[catch:/illegal_argument_exception/]
  152. [source,console-result]
  153. ----
  154. {
  155. "error" : {
  156. "root_cause" : [
  157. {
  158. "type" : "illegal_argument_exception",
  159. "reason" : "There is not a rollup job that has a [avg] agg with name [avg_temperature] which also satisfies all requirements of query.",
  160. "stack_trace": ...
  161. }
  162. ],
  163. "type" : "illegal_argument_exception",
  164. "reason" : "There is not a rollup job that has a [avg] agg with name [avg_temperature] which also satisfies all requirements of query.",
  165. "stack_trace": ...
  166. },
  167. "status": 400
  168. }
  169. ----
  170. // TESTRESPONSE[s/"stack_trace": \.\.\./"stack_trace": $body.$_path/]
  171. ===== Searching both historical rollup and non-rollup data
  172. The rollup search API has the capability to search across both "live"
  173. non-rollup data and the aggregated rollup data. This is done by simply adding
  174. the live indices to the URI:
  175. [source,console]
  176. --------------------------------------------------
  177. GET sensor-1,sensor_rollup/_rollup_search <1>
  178. {
  179. "size": 0,
  180. "aggregations": {
  181. "max_temperature": {
  182. "max": {
  183. "field": "temperature"
  184. }
  185. }
  186. }
  187. }
  188. --------------------------------------------------
  189. // TEST[continued]
  190. // TEST[s/_rollup_search/_rollup_search?filter_path=took,timed_out,terminated_early,_shards,hits,aggregations/]
  191. <1> Note the URI now searches `sensor-1` and `sensor_rollup` at the same time
  192. When the search is executed, the rollup search endpoint does two things:
  193. 1. The original request is sent to the non-rollup index unaltered.
  194. 2. A rewritten version of the original request is sent to the rollup index.
  195. When the two responses are received, the endpoint rewrites the rollup response
  196. and merges the two together. During the merging process, if there is any overlap
  197. in buckets between the two responses, the buckets from the non-rollup index are
  198. used.
  199. The response to the above query looks as expected, despite spanning rollup and
  200. non-rollup indices:
  201. [source,console-result]
  202. ----
  203. {
  204. "took" : 102,
  205. "timed_out" : false,
  206. "terminated_early" : false,
  207. "_shards" : ... ,
  208. "hits" : {
  209. "total" : {
  210. "value": 0,
  211. "relation": "eq"
  212. },
  213. "max_score" : 0.0,
  214. "hits" : [ ]
  215. },
  216. "aggregations" : {
  217. "max_temperature" : {
  218. "value" : 202.0
  219. }
  220. }
  221. }
  222. ----
  223. // TESTRESPONSE[s/"took" : 102/"took" : $body.$_path/]
  224. // TESTRESPONSE[s/"_shards" : \.\.\. /"_shards" : $body.$_path/]