Index rollups

Time series data increases storage costs, strains cluster health, and slows down aggregations over time. Index rollup lets you periodically reduce data granularity by rolling up old data into summarized indexes.

You pick the fields that interest you and use index rollup to create a new index with only those fields aggregated into coarser time buckets. You can store months or years of historical data at a fraction of the cost with the same query performance.

For example, say you collect CPU consumption data every five seconds and store it on a hot node. Instead of moving older data to a read-only warm node, you can roll up or compress this data with only the average CPU consumption per day or with a 10% decrease in its interval every week.

You can use index rollup in three ways:

  1. Use the index rollup API for an on-demand index rollup job that operates on an index that’s not being actively ingested such as a rolled-over index. For example, you can perform an index rollup operation to reduce data collected at a five minute interval to a weekly average for trend analysis.
  2. Use the OpenSearch Dashboards UI to create an index rollup job that runs on a defined schedule. You can also set it up to roll up your indexes as it’s being actively ingested. For example, you can continuously roll up Logstash indexes from a five second interval to a one hour interval.
  3. Specify the index rollup job as an ISM action for complete index management. This allows you to roll up an index after a certain event such as a rollover, index age reaching a certain point, index becoming read-only, and so on. You can also have rollover and index rollup jobs running in sequence, where the rollover first moves the current index to a warm node and then the index rollup job creates a new index with the minimized data on the hot node.

Create an Index Rollup Job

To get started, choose Index Management in OpenSearch Dashboards. Select Rollup Jobs and choose Create rollup job.

Step 1: Set up indexes

  1. In the Job name and description section, specify a unique name and an optional description for the index rollup job.
  2. In the Indices section, select the source and target index. The source index is the one that you want to roll up. The source index remains as is, the index rollup job creates a new index referred to as a target index. The target index is where the index rollup results are saved. For target index, you can either type in a name for a new index or you select an existing index.
  3. Choose Next

After you create an index rollup job, you can’t change your index selections.

Step 2: Define aggregations and metrics

Select the attributes with the aggregations (terms and histograms) and metrics (avg, sum, max, min, and value count) that you want to roll up. Make sure you don’t add a lot of highly granular attributes, because you won’t save much space.

For example, consider a dataset of cities and demographics within those cities. You can aggregate based on cities and specify demographics within a city as metrics. The order in which you select attributes is critical. A city followed by a demographic is different from a demographic followed by a city.

  1. In the Time aggregation section, select a timestamp field. Choose between a Fixed or Calendar interval type and specify the interval and timezone. The index rollup job uses this information to create a date histogram for the timestamp field.
  2. (Optional) Add additional aggregations for each field. You can choose terms aggregation for all field types and histogram aggregation only for numeric fields.
  3. (Optional) Add additional metrics for each field. You can choose between All, Min, Max, Sum, Avg, or Value Count.
  4. Choose Next.

Step 3: Specify schedule

Specify a schedule to roll up your indexes as it’s being ingested. The index rollup job is enabled by default.

  1. Specify if the data is continuous or not.
  2. For roll up execution frequency, select Define by fixed interval and specify the Rollup interval and the time unit or Define by cron expression and add in a cron expression to select the interval. To learn how to define a cron expression, see Alerting.
  3. Specify the number of pages per execution process. A larger number means faster execution and more cost for memory.
  4. (Optional) Add a delay to the roll up executions. This is the amount of time the job waits for data ingestion to accommodate any processing time. For example, if you set this value to 10 minutes, an index rollup that executes at 2 PM to roll up 1 PM to 2 PM of data starts at 2:10 PM.
  5. Choose Next.

Step 4: Review and create

Review your configuration and select Create.

Step 5: Search the target index

You can use the standard _search API to search the target index. Make sure that the query matches the constraints of the target index. For example, if you don’t set up terms aggregations on a field, you don’t receive results for terms aggregations. If you don’t set up the maximum aggregations, you don’t receive results for maximum aggregations.

You can’t access the internal structure of the data in the target index because the plugin automatically rewrites the query in the background to suit the target index. This is to make sure you can use the same query for the source and target index.

To query the target index, set size to 0:

  1. GET target_index/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "match_all": {}
  6. },
  7. "aggs": {
  8. "avg_cpu": {
  9. "avg": {
  10. "field": "cpu_usage"
  11. }
  12. }
  13. }
  14. }

Consider a scenario where you collect rolled up data from 1 PM to 9 PM in hourly intervals and live data from 7 PM to 11 PM in minutely intervals. If you execute an aggregation over these in the same query, for 7 PM to 9 PM, you see an overlap of both rolled up data and live data because they get counted twice in the aggregations.

Sample Walkthrough

This walkthrough uses the OpenSearch Dashboards sample e-commerce data. To add that sample data, log in to OpenSearch Dashboards, choose Home and Try our sample data. For Sample eCommerce orders, choose Add data.

Then run a search:

  1. GET opensearch_dashboards_sample_data_ecommerce/_search

Example response

  1. {
  2. "took": 23,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4675,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "opensearch_dashboards_sample_data_ecommerce",
  19. "_type": "_doc",
  20. "_id": "jlMlwXcBQVLeQPrkC_kQ",
  21. "_score": 1,
  22. "_source": {
  23. "category": [
  24. "Women's Clothing",
  25. "Women's Accessories"
  26. ],
  27. "currency": "EUR",
  28. "customer_first_name": "Selena",
  29. "customer_full_name": "Selena Mullins",
  30. "customer_gender": "FEMALE",
  31. "customer_id": 42,
  32. "customer_last_name": "Mullins",
  33. "customer_phone": "",
  34. "day_of_week": "Saturday",
  35. "day_of_week_i": 5,
  36. "email": "selena@mullins-family.zzz",
  37. "manufacturer": [
  38. "Tigress Enterprises"
  39. ],
  40. "order_date": "2021-02-27T03:56:10+00:00",
  41. "order_id": 581553,
  42. "products": [
  43. {
  44. "base_price": 24.99,
  45. "discount_percentage": 0,
  46. "quantity": 1,
  47. "manufacturer": "Tigress Enterprises",
  48. "tax_amount": 0,
  49. "product_id": 19240,
  50. "category": "Women's Clothing",
  51. "sku": "ZO0064500645",
  52. "taxless_price": 24.99,
  53. "unit_discount_amount": 0,
  54. "min_price": 12.99,
  55. "_id": "sold_product_581553_19240",
  56. "discount_amount": 0,
  57. "created_on": "2016-12-24T03:56:10+00:00",
  58. "product_name": "Blouse - port royal",
  59. "price": 24.99,
  60. "taxful_price": 24.99,
  61. "base_unit_price": 24.99
  62. },
  63. {
  64. "base_price": 10.99,
  65. "discount_percentage": 0,
  66. "quantity": 1,
  67. "manufacturer": "Tigress Enterprises",
  68. "tax_amount": 0,
  69. "product_id": 17221,
  70. "category": "Women's Accessories",
  71. "sku": "ZO0085200852",
  72. "taxless_price": 10.99,
  73. "unit_discount_amount": 0,
  74. "min_price": 5.06,
  75. "_id": "sold_product_581553_17221",
  76. "discount_amount": 0,
  77. "created_on": "2016-12-24T03:56:10+00:00",
  78. "product_name": "Snood - rose",
  79. "price": 10.99,
  80. "taxful_price": 10.99,
  81. "base_unit_price": 10.99
  82. }
  83. ],
  84. "sku": [
  85. "ZO0064500645",
  86. "ZO0085200852"
  87. ],
  88. "taxful_total_price": 35.98,
  89. "taxless_total_price": 35.98,
  90. "total_quantity": 2,
  91. "total_unique_products": 2,
  92. "type": "order",
  93. "user": "selena",
  94. "geoip": {
  95. "country_iso_code": "MA",
  96. "location": {
  97. "lon": -8,
  98. "lat": 31.6
  99. },
  100. "region_name": "Marrakech-Tensift-Al Haouz",
  101. "continent_name": "Africa",
  102. "city_name": "Marrakesh"
  103. },
  104. "event": {
  105. "dataset": "sample_ecommerce"
  106. }
  107. }
  108. }
  109. ]
  110. }
  111. }
  112. ...

Create an index rollup job. This example picks the order_date, customer_gender, geoip.city_name, geoip.region_name, and day_of_week fields and rolls them into an example_rollup target index:

  1. PUT _plugins/_rollup/jobs/example
  2. {
  3. "rollup": {
  4. "enabled": true,
  5. "schedule": {
  6. "interval": {
  7. "period": 1,
  8. "unit": "Minutes",
  9. "start_time": 1602100553
  10. }
  11. },
  12. "last_updated_time": 1602100553,
  13. "description": "An example policy that rolls up the sample ecommerce data",
  14. "source_index": "opensearch_dashboards_sample_data_ecommerce",
  15. "target_index": "example_rollup",
  16. "page_size": 1000,
  17. "delay": 0,
  18. "continuous": false,
  19. "dimensions": [
  20. {
  21. "date_histogram": {
  22. "source_field": "order_date",
  23. "fixed_interval": "60m",
  24. "timezone": "America/Los_Angeles"
  25. }
  26. },
  27. {
  28. "terms": {
  29. "source_field": "customer_gender"
  30. }
  31. },
  32. {
  33. "terms": {
  34. "source_field": "geoip.city_name"
  35. }
  36. },
  37. {
  38. "terms": {
  39. "source_field": "geoip.region_name"
  40. }
  41. },
  42. {
  43. "terms": {
  44. "source_field": "day_of_week"
  45. }
  46. }
  47. ],
  48. "metrics": [
  49. {
  50. "source_field": "taxless_total_price",
  51. "metrics": [
  52. {
  53. "avg": {}
  54. },
  55. {
  56. "sum": {}
  57. },
  58. {
  59. "max": {}
  60. },
  61. {
  62. "min": {}
  63. },
  64. {
  65. "value_count": {}
  66. }
  67. ]
  68. },
  69. {
  70. "source_field": "total_quantity",
  71. "metrics": [
  72. {
  73. "avg": {}
  74. },
  75. {
  76. "max": {}
  77. }
  78. ]
  79. }
  80. ]
  81. }
  82. }

You can query the example_rollup index for the terms aggregations on the fields set up in the rollup job. You get back the same response that you would on the original opensearch_dashboards_sample_data_ecommerce source index:

  1. POST example_rollup/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "bool": {
  6. "must": {"term": { "geoip.region_name": "California" } }
  7. }
  8. },
  9. "aggregations": {
  10. "daily_numbers": {
  11. "terms": {
  12. "field": "day_of_week"
  13. },
  14. "aggs": {
  15. "per_city": {
  16. "terms": {
  17. "field": "geoip.city_name"
  18. },
  19. "aggregations": {
  20. "average quantity": {
  21. "avg": {
  22. "field": "total_quantity"
  23. }
  24. }
  25. }
  26. },
  27. "total_revenue": {
  28. "sum": {
  29. "field": "taxless_total_price"
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }

Example response

  1. {
  2. "took" : 14,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 281,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "daily_numbers" : {
  20. "doc_count_error_upper_bound" : 0,
  21. "sum_other_doc_count" : 0,
  22. "buckets" : [
  23. {
  24. "key" : "Friday",
  25. "doc_count" : 59,
  26. "total_revenue" : {
  27. "value" : 4858.84375
  28. },
  29. "per_city" : {
  30. "doc_count_error_upper_bound" : 0,
  31. "sum_other_doc_count" : 0,
  32. "buckets" : [
  33. {
  34. "key" : "Los Angeles",
  35. "doc_count" : 59,
  36. "average quantity" : {
  37. "value" : 2.305084745762712
  38. }
  39. }
  40. ]
  41. }
  42. },
  43. {
  44. "key" : "Saturday",
  45. "doc_count" : 46,
  46. "total_revenue" : {
  47. "value" : 3547.203125
  48. },
  49. "per_city" : {
  50. "doc_count_error_upper_bound" : 0,
  51. "sum_other_doc_count" : 0,
  52. "buckets" : [
  53. {
  54. "key" : "Los Angeles",
  55. "doc_count" : 46,
  56. "average quantity" : {
  57. "value" : 2.260869565217391
  58. }
  59. }
  60. ]
  61. }
  62. },
  63. {
  64. "key" : "Tuesday",
  65. "doc_count" : 45,
  66. "total_revenue" : {
  67. "value" : 3983.28125
  68. },
  69. "per_city" : {
  70. "doc_count_error_upper_bound" : 0,
  71. "sum_other_doc_count" : 0,
  72. "buckets" : [
  73. {
  74. "key" : "Los Angeles",
  75. "doc_count" : 45,
  76. "average quantity" : {
  77. "value" : 2.2888888888888888
  78. }
  79. }
  80. ]
  81. }
  82. },
  83. {
  84. "key" : "Sunday",
  85. "doc_count" : 44,
  86. "total_revenue" : {
  87. "value" : 3308.1640625
  88. },
  89. "per_city" : {
  90. "doc_count_error_upper_bound" : 0,
  91. "sum_other_doc_count" : 0,
  92. "buckets" : [
  93. {
  94. "key" : "Los Angeles",
  95. "doc_count" : 44,
  96. "average quantity" : {
  97. "value" : 2.090909090909091
  98. }
  99. }
  100. ]
  101. }
  102. },
  103. {
  104. "key" : "Thursday",
  105. "doc_count" : 40,
  106. "total_revenue" : {
  107. "value" : 2876.125
  108. },
  109. "per_city" : {
  110. "doc_count_error_upper_bound" : 0,
  111. "sum_other_doc_count" : 0,
  112. "buckets" : [
  113. {
  114. "key" : "Los Angeles",
  115. "doc_count" : 40,
  116. "average quantity" : {
  117. "value" : 2.3
  118. }
  119. }
  120. ]
  121. }
  122. },
  123. {
  124. "key" : "Monday",
  125. "doc_count" : 38,
  126. "total_revenue" : {
  127. "value" : 2673.453125
  128. },
  129. "per_city" : {
  130. "doc_count_error_upper_bound" : 0,
  131. "sum_other_doc_count" : 0,
  132. "buckets" : [
  133. {
  134. "key" : "Los Angeles",
  135. "doc_count" : 38,
  136. "average quantity" : {
  137. "value" : 2.1578947368421053
  138. }
  139. }
  140. ]
  141. }
  142. },
  143. {
  144. "key" : "Wednesday",
  145. "doc_count" : 38,
  146. "total_revenue" : {
  147. "value" : 3202.453125
  148. },
  149. "per_city" : {
  150. "doc_count_error_upper_bound" : 0,
  151. "sum_other_doc_count" : 0,
  152. "buckets" : [
  153. {
  154. "key" : "Los Angeles",
  155. "doc_count" : 38,
  156. "average quantity" : {
  157. "value" : 2.236842105263158
  158. }
  159. }
  160. ]
  161. }
  162. }
  163. ]
  164. }
  165. }
  166. }

The doc_count field

The doc_count field in bucket aggregations contains the number of documents collected in each bucket. When calculating the bucket’s doc_count, the number of documents is incremented by the number of the pre-aggregated documents in each summary document. The doc_count returned from rollup searches represents the total number of matching documents from the source index. The document count for each bucket is the same whether you search the source index or the rollup target index.

Query string queries

To take advantage of shorter and more easily written strings in Query DSL, you can use query strings to simplify search queries in rollup indexes. To use query strings, add the following fields to your rollup search request:

  1. "query": {
  2. "query_string": {
  3. "query": "field_name:field_value"
  4. }
  5. }

The following example uses a query string with a * wildcard operator to search inside a rollup index called my_server_logs_rollup:

  1. GET my_server_logs_rollup/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "query_string": {
  6. "query": "email* OR inventory",
  7. "default_field": "service_name"
  8. }
  9. },
  10. "aggs": {
  11. "service_name": {
  12. "terms": {
  13. "field": "service_name"
  14. },
  15. "aggs": {
  16. "region": {
  17. "terms": {
  18. "field": "region"
  19. },
  20. "aggs": {
  21. "average quantity": {
  22. "avg": {
  23. "field": "cpu_usage"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }

For more information about query string query parameters, see Query string query.

Dynamic target index

In ISM rollup, the target_index field may contain a template that is compiled at the time of each rollup indexing. For example, if you specify the target_index field as rollup_ndx-{{ctx.source_index}}, the source index log-000001 will roll up into a target index rollup_ndx-log-000001. This allows you to roll up data into multiple time-based indexes, with one rollup job created for each source index.

The source_index parameter in {{ctx.source_index}} cannot contain wildcards.

Searching multiple rollup indexes

When data is rolled up into multiple target indexes, you can run one search across all of the rollup indexes. To search multiple target indexes that have the same rollup, specify the index names as a comma-separated list or a wildcard pattern. For example, with target_index as rollup_ndx-{{ctx.source_index}} and source indexes that start with log, specify the rollup_ndx-log* pattern. Or, to search for rolled up log-000001 and log-000002 indexes, specify the rollup_ndx-log-000001,rollup_ndx-log-000002 list.

You cannot search a mix of rollup and non-rollup indexes with the same query.

Example

The following example demonstrates the doc_count field, dynamic index names, and searching multiple rollup indexes with the same rollup.

Step 1: Add an index template for ISM to manage the rolling over of the indexes aliased by log:

  1. PUT _index_template/ism_rollover
  2. {
  3. "index_patterns": ["log*"],
  4. "template": {
  5. "settings": {
  6. "plugins.index_state_management.rollover_alias": "log"
  7. }
  8. }
  9. }

Step 2: Set up an ISM rollover policy to roll over any index whose name starts with log* after one document is uploaded to it, and then roll up the individual backing index. The target index name is dynamically generated from the source index name by prepending the string rollup_ndx- to the source index name.

  1. PUT _plugins/_ism/policies/rollover_policy
  2. {
  3. "policy": {
  4. "description": "Example rollover policy.",
  5. "default_state": "rollover",
  6. "states": [
  7. {
  8. "name": "rollover",
  9. "actions": [
  10. {
  11. "rollover": {
  12. "min_doc_count": 1
  13. }
  14. }
  15. ],
  16. "transitions": [
  17. {
  18. "state_name": "rp"
  19. }
  20. ]
  21. },
  22. {
  23. "name": "rp",
  24. "actions": [
  25. {
  26. "rollup": {
  27. "ism_rollup": {
  28. "target_index": "rollup_ndx-{{ctx.source_index}}",
  29. "description": "Example rollup job",
  30. "page_size": 200,
  31. "dimensions": [
  32. {
  33. "date_histogram": {
  34. "source_field": "ts",
  35. "fixed_interval": "60m",
  36. "timezone": "America/Los_Angeles"
  37. }
  38. },
  39. {
  40. "terms": {
  41. "source_field": "message.keyword"
  42. }
  43. }
  44. ],
  45. "metrics": [
  46. {
  47. "source_field": "msg_size",
  48. "metrics": [
  49. {
  50. "sum": {}
  51. }
  52. ]
  53. }
  54. ]
  55. }
  56. }
  57. }
  58. ],
  59. "transitions": []
  60. }
  61. ],
  62. "ism_template": {
  63. "index_patterns": ["log*"],
  64. "priority": 100
  65. }
  66. }
  67. }

Step 3: Create an index named log-000001 and set up an alias log for it.

  1. PUT log-000001
  2. {
  3. "aliases": {
  4. "log": {
  5. "is_write_index": true
  6. }
  7. }
  8. }

Step 4: Index four documents into the index created above. Two of the documents have the message “Success”, and two have the message “Error”.

  1. POST log/_doc?refresh=true
  2. {
  3. "ts" : "2022-08-26T09:28:48-04:00",
  4. "message": "Success",
  5. "msg_size": 10
  6. }
  1. POST log/_doc?refresh=true
  2. {
  3. "ts" : "2022-08-26T10:06:25-04:00",
  4. "message": "Error",
  5. "msg_size": 20
  6. }
  1. POST log/_doc?refresh=true
  2. {
  3. "ts" : "2022-08-26T10:23:54-04:00",
  4. "message": "Error",
  5. "msg_size": 30
  6. }
  1. POST log/_doc?refresh=true
  2. {
  3. "ts" : "2022-08-26T10:53:41-04:00",
  4. "message": "Success",
  5. "msg_size": 40
  6. }

Once you index the first document, the rollover action is executed. This action creates the index log-000002 with rollover_policy attached to it. Then the rollup action is executed, which creates the rollup index rollup_ndx-log-000001.

To monitor the status of rollover and rollup index creation, you can use the ISM explain API: GET _plugins/_ism/explain

Step 5: Search the rollup index.

  1. GET rollup_ndx-log-*/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "match_all": {}
  6. },
  7. "aggregations": {
  8. "message_numbers": {
  9. "terms": {
  10. "field": "message.keyword"
  11. },
  12. "aggs": {
  13. "per_message": {
  14. "terms": {
  15. "field": "message.keyword"
  16. },
  17. "aggregations": {
  18. "sum_message": {
  19. "sum": {
  20. "field": "msg_size"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }

The response contains two buckets, “Error” and “Success”, and the document count for each bucket is 2:

  1. {
  2. "took" : 30,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 4,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "message_numbers" : {
  20. "doc_count_error_upper_bound" : 0,
  21. "sum_other_doc_count" : 0,
  22. "buckets" : [
  23. {
  24. "key" : "Success",
  25. "doc_count" : 2,
  26. "per_message" : {
  27. "doc_count_error_upper_bound" : 0,
  28. "sum_other_doc_count" : 0,
  29. "buckets" : [
  30. {
  31. "key" : "Success",
  32. "doc_count" : 2,
  33. "sum_message" : {
  34. "value" : 50.0
  35. }
  36. }
  37. ]
  38. }
  39. },
  40. {
  41. "key" : "Error",
  42. "doc_count" : 2,
  43. "per_message" : {
  44. "doc_count_error_upper_bound" : 0,
  45. "sum_other_doc_count" : 0,
  46. "buckets" : [
  47. {
  48. "key" : "Error",
  49. "doc_count" : 2,
  50. "sum_message" : {
  51. "value" : 50.0
  52. }
  53. }
  54. ]
  55. }
  56. }
  57. ]
  58. }
  59. }
  60. }

Index codec considerations

For index codec considerations, see Index codecs.