derivative-aggregation.asciidoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. [[search-aggregations-pipeline-derivative-aggregation]]
  2. === Derivative Aggregation
  3. A parent pipeline 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. "derivative": {
  11. "buckets_path": "the_sum"
  12. }
  13. --------------------------------------------------
  14. // NOTCONSOLE
  15. [[derivative-params]]
  16. .`derivative` Parameters
  17. [options="header"]
  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 |`skip`
  24. |`format` |format to apply to the output value of this aggregation |Optional | `null`
  25. |===
  26. ==== First Order Derivative
  27. The following snippet calculates the derivative of the total monthly `sales`:
  28. [source,console]
  29. --------------------------------------------------
  30. POST /sales/_search
  31. {
  32. "size": 0,
  33. "aggs" : {
  34. "sales_per_month" : {
  35. "date_histogram" : {
  36. "field" : "date",
  37. "calendar_interval" : "month"
  38. },
  39. "aggs": {
  40. "sales": {
  41. "sum": {
  42. "field": "price"
  43. }
  44. },
  45. "sales_deriv": {
  46. "derivative": {
  47. "buckets_path": "sales" <1>
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // TEST[setup:sales]
  56. <1> `buckets_path` instructs this derivative aggregation to use the output of the `sales` aggregation for the derivative
  57. And the following may be the response:
  58. [source,js]
  59. --------------------------------------------------
  60. {
  61. "took": 11,
  62. "timed_out": false,
  63. "_shards": ...,
  64. "hits": ...,
  65. "aggregations": {
  66. "sales_per_month": {
  67. "buckets": [
  68. {
  69. "key_as_string": "2015/01/01 00:00:00",
  70. "key": 1420070400000,
  71. "doc_count": 3,
  72. "sales": {
  73. "value": 550.0
  74. } <1>
  75. },
  76. {
  77. "key_as_string": "2015/02/01 00:00:00",
  78. "key": 1422748800000,
  79. "doc_count": 2,
  80. "sales": {
  81. "value": 60.0
  82. },
  83. "sales_deriv": {
  84. "value": -490.0 <2>
  85. }
  86. },
  87. {
  88. "key_as_string": "2015/03/01 00:00:00",
  89. "key": 1425168000000,
  90. "doc_count": 2, <3>
  91. "sales": {
  92. "value": 375.0
  93. },
  94. "sales_deriv": {
  95. "value": 315.0
  96. }
  97. }
  98. ]
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  104. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  105. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  106. <1> No derivative for the first bucket since we need at least 2 data points to calculate the derivative
  107. <2> Derivative value units are implicitly defined by the `sales` aggregation and the parent histogram so in this case the units
  108. would be $/month assuming the `price` field has units of $.
  109. <3> The number of documents in the bucket are represented by the `doc_count`
  110. ==== Second Order Derivative
  111. A second order derivative can be calculated by chaining the derivative pipeline aggregation onto the result of another derivative
  112. pipeline aggregation as in the following example which will calculate both the first and the second order derivative of the total
  113. monthly sales:
  114. [source,console]
  115. --------------------------------------------------
  116. POST /sales/_search
  117. {
  118. "size": 0,
  119. "aggs" : {
  120. "sales_per_month" : {
  121. "date_histogram" : {
  122. "field" : "date",
  123. "calendar_interval" : "month"
  124. },
  125. "aggs": {
  126. "sales": {
  127. "sum": {
  128. "field": "price"
  129. }
  130. },
  131. "sales_deriv": {
  132. "derivative": {
  133. "buckets_path": "sales"
  134. }
  135. },
  136. "sales_2nd_deriv": {
  137. "derivative": {
  138. "buckets_path": "sales_deriv" <1>
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. --------------------------------------------------
  146. // TEST[setup:sales]
  147. <1> `buckets_path` for the second derivative points to the name of the first derivative
  148. And the following may be the response:
  149. [source,js]
  150. --------------------------------------------------
  151. {
  152. "took": 50,
  153. "timed_out": false,
  154. "_shards": ...,
  155. "hits": ...,
  156. "aggregations": {
  157. "sales_per_month": {
  158. "buckets": [
  159. {
  160. "key_as_string": "2015/01/01 00:00:00",
  161. "key": 1420070400000,
  162. "doc_count": 3,
  163. "sales": {
  164. "value": 550.0
  165. } <1>
  166. },
  167. {
  168. "key_as_string": "2015/02/01 00:00:00",
  169. "key": 1422748800000,
  170. "doc_count": 2,
  171. "sales": {
  172. "value": 60.0
  173. },
  174. "sales_deriv": {
  175. "value": -490.0
  176. } <1>
  177. },
  178. {
  179. "key_as_string": "2015/03/01 00:00:00",
  180. "key": 1425168000000,
  181. "doc_count": 2,
  182. "sales": {
  183. "value": 375.0
  184. },
  185. "sales_deriv": {
  186. "value": 315.0
  187. },
  188. "sales_2nd_deriv": {
  189. "value": 805.0
  190. }
  191. }
  192. ]
  193. }
  194. }
  195. }
  196. --------------------------------------------------
  197. // TESTRESPONSE[s/"took": 50/"took": $body.took/]
  198. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  199. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  200. <1> No second derivative for the first two buckets since we need at least 2 data points from the first derivative to calculate the
  201. second derivative
  202. ==== Units
  203. The derivative aggregation allows the units of the derivative values to be specified. This returns an extra field in the response
  204. `normalized_value` which reports the derivative value in the desired x-axis units. In the below example we calculate the derivative
  205. of the total sales per month but ask for the derivative of the sales as in the units of sales per day:
  206. [source,console]
  207. --------------------------------------------------
  208. POST /sales/_search
  209. {
  210. "size": 0,
  211. "aggs" : {
  212. "sales_per_month" : {
  213. "date_histogram" : {
  214. "field" : "date",
  215. "calendar_interval" : "month"
  216. },
  217. "aggs": {
  218. "sales": {
  219. "sum": {
  220. "field": "price"
  221. }
  222. },
  223. "sales_deriv": {
  224. "derivative": {
  225. "buckets_path": "sales",
  226. "unit": "day" <1>
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. --------------------------------------------------
  234. // TEST[setup:sales]
  235. <1> `unit` specifies what unit to use for the x-axis of the derivative calculation
  236. And the following may be the response:
  237. [source,js]
  238. --------------------------------------------------
  239. {
  240. "took": 50,
  241. "timed_out": false,
  242. "_shards": ...,
  243. "hits": ...,
  244. "aggregations": {
  245. "sales_per_month": {
  246. "buckets": [
  247. {
  248. "key_as_string": "2015/01/01 00:00:00",
  249. "key": 1420070400000,
  250. "doc_count": 3,
  251. "sales": {
  252. "value": 550.0
  253. } <1>
  254. },
  255. {
  256. "key_as_string": "2015/02/01 00:00:00",
  257. "key": 1422748800000,
  258. "doc_count": 2,
  259. "sales": {
  260. "value": 60.0
  261. },
  262. "sales_deriv": {
  263. "value": -490.0, <1>
  264. "normalized_value": -15.806451612903226 <2>
  265. }
  266. },
  267. {
  268. "key_as_string": "2015/03/01 00:00:00",
  269. "key": 1425168000000,
  270. "doc_count": 2,
  271. "sales": {
  272. "value": 375.0
  273. },
  274. "sales_deriv": {
  275. "value": 315.0,
  276. "normalized_value": 11.25
  277. }
  278. }
  279. ]
  280. }
  281. }
  282. }
  283. --------------------------------------------------
  284. // TESTRESPONSE[s/"took": 50/"took": $body.took/]
  285. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  286. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  287. <1> `value` is reported in the original units of 'per month'
  288. <2> `normalized_value` is reported in the desired units of 'per day'