time-series-aggregation.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. [[search-aggregations-bucket-time-series-aggregation]]
  2. === Time series aggregation
  3. ++++
  4. <titleabbrev>Time series</titleabbrev>
  5. ++++
  6. preview::[]
  7. The time series aggregation queries data created using a time series index. This is typically data such as metrics
  8. or other data streams with a time component, and requires creating an index using the time series mode.
  9. //////////////////////////
  10. Creating a time series mapping
  11. To create an index with the time series mapping, specify "mode" as "time_series" in the index settings,
  12. "routing_path" specifying the a list of time series fields, and a start and end time for the series. Each of the
  13. "routing_path" fields must be keyword fields with "time_series_dimension" set to true. Additionally, add a
  14. date field used as the timestamp.
  15. [source,js]
  16. --------------------------------------------------
  17. PUT /my-time-series-index
  18. {
  19. "settings": {
  20. "index": {
  21. "number_of_shards": 3,
  22. "number_of_replicas": 2,
  23. "mode": "time_series",
  24. "routing_path": ["key"],
  25. "time_series": {
  26. "start_time": "2022-01-01T00:00:00Z",
  27. "end_time": "2023-01-01T00:00:00Z"
  28. }
  29. }
  30. },
  31. "mappings": {
  32. "properties": {
  33. "key": {
  34. "type": "keyword",
  35. "time_series_dimension": true
  36. },
  37. "@timestamp": {
  38. "type": "date"
  39. }
  40. }
  41. }
  42. }
  43. -------------------------------------------------
  44. // NOTCONSOLE
  45. //////////////////////////
  46. Data can be added to the time series index like other indices:
  47. [source,js]
  48. --------------------------------------------------
  49. PUT /my-time-series-index-0/_bulk
  50. { "index": {} }
  51. { "key": "a", "val": 1, "@timestamp": "2022-01-01T00:00:10Z" }
  52. { "index": {}}
  53. { "key": "a", "val": 2, "@timestamp": "2022-01-02T00:00:00Z" }
  54. { "index": {} }
  55. { "key": "b", "val": 2, "@timestamp": "2022-01-01T00:00:10Z" }
  56. { "index": {}}
  57. { "key": "b", "val": 3, "@timestamp": "2022-01-02T00:00:00Z" }
  58. --------------------------------------------------
  59. // NOTCONSOLE
  60. //////////////////////////
  61. To perform a time series aggregation, specify "time_series" as the aggregation type. When the boolean "keyed"
  62. is true, each bucket is given a unique key.
  63. [source,js,id=time-series-aggregation-example]
  64. --------------------------------------------------
  65. GET /_search
  66. {
  67. "aggs": {
  68. "ts": {
  69. "time_series": { "keyed": false }
  70. }
  71. }
  72. }
  73. --------------------------------------------------
  74. // NOTCONSOLE
  75. //////////////////////////
  76. This will return all results in the time series, however a more typical query will use sub aggregations to reduce the
  77. date returned to something more relevant.
  78. [[search-aggregations-bucket-time-series-aggregation-size]]
  79. ==== Size
  80. By default, `time series` aggregations return 10000 results. The "size" parameter can be used to limit the results
  81. further. Alternatively, using sub aggregations can limit the amount of values returned as a time series aggregation.
  82. [[search-aggregations-bucket-time-series-aggregation-keyed]]
  83. ==== Keyed
  84. The `keyed` parameter determines if buckets are returned as a map with unique keys per bucket. By default with `keyed`
  85. set to false, buckets are returned as an array.