elasticsearch(4) - 常用Composite聚合查询

es常见的聚合查询有composite,现在案例主要是composite聚合例子。

Bool和Nested查询(订单结算查询)

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
GET /order/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"courseId": {
"value": "1"
}
}
},
{
"nested": {
"path": "sharer",
"query": {
"term": {
"sharer.sharerId": {
"value": "24"
}
}
}
}
},
{
"range": {
"term": {
"gte": 201103,
"lte": 202202
}
}
},
{
"term": {
"categoryId": {
"value": "1"
}
}
},
{
"range": {
"payMonth": {
"gte": 201103,
"lte": 202202
}
}
}
]
}
},
"sort": [
{
"orderId": {
"order": "desc"
}
},
{
"term": {
"order": "desc"
}
}
],
"from": 0,
"size": 20
}

Composite聚合和嵌套聚合(费用统计)

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
GET /order/_search?size=0
{
"query": {
"bool": {
"must": [
{
"terms": {
"courseId": [
"16861",
"25590"
]
}
},
{
"term": {
"sharerTerm": {
"value": "202003"
}
}
}
]
}
},
"aggs": {
"compositeData": {
"composite": {
"sources": [
{
"sharerId": {
"terms": {
"field": "sharerId"
}
}
},
{
"beginTerm": {
"terms": {
"field": "sharerTerm"
}
}
},
{
"courseId": {
"terms": {
"field": "courseId"
}
}
}
]
},
"aggs": {
"amortizesNest": {
"nested": {
"path": "sharerAmortizes"
},
"aggs": {
"amortizes.term": {
"filter": {
"term": {
"sharerAmortizes.term": 202002
}
},
"aggs": {
"amortizes.amount": {
"sum": {
"field": "sharerAmortizes.amount"
}
}
}
}
}
}
}
}
}
}

Compsite聚合和Sum统计(支付摊期统计)

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
GET /order/_search?size=0&request_cache=true
{
"query": {
"bool": {
"must": [
{
"range": {
"payMonth": {
"gte": 202110,
"lte": 202110
}
}
},
{
"range": {
"term": {
"gte": 202110
}
}
},
{
"term": {
"courseId": {
"value": "110872"
}
}
}
]
}
},
"aggs": {
"buckets": {
"composite": {
"size": 1000,
"sources": [
{
"courseId": {
"terms": {
"field": "courseId"
}
}
},
{
"term": {
"terms": {
"field": "term"
}
}
}
]
},
"aggs": {
"earningSum": {
"sum": {
"field": "earningSum"
}
},
"goodsEarning.1": {
"sum": {
"field": "goodsEarning.1"
}
},
"goodsEarning.2": {
"sum": {
"field": "goodsEarning.2"
}
},
"goodsEarning.3": {
"sum": {
"field": "goodsEarning.3"
}
},
"goodsEarning.4": {
"sum": {
"field": "goodsEarning.4"
}
},
"goodsEarning.5": {
"sum": {
"field": "goodsEarning.5"
}
},
"courseEarning": {
"sum": {
"field": "courseEarning"
}
},
"cost": {
"sum": {
"field": "cost"
}
},
"selfCost": {
"sum": {
"field": "selfCost"
}
},
"allCost": {
"sum": {
"field": "allCost"
}
},
"deliveryTaxedFee": {
"sum": {
"field": "deliveryTaxedFee"
}
},
"deliveryFee": {
"sum": {
"field": "deliveryFee"
}
},
"sharerSettles": {
"nested": {
"path": "sharerSettles"
},
"aggs": {
"sharerSettles.sharerId": {
"terms": {
"field": "sharerSettles.sharerId",
"size": 1000,
"shard_size": 1000,
"collect_mode": "breadth_first"
},
"aggs": {
"sharerSettles.rate": {
"terms": {
"field": "sharerSettles.rate_key",
"size": 100,
"shard_size": 100,
"collect_mode": "breadth_first"
},
"aggs": {
"sharerSettles.shareSum": {
"sum": {
"field": "sharerSettles.shareSum"
}
},
"sharerSettles.share": {
"sum": {
"field": "sharerSettles.share"
}
},
"sharerSettles.cost": {
"sum": {
"field": "sharerSettles.cost"
}
},
"sharerSettles.shareCost": {
"sum": {
"field": "sharerSettles.shareCost"
}
},
"sharerSettles.taxedEarning": {
"sum": {
"field": "sharerSettles.taxedEarning"
}
},
"sharerSettles.earning": {
"sum": {
"field": "sharerSettles.earning"
}
},
"sharerSettles.rate": {
"sum": {
"field": "sharerSettles.rate"
}
}
}
}
}
}
}
}
}
}
}
}

Composite聚合(支付方式统计)

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
106
107
108
109
110
111
GET /order/_search?size=0&request_cache=true
{
"query": {
"bool": {
"must": [
{
"range": {
"payMonth": {
"gte": 202010,
"lte": 202011
}
}
},
{
"range": {
"term": {
"gte": 202010,
"lte": 202011
}
}
}
]
}
},
"aggs": {
"buckets": {
"composite": {
"size": 1000,
"sources": [
{
"courseId": {
"terms": {
"field": "courseId",
"order": "desc"
}
}
},
{
"term": {
"terms": {
"field": "term",
"order": "asc"
}
}
},
{
"payFrom": {
"terms": {
"field": "payFrom",
"missing_bucket": true,
"order": "asc"
}
}
}
]
},
"aggs": {
"courseEarning": {
"sum": {
"field": "courseEarning"
}
},
"goodsEarning.1": {
"sum": {
"field": "goodsEarning.1"
}
},
"goodsEarning.2": {
"sum": {
"field": "goodsEarning.2"
}
},
"goodsEarning.3": {
"sum": {
"field": "goodsEarning.3"
}
},
"goodsEarning.4": {
"sum": {
"field": "goodsEarning.4"
}
},
"goodsEarning.5": {
"sum": {
"field": "goodsEarning.5"
}
},
"poundage": {
"sum": {
"field": "poundage"
}
},
"agentFee": {
"sum": {
"field": "agentFee"
}
},
"deliveryTaxedFee": {
"sum": {
"field": "deliveryTaxedFee"
}
},
"deliveryFee": {
"sum": {
"field": "deliveryFee"
}
}
}
}
}
}