我有点怀疑你在刷屏

聚合某个字段,后获取其发帖天数最多的前两个

Elasticsearch | 作者 freedom | 发布于2020年04月15日 | 阅读数:3015

软件版本;elasticsearch 5.6.4

问题:例如:name字段有 (张三、李四、王五) 三种值,pubtime字段是发帖时间(YYYY-MM-dd),现在要查询发帖天数最多的两个人,该如何写语句?
 
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

 
PUT my_index_009
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"publish_time": {
"type": "date"
}
}
}
}

POST my_index09/_bulk
{"index":{"_id":1}}
{"name":"张三","publish_time":"2020-05-01" }
{"index":{"_id":2}}
{"name":"李四","publish_time":"2020-05-02" }
{"index":{"_id":3}}
{"name":"王五","publish_time":"2020-05-03" }
{"index":{"_id":4}}
{"name":"张三","publish_time":"2015-05-02" }
{"index":{"_id":5}}
{"name":"张三","publish_time":"2015-05-03" }
{"index":{"_id":6}}
{"name":"李四","publish_time":"2015-05-04" }
{"index":{"_id":7}}
{"name":"王五","publish_time":"2015-05-05" }
{"index":{"_id":8}}
{"name":"张三","publish_time":"2015-05-04" }

POST my_index09/_search
{
"size": 0,
"aggs": {
"name_aggs": {
"terms": {
"field": "name.keyword",
"shard_size":10,
"size": 2
},
"aggs": {
"date_aggs": {
"value_count": {
"field": "publish_time"
}
}
}
}
}
}

要回复问题请先登录注册