顿搜
飞过闲红千叶,夕岸在哪
类目归类
在SQL语句中有group by,而在Elasticsearch中把它叫Aggregation,即聚合运算
GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword"
}
}
}
}返回结果
{
"took" : 2,
"timed_out" : false,
…
"hits" : {
"total" : {
"value" : 4, // 统计总条数
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_address" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "上海",
"doc_count" : 2
},
{
"key" : "杭州",
"doc_count" : 2
}
]
}
}
}GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword"
},
"aggs": {
"score_avg": {
"avg": {
"field": "score"
}
}
}
}
}
}返回结果
{
"took" : 6,
…
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_address" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "上海",
"doc_count" : 2,
"score_avg" : {
"value" : 75.0
}
},
{
"key" : "杭州",
"doc_count" : 2,
"score_avg" : {
"value" : 75.0
}
}
]
}
}
}GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword"
},
"aggs": {
"score_max": {
"max": {
"field": "score"
}
}
}
}
}
}GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword"
},
"aggs": {
"score_min": {
"min": {
"field": "score"
}
}
}
}
}
}GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword"
},
"aggs": {
"score_sum": {
"sum": {
"field": "score"
}
}
}
}
}
}GET /dbindex/_search
{
"size": 0,
"aggs": {
"group_by_address": {
"terms": {
"field": "address.keyword",
"order": {
"score_min": "desc"
}
},
"aggs": {
"score_min": {
"min": {
"field": "score"
}
}
}
}
}
}