Skip to content

Commit e05c6b1

Browse files
authored
Add REST tests for scripted metric aggregation. (#95632)
The added tests cover the [documented](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html) functionality. Related to #26220.
1 parent d6b80c0 commit e05c6b1

File tree

1 file changed

+100
-0
lines changed
  • modules/aggregations/src/yamlRestTest/resources/rest-api-spec/test/aggregations

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
setup:
2+
- do:
3+
indices.create:
4+
index: test
5+
body:
6+
settings:
7+
number_of_shards: 2
8+
number_of_replicas: 0
9+
mappings:
10+
properties:
11+
transaction:
12+
type: keyword
13+
amount:
14+
type: long
15+
- do:
16+
bulk:
17+
index: test
18+
refresh: true
19+
body:
20+
- { index: { } }
21+
- { transaction: "sale", amount: 80 }
22+
- { index: { } }
23+
- { transaction: "cost", amount: 10 }
24+
- { index: { } }
25+
- { transaction: "cost", amount: 30 }
26+
- { index: { } }
27+
- { transaction: "sale", amount: 130 }
28+
29+
30+
---
31+
"scripted transaction sum":
32+
- do:
33+
search:
34+
index: test
35+
body:
36+
size: 0
37+
aggs:
38+
profit:
39+
scripted_metric:
40+
init_script: |
41+
state.transactions = []
42+
map_script: |
43+
state.transactions.add(
44+
doc.transaction.value == 'sale'
45+
? doc.amount.value
46+
: -1 * doc.amount.value
47+
)
48+
combine_script: |
49+
long profit = 0;
50+
for (t in state.transactions) {
51+
profit += t;
52+
}
53+
return profit
54+
reduce_script: |
55+
long profit = 0;
56+
for (t in states) {
57+
profit += t;
58+
}
59+
return profit
60+
- match: { hits.total.value: 4 }
61+
- match: { aggregations.profit.value: 170 }
62+
63+
64+
---
65+
"scripted with params":
66+
- do:
67+
search:
68+
index: test
69+
body:
70+
size: 0
71+
aggs:
72+
profit:
73+
scripted_metric:
74+
init_script: |
75+
state.transactions = []
76+
map_script: |
77+
state.transactions.add(
78+
doc.transaction.value == 'sale'
79+
? doc.amount.value * params.sale_multiplier
80+
: doc.amount.value * params.cost_multiplier
81+
)
82+
combine_script: |
83+
long profit = 0;
84+
for (t in state.transactions) {
85+
profit += t;
86+
}
87+
return profit * params.combine_multiplier
88+
reduce_script: |
89+
long profit = 0;
90+
for (t in states) {
91+
profit += t;
92+
}
93+
return profit * params.reduce_multiplier
94+
params:
95+
cost_multiplier: 4
96+
sale_multiplier: 2
97+
combine_multiplier: 20
98+
reduce_multiplier: 100
99+
- match: { hits.total.value: 4 }
100+
- match: { aggregations.profit.value: 1160000 }

0 commit comments

Comments
 (0)