看,灰机...

es 按照某个field 聚合后,如何得到一有多少组?

Elasticsearch | 作者 shengtu0328 | 发布于2022年01月02日 | 阅读数:1553

就比如说根据某个field 聚合后, 有a组 b组 c组 ,想要得到3。有什么api可以得到3吗
 
现在需求是根据keyword (keyword是个field名称)聚合 ,获取每个keyword的(gmt_create 创建时间)最早的一条数据, 并且在keywod 聚合时要控制分页(一页20个keyword),和组外要按照gmt_create降序
{
"from": 0,
"size": 0,
"query": {
"bool": {
"must": [

{
"match": {
"knowledge": {
"query": "交通"
}
}
}
]
}
},
"aggs": {
"aggs_keyword": {
"terms": {
"field": "keyword"
},
"aggs": {
"aggs_gmt_create": {
"min": {
"field": "gmt_create"
}
},
"top_hits_knowledge_point": {
"top_hits": {
"from": 0,
"size": 1,
"sort": [
{
"gmt_create": {
"order": "asc"
}
}
]
}
},
"bucket_sort_gmt_create": {
"bucket_sort": {
"sort": {
"aggs_gmt_create": {
"order": "desc"
}
},
"from": 0,
"size": 20
}
}
}
}
}
}
已邀请:

tangjw

赞同来自: shengtu0328

根据哪个filed分组 就根据哪个field进行count统计就可以了吧?eg:
GET test/_search
{
  "size": 0,
  "aggs": {
    "testagg": {
      "terms": {
        "field": "keyword"
      }
    },
    "count":{
      "cardinality": {
        "field": "keyword"
      }
    }
    
  }
}
 
然后应该可以用ES的 highlevelclient 可以或者到count的值

shengtu0328

赞同来自:

这是结果有两个组,
交通规划3个 交通1个,但是怎么得到“2”个组的 2

 
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggs_keyword" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "交通规划",
"doc_count" : 3,
"aggs_gmt_create" : {
"value" : 1.608629483E12,
"value_as_string" : "2020-12-22 09:31:23"
},
"top_hits_knowledge_point" : {
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "knowledge_point_v1.0.0",
"_type" : "_doc",
"_id" : "-ckpGn4BZMm2NA1AliwF",
"_score" : null,
"_source" : {
"task_id" : "5",
"keyword" : "交通规划",
"knowledge" : "交通规划",
"gmt_create" : "2020-12-22 09:31:23"
},
"sort" : [
1608629483000
]
}
]
}
}
},
{
"key" : "交通",
"doc_count" : 1,
"aggs_gmt_create" : {
"value" : 1.608629483E12,
"value_as_string" : "2020-12-22 09:31:23"
},
"top_hits_knowledge_point" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "knowledge_point_v1.0.0",
"_type" : "_doc",
"_id" : "-MmDD34BZMm2NA1AAizw",
"_score" : null,
"_source" : {
"task_id" : "5",
"keyword" : "交通",
"knowledge" : "交通安全",
"gmt_create" : "2020-12-22 09:31:23"
},
"sort" : [
1608629483000
]
}
]
}
}
}
]
}
}
}

要回复问题请先登录注册