Elasticsearch:日期聚合最近

时间:2016-03-28 20:05:22

标签: date search elasticsearch aggregation

我的查询有效。它根据Id聚合数据,并根据创建的字段查找 MOST RECENT 对象。我遇到的问题是我想找到最近的第二次而不是最近的。我该怎么做?我一直在查看文档,我所能找到的是范围对我没什么帮助。谢谢:))

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

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Top_hits正是您要找的。使用此:

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

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