count(1)
select clssId,count(1) from student group by classId
{ "size":0, "aggs": { "group_by_classId": { "terms": { "field": "classId.keyword" } } } }
结果 key为classId doc_count 为count
size 0表示只看聚合结果不看搜索结果
{ "took": 82, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "group_by_classId": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "2", "doc_count": 3 }, { "key": "5", "doc_count": 1 } ] } } }
count+avg
select clssId,count(1),avg(score) from student group by classId
{ "size":0, "aggs": { "group_by_classId": { "terms": { "field": "classId.keyword" }, "aggs": { "average_balance": { "avg": { "field": "score" } } } } } }
结果
{ "took": 95, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "group_by_classId": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "2", "doc_count": 3, "average_balance": { "value": 53.333333333333336 } }, { "key": "5", "doc_count": 1, "average_balance": { "value": 10 } } ] } } }
聚合并排序
select clssId,count(1),avg(score) from student group by classId order by avg(score) desc
{ "size":0, "aggs": { "group_by_classId": { "terms": { "field": "classId.keyword", "order": { "average_score": "desc" } }, "aggs": { "average_score": { "avg": { "field": "score" } } } } } }
Metric Aggregations
地理边界聚合
1.添加测试mapping
{ "mappings": { "doc": { "properties": { "location": { "type": "geo_point" } } } } }
2.添加测试数据
{ "text": "成都", "location": { "lat": 11.12, "lon": -5.34 } }
{ "text": "广州", "location": { "lat": 66.12, "lon": -22.34 } }
{ "text": "深圳", "location": { "lat": 121.12, "lon": -77.34 } }
测试query
{ "size":0, "query" : { "match_all" : { } }, "aggs" : { "viewport" : { "geo_bounds" : { "field" : "location", "wrap_longitude" : true //可选参数,指定边界框是否允许与国际日期变更线重叠,默认值是true。 } } } }
结果
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0, "hits": [] }, "aggregations": { "viewport": { "bounds": { "top_left": { "lat": 41.1199999647215, "lon": -71.34000004269183 }, "bottom_right": { "lat": 11.119999992661178, "lon": -5.340000037103891 } } } } }
地理重心聚合
用以上测试数据
query
{ "size":0, "query" : { "match_all" : { } }, "aggs" : { "centroid" : { "geo_centroid" : { "field" : "location" } } } }
结果
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0, "hits": [] }, "aggregations": { "centroid": { "location": { "lat": 26.11999997869134, "lon": -38.34000003989786 }, "count": 2 } } }
平均值聚合
{ "size":0, "aggs" : { "avg_grade" : { "avg" : { "field" : "mdProductId" } } } }
count聚合
{ "aggs" : { "types_count" : { "value_count" : { "field" : "type" } } } }
最大值聚合
{ "aggs" : { "max_price" : { "max" : { "field" : "price" } } } }
最小值聚合
{ "aggs" : { "min_price" : { "min" : { "field" : "price" } } } }
使用脚本聚合
如最小值为例子
{ "aggs" : { "min_price" : { "min" : { "script" : { "inline" : "doc.price.value" } } } } }
设置聚合默认值
POST /sales/_search { "aggs" : { "grade_max" : { "max" : { "field" : "grade", "missing": 10 } } } }
聚合结果脚本运算
{ "aggs" : { "min_price_in_euros" : { "min" : { "field" : "price", "script" : { "inline" : "_value * params.conversion_rate", "params" : { "conversion_rate" : 1.2 } } } } } }
父子聚合
{
"size": 0,
"aggs": {
"detail": {
"children": {
"type": "ic_product_store_account"--子文档
},
"aggs": {
"sum_price": {
"sum": {
"field": "sumCount"--子文档聚合字段
}
}
}
}
}
}
goupBy语法
avg
{
"size": 0,
"aggs" : {
"group_by_tags" : {
"terms" : { "field" : "productId" },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
select productId,avg(price) from type grup by productId
需要查询条件加上query语法就行了