Skip to content

Commit d4d9a38

Browse files
committed
examples
1 parent 7f369ce commit d4d9a38

File tree

7 files changed

+383
-0
lines changed

7 files changed

+383
-0
lines changed

docs/reference/data-analysis/aggregations/search-aggregations-bucket-composite-aggregation.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,64 @@ Unlike the other `multi-bucket` aggregations, you can use the `composite` aggreg
1818

1919
The composite buckets are built from the combinations of the values extracted/created for each document and each combination is considered as a composite bucket.
2020

21+
%
22+
% [source,console]
23+
% --------------------------------------------------
24+
% PUT /sales
25+
% {
26+
% "mappings": {
27+
% "properties": {
28+
% "product": {
29+
% "type": "keyword"
30+
% },
31+
% "timestamp": {
32+
% "type": "date"
33+
% },
34+
% "price": {
35+
% "type": "long"
36+
% },
37+
% "shop": {
38+
% "type": "keyword"
39+
% },
40+
% "location": {
41+
% "type": "geo_point"
42+
% },
43+
% "nested": {
44+
% "type": "nested",
45+
% "properties": {
46+
% "product": {
47+
% "type": "keyword"
48+
% },
49+
% "timestamp": {
50+
% "type": "date"
51+
% },
52+
% "price": {
53+
% "type": "long"
54+
% },
55+
% "shop": {
56+
% "type": "keyword"
57+
% }
58+
% }
59+
% }
60+
% }
61+
% }
62+
% }
63+
%
64+
% POST /sales/_bulk?refresh
65+
% {"index":{"_id":0}}
66+
% {"product": "mad max", "price": "20", "timestamp": "2017-05-09T14:35"}
67+
% {"index":{"_id":1}}
68+
% {"product": "mad max", "price": "25", "timestamp": "2017-05-09T12:35"}
69+
% {"index":{"_id":2}}
70+
% {"product": "rocky", "price": "10", "timestamp": "2017-05-08T09:10"}
71+
% {"index":{"_id":3}}
72+
% {"product": "mad max", "price": "27", "timestamp": "2017-05-10T07:07"}
73+
% {"index":{"_id":4}}
74+
% {"product": "apocalypse now", "price": "10", "timestamp": "2017-05-11T08:35"}
75+
% -------------------------------------------------
76+
% // TESTSETUP
77+
%
78+
2179
For example, consider the following document:
2280

2381
```js
@@ -27,6 +85,8 @@ For example, consider the following document:
2785
}
2886
```
2987

88+
% NOTCONSOLE
89+
3090
Using `keyword` and `number` as source fields for the aggregation results in the following composite buckets:
3191

3292
```js
@@ -38,6 +98,8 @@ Using `keyword` and `number` as source fields for the aggregation results in the
3898
{ "keyword": "bar", "number": 76 }
3999
```
40100

101+
% NOTCONSOLE
102+
41103
## Value sources [_value_sources]
42104

43105
The `sources` parameter defines the source fields to use when building composite buckets. The order that the `sources` are defined controls the order that the keys are returned.
@@ -111,6 +173,32 @@ GET /_search
111173
}
112174
```
113175

176+
% [source,console-result]
177+
% ----
178+
% {
179+
% "timed_out": false,
180+
% "took": "$body.took",
181+
% "_shards": {
182+
% "total": 1,
183+
% "successful": 1,
184+
% "failed": 0,
185+
% "skipped": 0
186+
% },
187+
% "hits": "$body.hits",
188+
% "aggregations": {
189+
% "my_buckets": {
190+
% "after_key": { "dow": "Wednesday" },
191+
% "buckets": [
192+
% { "key": { "dow": "Monday" }, "doc_count": 1 },
193+
% { "key": { "dow": "Thursday" }, "doc_count": 1 },
194+
% { "key": { "dow": "Tuesday" }, "doc_count": 2 },
195+
% { "key": { "dow": "Wednesday" }, "doc_count": 1 }
196+
% ]
197+
% }
198+
% }
199+
% }
200+
% ----
201+
114202
Although similar, the `terms` value source doesn’t support the same set of parameters as the `terms` aggregation. For other supported value source parameters, see:
115203

116204
* [Order](#_order)
@@ -180,6 +268,31 @@ GET /_search
180268
}
181269
```
182270

271+
% [source,console-result]
272+
% ----
273+
% {
274+
% "timed_out": false,
275+
% "took": "$body.took",
276+
% "_shards": {
277+
% "total": 1,
278+
% "successful": 1,
279+
% "failed": 0,
280+
% "skipped": 0
281+
% },
282+
% "hits": "$body.hits",
283+
% "aggregations": {
284+
% "my_buckets": {
285+
% "after_key": { "price": 20.0 },
286+
% "buckets": [
287+
% { "key": { "price": 10.0 }, "doc_count": 2 },
288+
% { "key": { "price": 15.0 }, "doc_count": 1 },
289+
% { "key": { "price": 20.0 }, "doc_count": 2 }
290+
% ]
291+
% }
292+
% }
293+
% }
294+
% ----
295+
183296

184297
### Date histogram [_date_histogram]
185298

@@ -311,6 +424,8 @@ Instead of a single bucket starting at midnight, the above request groups the do
311424
}
312425
```
313426

427+
% TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
428+
314429
::::{note}
315430
The start `offset` of each bucket is calculated after `time_zone` adjustments have been made.
316431
::::
@@ -514,6 +629,8 @@ GET /_search
514629
}
515630
```
516631

632+
% TEST[s/_search/_search\?filter_path=aggregations/]
633+
517634
... returns:
518635

519636
```console-result
@@ -546,6 +663,8 @@ GET /_search
546663
}
547664
```
548665

666+
% TESTRESPONSE[s/\.\.\.//]
667+
549668
To get the next set of buckets, resend the same aggregation with the `after` parameter set to the `after_key` value returned in the response. For example, this request uses the `after_key` value provided in the previous response:
550669

551670
$$$composite-aggregation-after-example$$$
@@ -710,6 +829,8 @@ GET /_search
710829
}
711830
```
712831

832+
% TEST[s/_search/_search\?filter_path=aggregations/]
833+
713834
... returns:
714835

715836
```console-result
@@ -768,6 +889,8 @@ GET /_search
768889
}
769890
```
770891

892+
% TESTRESPONSE[s/\.\.\.//]
893+
771894

772895
## Pipeline aggregations [search-aggregations-bucket-composite-aggregation-pipeline-aggregations]
773896

docs/reference/data-analysis/aggregations/search-aggregations-metrics-avg-aggregation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ POST /exams/_search?size=0
2020
}
2121
```
2222

23+
% TEST[setup:exams]
24+
2325
The above aggregation computes the average grade over all documents. The aggregation type is `avg` and the `field` setting defines the numeric field of the documents the average will be computed on. The above will return the following:
2426

2527
```console-result
@@ -33,6 +35,8 @@ The above aggregation computes the average grade over all documents. The aggrega
3335
}
3436
```
3537

38+
% TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
39+
3640
The name of the aggregation (`avg_grade` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
3741

3842
## Script [_script_2]
@@ -63,6 +67,21 @@ POST /exams/_search?size=0
6367
}
6468
```
6569

70+
% TEST[setup:exams]
71+
72+
% TEST[s/size=0/size=0&filter_path=aggregations/]
73+
74+
% [source,console-result]
75+
% ----
76+
% {
77+
% "aggregations": {
78+
% "avg_corrected_grade": {
79+
% "value": 80.0
80+
% }
81+
% }
82+
% }
83+
% ----
84+
6685

6786
## Missing value [_missing_value_6]
6887

@@ -82,6 +101,8 @@ POST /exams/_search?size=0
82101
}
83102
```
84103

104+
% TEST[setup:exams]
105+
85106
1. Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.
86107

87108

@@ -134,4 +155,6 @@ For each histogram field the `avg` aggregation adds each number in the `values`
134155
}
135156
```
136157

158+
% TESTRESPONSE[skip:test not setup]
159+
137160

0 commit comments

Comments
 (0)