rollup-search.asciidoc 7.6 KB

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