计算ElasticSearch中记录的最后快照的计数

时间:2019-12-01 15:39:53

标签: elasticsearch elasticsearch-aggregation

我将数据快照存储在ElasticSearch中。我想对每个条目的最新快照执行计数度量聚合,目的是要知道我当前(最新)数据的状态是

我有这样的东西

[
  {
    "id": 2,
    "state": "deleted",
    "timestamp": "2019-11-20T18:18:09+00:00"
  },
  {
    "id": 2,
    "state": "published",
    "timestamp": "2019-11-19T18:18:09+00:00"
  },
  {
    "id": 3,
    "state": "published",
    "timestamp": "2019-10-17T18:18:09+00:00"
  },
  {
    "id": 3,
    "state": "draft",
    "timestamp": "2019-10-16T18:18:09+00:00"
  }
]

我尝试过

POST /snapshots/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "2": {
      "terms": {
        "field": "state.keyword",
      },
      "aggs": {
        "1": {
          "top_hits": {
            "size": 1,
              "sort": [
              {
                "timestamp": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

但是问题是它首先创建一个存储桶,然后在该存储桶中进行排序并计算top_hits,因此代替了

  

已删除= 1

     

已发布= 1

     

草稿= 0

返回

  

已删除= 1

     

已发布= 1

     

草稿= 1

0 个答案:

没有答案