moving-percentiles-aggregation.asciidoc 5.5 KB

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