Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Derivative Aggregation
> More info about derivative aggregation is in the [official elasticsearch docs][1]
A parent pipeline aggregation which calculates the derivative of a specified metric in a parent
histogram (or date_histogram) aggregation. The specified metric must be numeric and the enclosing
histogram must have min_doc_count set to 0 (default for histogram aggregations).
## Simple example
```JSON
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
},
"sales_deriv": {
"derivative": {
"buckets_path": "sales"
}
}
}
}
}
}
```
And now the query via DSL:
```php
$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month');
$sumAggregation = new SumAggregation('sales', 'price');
$derivativeAggregation = new DerivativeAggregation('sales_deriv', 'sales');
$dateAggregation->addAggregation($sumAggregation);
$dateAggregation->addAggregation($derivativeAggregation);
$search = new Search();
$search->addAggregation($dateAggregation);
$queryArray = $search->toArray();
```
## Second order derivative
Somewhat more complex would be an example of a second order derivatives. This functionality
is presented in the folowing example:
```json
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
},
"sales_deriv": {
"derivative": {
"buckets_path": "sales"
}
},
"sales_2nd_deriv": {
"derivative": {
"buckets_path": "sales_deriv"
}
}
}
}
}
}
```
And now via DSL:
```php
$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month');
$sumAggregation = new SumAggregation('sales', 'price');
$firstDerivativeAggregation = new DerivativeAggregation('sales_deriv', 'sales');
$secondDerivativeAggregation = new DerivativeAggregation('sales_2nd_deriv', 'sales_deriv');
$dateAggregation->addAggregation($sumAggregation);
$dateAggregation->addAggregation($firstDerivativeAggregation);
$dateAggregation->addAggregation($secondDerivativeAggregation);
$search = new Search();
$search->addAggregation($dateAggregation);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html