derivative-aggregation.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. [[search-aggregations-reducer-derivative-aggregation]]
  2. === Derivative Aggregation
  3. A parent reducer aggregation which calculates the derivative of a specified metric in a parent histogram (or date_histogram)
  4. aggregation. The specified metric must be numeric and the enclosing histogram must have `min_doc_count` set to `0` (default
  5. for `histogram` aggregations).
  6. ==== Syntax
  7. A `derivative` aggregation looks like this in isolation:
  8. [source,js]
  9. --------------------------------------------------
  10. {
  11. "derivative": {
  12. "buckets_path": "the_sum"
  13. }
  14. }
  15. --------------------------------------------------
  16. .`derivative` Parameters
  17. |===
  18. |Parameter Name |Description |Required |Default Value
  19. |`buckets_path` |Path to the metric of interest (see <<bucket-path-syntax, `buckets_path` Syntax>> for more details |Required |
  20. |===
  21. ==== First Order Derivative
  22. The following snippet calculates the derivative of the total monthly `sales`:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. "aggs" : {
  27. "sales_per_month" : {
  28. "date_histogram" : {
  29. "field" : "date",
  30. "interval" : "month"
  31. },
  32. "aggs": {
  33. "sales": {
  34. "sum": {
  35. "field": "price"
  36. }
  37. },
  38. "sales_deriv": {
  39. "derivative": {
  40. "buckets_paths": "sales" <1>
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. <1> `bucket_paths` instructs this derivative aggregation to use the output of the `sales` aggregation for the derivative
  49. And the following may be the response:
  50. [source,js]
  51. --------------------------------------------------
  52. {
  53. "aggregations": {
  54. "sales_per_month": {
  55. "buckets": [
  56. {
  57. "key_as_string": "2015/01/01 00:00:00",
  58. "key": 1420070400000,
  59. "doc_count": 3,
  60. "sales": {
  61. "value": 550
  62. } <1>
  63. },
  64. {
  65. "key_as_string": "2015/02/01 00:00:00",
  66. "key": 1422748800000,
  67. "doc_count": 2,
  68. "sales": {
  69. "value": 60
  70. },
  71. "sales_deriv": {
  72. "value": -490 <2>
  73. }
  74. },
  75. {
  76. "key_as_string": "2015/03/01 00:00:00",
  77. "key": 1425168000000,
  78. "doc_count": 2, <3>
  79. "sales": {
  80. "value": 375
  81. },
  82. "sales_deriv": {
  83. "value": 315
  84. }
  85. }
  86. ]
  87. }
  88. }
  89. }
  90. --------------------------------------------------
  91. <1> No derivative for the first bucket since we need at least 2 data points to calculate the derivative
  92. <2> Derivative value units are implicitly defined by the `sales` aggregation and the parent histogram so in this case the units
  93. would be $/month assuming the `price` field has units of $.
  94. <3> The number of documents in the bucket are represented by the `doc_count` f
  95. ==== Second Order Derivative
  96. A second order derivative can be calculated by chaining the derivative reducer aggregation onto the result of another derivative
  97. reducer aggregation as in the following example which will calculate both the first and the second order derivative of the total
  98. monthly sales:
  99. [source,js]
  100. --------------------------------------------------
  101. {
  102. "aggs" : {
  103. "sales_per_month" : {
  104. "date_histogram" : {
  105. "field" : "date",
  106. "interval" : "month"
  107. },
  108. "aggs": {
  109. "sales": {
  110. "sum": {
  111. "field": "price"
  112. }
  113. },
  114. "sales_deriv": {
  115. "derivative": {
  116. "buckets_paths": "sales"
  117. }
  118. },
  119. "sales_2nd_deriv": {
  120. "derivative": {
  121. "buckets_paths": "sales_deriv" <1>
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. <1> `bucket_paths` for the second derivative points to the name of the first derivative
  130. And the following may be the response:
  131. [source,js]
  132. --------------------------------------------------
  133. {
  134. "aggregations": {
  135. "sales_per_month": {
  136. "buckets": [
  137. {
  138. "key_as_string": "2015/01/01 00:00:00",
  139. "key": 1420070400000,
  140. "doc_count": 3,
  141. "sales": {
  142. "value": 550
  143. } <1>
  144. },
  145. {
  146. "key_as_string": "2015/02/01 00:00:00",
  147. "key": 1422748800000,
  148. "doc_count": 2,
  149. "sales": {
  150. "value": 60
  151. },
  152. "sales_deriv": {
  153. "value": -490
  154. } <1>
  155. },
  156. {
  157. "key_as_string": "2015/03/01 00:00:00",
  158. "key": 1425168000000,
  159. "doc_count": 2,
  160. "sales": {
  161. "value": 375
  162. },
  163. "sales_deriv": {
  164. "value": 315
  165. },
  166. "sales_2nd_deriv": {
  167. "value": 805
  168. }
  169. }
  170. ]
  171. }
  172. }
  173. }
  174. --------------------------------------------------
  175. <1> No second derivative for the first two buckets since we need at least 2 data points from the first derivative to calculate the
  176. second derivative