{
"size" : 0,
"aggs" : { 1
"popular_colors" : { 2
"terms" : { 3
"field" : "color"
}
}
}
}
|
1 |
聚合操作被置于顶层参数 |
|
2 |
然后,可以为聚合指定一个我们想要名称,本例中是: |
|
3 |
最后,定义单个桶的类型 |
{
...
"hits": {
"hits": [] ....1
},
"aggregations": {
"popular_colors": { ....2
"buckets": [
{
"key": "red", ....3
"doc_count": 4 ....4
},
{
"key": "blue",
"doc_count": 2
},
{
"key": "green",
"doc_count": 2
}
]
}
}
}
|
1 |
因为我们设置了 |
|
2 |
|
|
3 |
每个桶的 |
|
4 |
每个桶的数量代表该颜色的文档数量。 |
增加度量指标
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": { ...1
"avg_price": { ...2
"avg": {
"field": "price" ...3
}
}
}
}
}
}
|
1 |
为度量新增 |
|
2 |
为度量指定名字: |
|
3 |
最后,为 |
嵌套桶
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"make": {
"terms": {
"field": "make"
}
}
}
}
}
}
|
1 |
注意前例中的 |
|
2 |
另一个聚合 |
|
3 |
这个聚合是 |
本文深入解析了Elasticsearch中的聚合查询功能,详细介绍了如何使用聚合操作获取数据的统计信息,包括颜色频率统计、平均价格计算以及按制造商分组的多级聚合。通过实际案例,展示了聚合查询在数据分析中的强大能力。
1070

被折叠的 条评论
为什么被折叠?



