derivative-aggregation.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. "derivative": {
  12. "buckets_path": "the_sum"
  13. }
  14. --------------------------------------------------
  15. // NOTCONSOLE
  16. .`derivative` Parameters
  17. |===
  18. |Parameter Name |Description |Required |Default Value
  19. |`buckets_path` |The path to the buckets we wish to find the derivative for (see <<buckets-path-syntax>> for more
  20. details) |Required |
  21. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  22. details)|Optional |`skip`
  23. |`format` |format to apply to the output value of this aggregation |Optional | `null`
  24. |===
  25. ==== First Order Derivative
  26. The following snippet calculates the derivative of the total monthly `sales`:
  27. [source,js]
  28. --------------------------------------------------
  29. POST /sales/_search
  30. {
  31. "size": 0,
  32. "aggs" : {
  33. "sales_per_month" : {
  34. "date_histogram" : {
  35. "field" : "date",
  36. "interval" : "month"
  37. },
  38. "aggs": {
  39. "sales": {
  40. "sum": {
  41. "field": "price"
  42. }
  43. },
  44. "sales_deriv": {
  45. "derivative": {
  46. "buckets_path": "sales" <1>
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. --------------------------------------------------
  54. // CONSOLE
  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,js]
  115. --------------------------------------------------
  116. POST /sales/_search
  117. {
  118. "size": 0,
  119. "aggs" : {
  120. "sales_per_month" : {
  121. "date_histogram" : {
  122. "field" : "date",
  123. "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. // CONSOLE
  147. // TEST[setup:sales]
  148. <1> `buckets_path` for the second derivative points to the name of the first derivative
  149. And the following may be the response:
  150. [source,js]
  151. --------------------------------------------------
  152. {
  153. "took": 50,
  154. "timed_out": false,
  155. "_shards": ...,
  156. "hits": ...,
  157. "aggregations": {
  158. "sales_per_month": {
  159. "buckets": [
  160. {
  161. "key_as_string": "2015/01/01 00:00:00",
  162. "key": 1420070400000,
  163. "doc_count": 3,
  164. "sales": {
  165. "value": 550.0
  166. } <1>
  167. },
  168. {
  169. "key_as_string": "2015/02/01 00:00:00",
  170. "key": 1422748800000,
  171. "doc_count": 2,
  172. "sales": {
  173. "value": 60.0
  174. },
  175. "sales_deriv": {
  176. "value": -490.0
  177. } <1>
  178. },
  179. {
  180. "key_as_string": "2015/03/01 00:00:00",
  181. "key": 1425168000000,
  182. "doc_count": 2,
  183. "sales": {
  184. "value": 375.0
  185. },
  186. "sales_deriv": {
  187. "value": 315.0
  188. },
  189. "sales_2nd_deriv": {
  190. "value": 805.0
  191. }
  192. }
  193. ]
  194. }
  195. }
  196. }
  197. --------------------------------------------------
  198. // TESTRESPONSE[s/"took": 50/"took": $body.took/]
  199. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  200. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  201. <1> No second derivative for the first two buckets since we need at least 2 data points from the first derivative to calculate the
  202. second derivative
  203. ==== Units
  204. The derivative aggregation allows the units of the derivative values to be specified. This returns an extra field in the response
  205. `normalized_value` which reports the derivative value in the desired x-axis units. In the below example we calculate the derivative
  206. of the total sales per month but ask for the derivative of the sales as in the units of sales per day:
  207. [source,js]
  208. --------------------------------------------------
  209. POST /sales/_search
  210. {
  211. "size": 0,
  212. "aggs" : {
  213. "sales_per_month" : {
  214. "date_histogram" : {
  215. "field" : "date",
  216. "interval" : "month"
  217. },
  218. "aggs": {
  219. "sales": {
  220. "sum": {
  221. "field": "price"
  222. }
  223. },
  224. "sales_deriv": {
  225. "derivative": {
  226. "buckets_path": "sales",
  227. "unit": "day" <1>
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. --------------------------------------------------
  235. // CONSOLE
  236. // TEST[setup:sales]
  237. <1> `unit` specifies what unit to use for the x-axis of the derivative calculation
  238. And the following may be the response:
  239. [source,js]
  240. --------------------------------------------------
  241. {
  242. "took": 50,
  243. "timed_out": false,
  244. "_shards": ...,
  245. "hits": ...,
  246. "aggregations": {
  247. "sales_per_month": {
  248. "buckets": [
  249. {
  250. "key_as_string": "2015/01/01 00:00:00",
  251. "key": 1420070400000,
  252. "doc_count": 3,
  253. "sales": {
  254. "value": 550.0
  255. } <1>
  256. },
  257. {
  258. "key_as_string": "2015/02/01 00:00:00",
  259. "key": 1422748800000,
  260. "doc_count": 2,
  261. "sales": {
  262. "value": 60.0
  263. },
  264. "sales_deriv": {
  265. "value": -490.0, <1>
  266. "normalized_value": -15.806451612903226 <2>
  267. }
  268. },
  269. {
  270. "key_as_string": "2015/03/01 00:00:00",
  271. "key": 1425168000000,
  272. "doc_count": 2,
  273. "sales": {
  274. "value": 375.0
  275. },
  276. "sales_deriv": {
  277. "value": 315.0,
  278. "normalized_value": 11.25
  279. }
  280. }
  281. ]
  282. }
  283. }
  284. }
  285. --------------------------------------------------
  286. // TESTRESPONSE[s/"took": 50/"took": $body.took/]
  287. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  288. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  289. <1> `value` is reported in the original units of 'per month'
  290. <2> `normalized_value` is reported in the desired units of 'per day'