derivative-aggregation.asciidoc 9.2 KB

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