Getting started with rollups

This functionality is experimental and may be changed or removed completely in a future release. Elastic will take a best effort approach to fix any issues, but experimental features are not subject to the support SLA of official GA features.

To use the Rollup feature, you need to create one or more “Rollup Jobs”. These jobs run continuously in the background and rollup the index or indices that you specify, placing the rolled documents in a secondary index (also of your choosing).

Imagine you have a series of daily indices that hold sensor data (sensor-2017-01-01, sensor-2017-01-02, etc). A sample document might look like this:

  1. {
  2. "timestamp": 1516729294000,
  3. "temperature": 200,
  4. "voltage": 5.2,
  5. "node": "a"
  6. }

Creating a rollup job

We’d like to rollup these documents into hourly summaries, which will allow us to generate reports and dashboards with any time interval one hour or greater. A rollup job might look like this:

  1. PUT _rollup/job/sensor
  2. {
  3. "index_pattern": "sensor-*",
  4. "rollup_index": "sensor_rollup",
  5. "cron": "*/30 * * * * ?",
  6. "page_size": 1000,
  7. "groups": {
  8. "date_histogram": {
  9. "field": "timestamp",
  10. "fixed_interval": "60m"
  11. },
  12. "terms": {
  13. "fields": [ "node" ]
  14. }
  15. },
  16. "metrics": [
  17. {
  18. "field": "temperature",
  19. "metrics": [ "min", "max", "sum" ]
  20. },
  21. {
  22. "field": "voltage",
  23. "metrics": [ "avg" ]
  24. }
  25. ]
  26. }

We give the job the ID of “sensor” (in the url: PUT _rollup/job/sensor), and tell it to rollup the index pattern "sensor-*". This job will find and rollup any index that matches that pattern. Rollup summaries are then stored in the "sensor_rollup" index.

The cron parameter controls when and how often the job activates. When a rollup job’s cron schedule triggers, it will begin rolling up from where it left off after the last activation. So if you configure the cron to run every 30 seconds, the job will process the last 30 seconds worth of data that was indexed into the sensor-* indices.

If instead the cron was configured to run once a day at midnight, the job would process the last 24 hours worth of data. The choice is largely preference, based on how “realtime” you want the rollups, and if you wish to process continuously or move it to off-peak hours.

Next, we define a set of groups. Essentially, we are defining the dimensions that we wish to pivot on at a later date when querying the data. The grouping in this job allows us to use date_histogram aggregations on the timestamp field, rolled up at hourly intervals. It also allows us to run terms aggregations on the node field.

Date histogram interval vs cron schedule

You’ll note that the job’s cron is configured to run every 30 seconds, but the date_histogram is configured to rollup at 60 minute intervals. How do these relate?

The date_histogram controls the granularity of the saved data. Data will be rolled up into hourly intervals, and you will be unable to query with finer granularity. The cron simply controls when the process looks for new data to rollup. Every 30 seconds it will see if there is a new hour’s worth of data and roll it up. If not, the job goes back to sleep.

Often, it doesn’t make sense to define such a small cron (30s) on a large interval (1h), because the majority of the activations will simply go back to sleep. But there’s nothing wrong with it either, the job will do the right thing.

After defining which groups should be generated for the data, you next configure which metrics should be collected. By default, only the doc_counts are collected for each group. To make rollup useful, you will often add metrics like averages, mins, maxes, etc. In this example, the metrics are fairly straightforward: we want to save the min/max/sum of the temperature field, and the average of the voltage field.

Averages aren’t composable?!

If you’ve worked with rollups before, you may be cautious around averages. If an average is saved for a 10 minute interval, it usually isn’t useful for larger intervals. You cannot average six 10-minute averages to find a hourly average; the average of averages is not equal to the total average.

For this reason, other systems tend to either omit the ability to average or store the average at multiple intervals to support more flexible querying.

Instead, the data rollup features save the count and sum for the defined time interval. This allows us to reconstruct the average at any interval greater-than or equal to the defined interval. This gives maximum flexibility for minimal storage costs…​ and you don’t have to worry about average accuracies (no average of averages here!)

For more details about the job syntax, see Create rollup jobs.

After you execute the above command and create the job, you’ll receive the following response:

  1. {
  2. "acknowledged": true
  3. }

Starting the job

After the job is created, it will be sitting in an inactive state. Jobs need to be started before they begin processing data (this allows you to stop them later as a way to temporarily pause, without deleting the configuration).

To start the job, execute this command:

  1. POST _rollup/job/sensor/_start

Searching the rolled results

After the job has run and processed some data, we can use the Rollup search endpoint to do some searching. The Rollup feature is designed so that you can use the same Query DSL syntax that you are accustomed to…​ it just happens to run on the rolled up data instead.

For example, take this query:

  1. GET /sensor_rollup/_rollup_search
  2. {
  3. "size": 0,
  4. "aggregations": {
  5. "max_temperature": {
  6. "max": {
  7. "field": "temperature"
  8. }
  9. }
  10. }
  11. }

It’s a simple aggregation that calculates the maximum of the temperature field. But you’ll notice that is is being sent to the sensor_rollup index instead of the raw sensor-* indices. And you’ll also notice that it is using the _rollup_search endpoint. Otherwise the syntax is exactly as you’d expect.

If you were to execute that query, you’d receive a result that looks like a normal aggregation response:

  1. {
  2. "took" : 102,
  3. "timed_out" : false,
  4. "terminated_early" : false,
  5. "_shards" : ... ,
  6. "hits" : {
  7. "total" : {
  8. "value": 0,
  9. "relation": "eq"
  10. },
  11. "max_score" : 0.0,
  12. "hits" : [ ]
  13. },
  14. "aggregations" : {
  15. "max_temperature" : {
  16. "value" : 202.0
  17. }
  18. }
  19. }

The only notable difference is that Rollup search results have zero hits, because we aren’t really searching the original, live data any more. Otherwise it’s identical syntax.

There are a few interesting takeaways here. Firstly, even though the data was rolled up with hourly intervals and partitioned by node name, the query we ran is just calculating the max temperature across all documents. The groups that were configured in the job are not mandatory elements of a query, they are just extra dimensions you can partition on. Second, the request and response syntax is nearly identical to normal DSL, making it easy to integrate into dashboards and applications.

Finally, we can use those grouping fields we defined to construct a more complicated query:

  1. GET /sensor_rollup/_rollup_search
  2. {
  3. "size": 0,
  4. "aggregations": {
  5. "timeline": {
  6. "date_histogram": {
  7. "field": "timestamp",
  8. "fixed_interval": "7d"
  9. },
  10. "aggs": {
  11. "nodes": {
  12. "terms": {
  13. "field": "node"
  14. },
  15. "aggs": {
  16. "max_temperature": {
  17. "max": {
  18. "field": "temperature"
  19. }
  20. },
  21. "avg_voltage": {
  22. "avg": {
  23. "field": "voltage"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }

Which returns a corresponding response:

  1. {
  2. "took" : 93,
  3. "timed_out" : false,
  4. "terminated_early" : false,
  5. "_shards" : ... ,
  6. "hits" : {
  7. "total" : {
  8. "value": 0,
  9. "relation": "eq"
  10. },
  11. "max_score" : 0.0,
  12. "hits" : [ ]
  13. },
  14. "aggregations" : {
  15. "timeline" : {
  16. "meta" : { },
  17. "buckets" : [
  18. {
  19. "key_as_string" : "2018-01-18T00:00:00.000Z",
  20. "key" : 1516233600000,
  21. "doc_count" : 6,
  22. "nodes" : {
  23. "doc_count_error_upper_bound" : 0,
  24. "sum_other_doc_count" : 0,
  25. "buckets" : [
  26. {
  27. "key" : "a",
  28. "doc_count" : 2,
  29. "max_temperature" : {
  30. "value" : 202.0
  31. },
  32. "avg_voltage" : {
  33. "value" : 5.1499998569488525
  34. }
  35. },
  36. {
  37. "key" : "b",
  38. "doc_count" : 2,
  39. "max_temperature" : {
  40. "value" : 201.0
  41. },
  42. "avg_voltage" : {
  43. "value" : 5.700000047683716
  44. }
  45. },
  46. {
  47. "key" : "c",
  48. "doc_count" : 2,
  49. "max_temperature" : {
  50. "value" : 202.0
  51. },
  52. "avg_voltage" : {
  53. "value" : 4.099999904632568
  54. }
  55. }
  56. ]
  57. }
  58. }
  59. ]
  60. }
  61. }
  62. }

In addition to being more complicated (date histogram and a terms aggregation, plus an additional average metric), you’ll notice the date_histogram uses a 7d interval instead of 60m.

Conclusion

This quickstart should have provided a concise overview of the core functionality that Rollup exposes. There are more tips and things to consider when setting up Rollups, which you can find throughout the rest of this section. You may also explore the REST API for an overview of what is available.