rollup-search.asciidoc 7.7 KB

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