derivative-aggregation.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. [[search-aggregations-pipeline-derivative-aggregation]]
  2. === Derivative Aggregation
  3. experimental[]
  4. A parent pipeline aggregation which calculates the derivative of a specified metric in a parent histogram (or date_histogram)
  5. aggregation. The specified metric must be numeric and the enclosing histogram must have `min_doc_count` set to `0` (default
  6. for `histogram` aggregations).
  7. ==== Syntax
  8. A `derivative` aggregation looks like this in isolation:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "derivative": {
  13. "buckets_path": "the_sum"
  14. }
  15. }
  16. --------------------------------------------------
  17. .`derivative` Parameters
  18. |===
  19. |Parameter Name |Description |Required |Default Value
  20. |`buckets_path` |The path to the buckets we wish to find the derivative for (see <<buckets-path-syntax>> for more
  21. details) |Required |
  22. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  23. details)|Optional, defaults to `skip` |
  24. |`format` |format to apply to the output value of this aggregation |Optional, defaults to `null` |
  25. |===
  26. ==== First Order Derivative
  27. The following snippet calculates the derivative of the total monthly `sales`:
  28. [source,js]
  29. --------------------------------------------------
  30. {
  31. "aggs" : {
  32. "sales_per_month" : {
  33. "date_histogram" : {
  34. "field" : "date",
  35. "interval" : "month"
  36. },
  37. "aggs": {
  38. "sales": {
  39. "sum": {
  40. "field": "price"
  41. }
  42. },
  43. "sales_deriv": {
  44. "derivative": {
  45. "buckets_path": "sales" <1>
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. <1> `buckets_path` instructs this derivative aggregation to use the output of the `sales` aggregation for the derivative
  54. And the following may be the response:
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "aggregations": {
  59. "sales_per_month": {
  60. "buckets": [
  61. {
  62. "key_as_string": "2015/01/01 00:00:00",
  63. "key": 1420070400000,
  64. "doc_count": 3,
  65. "sales": {
  66. "value": 550
  67. } <1>
  68. },
  69. {
  70. "key_as_string": "2015/02/01 00:00:00",
  71. "key": 1422748800000,
  72. "doc_count": 2,
  73. "sales": {
  74. "value": 60
  75. },
  76. "sales_deriv": {
  77. "value": -490 <2>
  78. }
  79. },
  80. {
  81. "key_as_string": "2015/03/01 00:00:00",
  82. "key": 1425168000000,
  83. "doc_count": 2, <3>
  84. "sales": {
  85. "value": 375
  86. },
  87. "sales_deriv": {
  88. "value": 315
  89. }
  90. }
  91. ]
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. <1> No derivative for the first bucket since we need at least 2 data points to calculate the derivative
  97. <2> Derivative value units are implicitly defined by the `sales` aggregation and the parent histogram so in this case the units
  98. would be $/month assuming the `price` field has units of $.
  99. <3> The number of documents in the bucket are represented by the `doc_count` f
  100. ==== Second Order Derivative
  101. A second order derivative can be calculated by chaining the derivative pipeline aggregation onto the result of another derivative
  102. pipeline aggregation as in the following example which will calculate both the first and the second order derivative of the total
  103. monthly sales:
  104. [source,js]
  105. --------------------------------------------------
  106. {
  107. "aggs" : {
  108. "sales_per_month" : {
  109. "date_histogram" : {
  110. "field" : "date",
  111. "interval" : "month"
  112. },
  113. "aggs": {
  114. "sales": {
  115. "sum": {
  116. "field": "price"
  117. }
  118. },
  119. "sales_deriv": {
  120. "derivative": {
  121. "buckets_path": "sales"
  122. }
  123. },
  124. "sales_2nd_deriv": {
  125. "derivative": {
  126. "buckets_path": "sales_deriv" <1>
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. --------------------------------------------------
  134. <1> `buckets_path` for the second derivative points to the name of the first derivative
  135. And the following may be the response:
  136. [source,js]
  137. --------------------------------------------------
  138. {
  139. "aggregations": {
  140. "sales_per_month": {
  141. "buckets": [
  142. {
  143. "key_as_string": "2015/01/01 00:00:00",
  144. "key": 1420070400000,
  145. "doc_count": 3,
  146. "sales": {
  147. "value": 550
  148. } <1>
  149. },
  150. {
  151. "key_as_string": "2015/02/01 00:00:00",
  152. "key": 1422748800000,
  153. "doc_count": 2,
  154. "sales": {
  155. "value": 60
  156. },
  157. "sales_deriv": {
  158. "value": -490
  159. } <1>
  160. },
  161. {
  162. "key_as_string": "2015/03/01 00:00:00",
  163. "key": 1425168000000,
  164. "doc_count": 2,
  165. "sales": {
  166. "value": 375
  167. },
  168. "sales_deriv": {
  169. "value": 315
  170. },
  171. "sales_2nd_deriv": {
  172. "value": 805
  173. }
  174. }
  175. ]
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. <1> No second derivative for the first two buckets since we need at least 2 data points from the first derivative to calculate the
  181. second derivative
  182. ==== Units
  183. The derivative aggregation allows the units of the derivative values to be specified. This returns an extra field in the response
  184. `normalized_value` which reports the derivative value in the desired x-axis units. In the below example we calculate the derivative
  185. of the total sales per month but ask for the derivative of the sales as in the units of sales per day:
  186. [source,js]
  187. --------------------------------------------------
  188. {
  189. "aggs" : {
  190. "sales_per_month" : {
  191. "date_histogram" : {
  192. "field" : "date",
  193. "interval" : "month"
  194. },
  195. "aggs": {
  196. "sales": {
  197. "sum": {
  198. "field": "price"
  199. }
  200. },
  201. "sales_deriv": {
  202. "derivative": {
  203. "buckets_path": "sales",
  204. "unit": "day" <1>
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. --------------------------------------------------
  212. <1> `unit` specifies what unit to use for the x-axis of the derivative calculation
  213. And the following may be the response:
  214. [source,js]
  215. --------------------------------------------------
  216. {
  217. "aggregations": {
  218. "sales_per_month": {
  219. "buckets": [
  220. {
  221. "key_as_string": "2015/01/01 00:00:00",
  222. "key": 1420070400000,
  223. "doc_count": 3,
  224. "sales": {
  225. "value": 550
  226. } <1>
  227. },
  228. {
  229. "key_as_string": "2015/02/01 00:00:00",
  230. "key": 1422748800000,
  231. "doc_count": 2,
  232. "sales": {
  233. "value": 60
  234. },
  235. "sales_deriv": {
  236. "value": -490, <1>
  237. "normalized_value": -17.5 <2>
  238. }
  239. },
  240. {
  241. "key_as_string": "2015/03/01 00:00:00",
  242. "key": 1425168000000,
  243. "doc_count": 2,
  244. "sales": {
  245. "value": 375
  246. },
  247. "sales_deriv": {
  248. "value": 315,
  249. "normalized_value": 10.16129032258065
  250. }
  251. }
  252. ]
  253. }
  254. }
  255. }
  256. --------------------------------------------------
  257. <1> `value` is reported in the original units of 'per month'
  258. <2> `normalized_value` is reported in the desired units of 'per day'