我有查询的作品。它基于Id聚合数据,并根据创建的字段查找 MOST RECENT 对象。我的问题是我想找到第二个最近的而不是MOST最近的。我将如何处理?我一直在浏览所有文档,发现的范围并不多。谢谢 :)

{
    "query":{
      "match": {
         "name": "Robert"
      }
    },
    "aggs": {
        "previous": {
            "terms": {
                "field": "Id",
                "order": {"timeCreated": "desc"}
            },
            "aggs": {
                "timeCreated": {
                    "max": {"field": "created"}
                }

            }
        }
    }
}

最佳答案

Top_hits是您要寻找的。用这个:

{
"query":{
  "match": {
     "name": "A"
  }
},
"aggs": {
    "previous": {
        "terms": {
            "field": "Id"

        },
         "aggs": {
            "latestRecords": {
              "top_hits": {
                "sort": {
                  "created": {
                    "order": "desc"
                  }
                },
                "size" :2
              }
            }
          }
       }
     }
   }

07-25 22:04