moving-percentiles-aggregation.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. [role="xpack"]
  2. [[search-aggregations-pipeline-moving-percentiles-aggregation]]
  3. === Moving percentiles aggregation
  4. ++++
  5. <titleabbrev>Moving percentiles</titleabbrev>
  6. ++++
  7. Given an ordered series of <<search-aggregations-metrics-percentile-aggregation, percentiles>>, the Moving Percentile aggregation
  8. will slide a window across those percentiles and allow the user to compute the cumulative percentile.
  9. This is conceptually very similar to the <<search-aggregations-pipeline-movfn-aggregation, Moving Function>> pipeline aggregation,
  10. except it works on the percentiles sketches instead of the actual buckets values.
  11. ==== Syntax
  12. A `moving_percentiles` aggregation looks like this in isolation:
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "moving_percentiles": {
  17. "buckets_path": "the_percentile",
  18. "window": 10
  19. }
  20. }
  21. --------------------------------------------------
  22. // NOTCONSOLE
  23. [[moving-percentiles-params]]
  24. .`moving_percentiles` Parameters
  25. [options="header"]
  26. |===
  27. |Parameter Name |Description |Required |Default Value
  28. |`buckets_path` |Path to the percentile of interest (see <<buckets-path-syntax, `buckets_path` Syntax>> for more details |Required |
  29. |`window` |The size of window to "slide" across the histogram. |Required |
  30. |`shift` |<<shift-parameter, Shift>> of window position. |Optional | 0
  31. |===
  32. `moving_percentiles` aggregations must be embedded inside of a `histogram` or `date_histogram` aggregation. They can be
  33. embedded like any other metric aggregation:
  34. [source,console]
  35. --------------------------------------------------
  36. POST /_search
  37. {
  38. "size": 0,
  39. "aggs": {
  40. "my_date_histo": { <1>
  41. "date_histogram": {
  42. "field": "date",
  43. "calendar_interval": "1M"
  44. },
  45. "aggs": {
  46. "the_percentile": { <2>
  47. "percentiles": {
  48. "field": "price",
  49. "percents": [ 1.0, 99.0 ]
  50. }
  51. },
  52. "the_movperc": {
  53. "moving_percentiles": {
  54. "buckets_path": "the_percentile", <3>
  55. "window": 10
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. --------------------------------------------------
  63. // TEST[setup:sales]
  64. <1> A `date_histogram` named "my_date_histo" is constructed on the "timestamp" field, with one-day intervals
  65. <2> A `percentile` metric is used to calculate the percentiles of a field.
  66. <3> Finally, we specify a `moving_percentiles` aggregation which uses "the_percentile" sketch as its input.
  67. Moving percentiles are built by first specifying a `histogram` or `date_histogram` over a field. You then add
  68. a percentile metric inside of that histogram. Finally, the `moving_percentiles` is embedded inside the histogram.
  69. The `buckets_path` parameter is then used to "point" at the percentiles aggregation inside of the histogram (see
  70. <<buckets-path-syntax>> for a description of the syntax for `buckets_path`).
  71. And the following may be the response:
  72. [source,console-result]
  73. --------------------------------------------------
  74. {
  75. "took": 11,
  76. "timed_out": false,
  77. "_shards": ...,
  78. "hits": ...,
  79. "aggregations": {
  80. "my_date_histo": {
  81. "buckets": [
  82. {
  83. "key_as_string": "2015/01/01 00:00:00",
  84. "key": 1420070400000,
  85. "doc_count": 3,
  86. "the_percentile": {
  87. "values": {
  88. "1.0": 150.0,
  89. "99.0": 200.0
  90. }
  91. }
  92. },
  93. {
  94. "key_as_string": "2015/02/01 00:00:00",
  95. "key": 1422748800000,
  96. "doc_count": 2,
  97. "the_percentile": {
  98. "values": {
  99. "1.0": 10.0,
  100. "99.0": 50.0
  101. }
  102. },
  103. "the_movperc": {
  104. "values": {
  105. "1.0": 150.0,
  106. "99.0": 200.0
  107. }
  108. }
  109. },
  110. {
  111. "key_as_string": "2015/03/01 00:00:00",
  112. "key": 1425168000000,
  113. "doc_count": 2,
  114. "the_percentile": {
  115. "values": {
  116. "1.0": 175.0,
  117. "99.0": 200.0
  118. }
  119. },
  120. "the_movperc": {
  121. "values": {
  122. "1.0": 10.0,
  123. "99.0": 200.0
  124. }
  125. }
  126. }
  127. ]
  128. }
  129. }
  130. }
  131. --------------------------------------------------
  132. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  133. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  134. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  135. The output format of the `moving_percentiles` aggregation is inherited from the format of the referenced
  136. <<search-aggregations-metrics-percentile-aggregation,`percentiles`>> aggregation.
  137. Moving percentiles pipeline aggregations always run with `skip` gap policy.
  138. [[moving-percentiles-shift-parameter]]
  139. ==== shift parameter
  140. By default (with `shift = 0`), the window that is offered for calculation is the last `n` values excluding the current bucket.
  141. Increasing `shift` by 1 moves starting window position by `1` to the right.
  142. - To include current bucket to the window, use `shift = 1`.
  143. - For center alignment (`n / 2` values before and after the current bucket), use `shift = window / 2`.
  144. - For right alignment (`n` values after the current bucket), use `shift = window`.
  145. If either of window edges moves outside the borders of data series, the window shrinks to include available values only.