rollup-search.asciidoc 7.3 KB

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