{ "from": 0, "size": 110000, "query": { "bool": { "must": [ { "match": { "deviceid": 27143 } }, { "match": { "monkey": 46 } }, { "range": { "createtime": { "gte": "2023-03-14", "lt": "2023-06-14 23:59:59" } } } ] } }, "aggs": { "group_by_date": { "date_histogram": { "field": "createtime", "interval": "1d", "format": "yyyyMMdd" }, "aggs": { "max_id": { "max": { "field": "id" } }, "mon_value": { "top_hits": { "size": 1, "_source": "monvalue" } } } } }}要获取最大值id的对应的monvalue,要如何修改?
时间: 2023-07-09 12:54:05 浏览: 42
要获取最大值id的对应的monvalue,可以在最外层的聚合中使用bucket_sort聚合对子聚合进行排序,然后使用top_hits聚合获取最大id所对应的文档,从而得到对应的monvalue值。以下是一个示例查询语句:
```
{
"from": 0,
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"deviceid": 27143
}
},
{
"match": {
"monkey": 46
}
},
{
"range": {
"createtime": {
"gte": "2023-03-14",
"lt": "2023-06-14 23:59:59"
}
}
}
]
}
},
"aggs": {
"max_id": {
"max": {
"field": "id"
}
},
"group_by_date": {
"date_histogram": {
"field": "createtime",
"interval": "1d",
"format": "yyyyMMdd"
},
"aggs": {
"mon_value": {
"top_hits": {
"size": 1,
"_source": "monvalue",
"sort": [
{
"id": {
"order": "desc"
}
}
]
}
},
"sort_by_id": {
"bucket_sort": {
"sort": [
{
"max_id": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}
}
}
```
这个查询中,我们首先使用max聚合获取id的最大值,然后在date_histogram聚合中使用top_hits聚合获取最大id所对应的文档,并且使用sort参数按照id的降序排序。然后我们使用bucket_sort聚合对子聚合进行排序,按照max_id的降序排序,从而得到最大id所对应的monvalue值。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)