单个查询以查找某个字段中具有最大价值的文档 [英] Single query to find document with largest value for some field
本文介绍了单个查询以查找某个字段中具有最大价值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有唯一的数字运行ID的文档集合,我想找到运行ID最高的文档。
I've got a collection of documents that have unique numeric run ids, and I want to find the document with the highest run id.
我可以这样做两个查询。
I can do that two queries.
首先,我可以找到最高的运行ID:
Firstly, I can find the highest run id:
{
"size": 0,
"aggregations": {
"latest_run_id": {
"aggregations": {
"latest_run_id": {
"max": {"field": "run_id"}
}
}
}
}
}
第二,我可以找到具有运行ID的文档:
Secondly, I can find the document with that run id:
{
"filter": {
{"term": {"run_id": latest_run_id}}
}
}
有没有一种方法可以通过单个查询完成?
Is there a way I can do this with a single query?
推荐答案
您可以通过组合排序和大小来实现此目的:
You can accomplish this by combining "sort" and "size":
`{
"filter" : {
"match_all" : { }
},
"sort": [
{
"run_id": {
"order": "desc"
}
}
],
"size": 1
}`
这将返回具有最高 run_id的记录
这篇关于单个查询以查找某个字段中具有最大价值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文