在ElasticSearch中分页top_hits聚合

问题描述 投票:0回答:1

现在我在Elastic Search中进行top_hits聚合,按字段对数据进行分组,按日期对组进行排序,然后选择前1。

我需要以某种方式页面聚合结果,我可以通过pageSize和pageNumber,但我不知道如何。

除此之外,我还需要此聚合的总结果,以便我们可以在Web界面的表格中显示它。

聚合看起来像这样:

POST my_index/_search
{
  "size": 0,
  "aggs": {
    "top_artifacts": {
      "terms": {
        "field": "artifactId.keyword"
      },
      "aggs": {
        "top_artifacts_hits": {
          "top_hits": {
            "size": 1,
            "sort": [{
              "date": {
                "order": "desc"
              }
            }]
          }
        }
      }
    }
  }
} 
elasticsearch
1个回答
0
投票

如果我明白你想要什么,你应该能够通过Composite Aggregation进行分页。你仍然可以在你的分页中传递你的size参数,但你的from将是你的关键。

POST my_index/_search
{
  "size": 0, 
  "aggs": {
    "top_artifacts": {
      "composite": {
        "sources": [
          {
            "artifact": {
              "terms": {
                "field": "artifactId.keyword"
              }
            }
          }
        ]
        , 
        "size": 1, // OPTIONAL SIZE (How many buckets)
        "after": {
          "artifact": "FOO_BAZ" // Buckets after this bucket key
        }
      },
      "aggs": {
        "hits": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "timestamp": {
                  "order": "desc"
                }
              }
              ]
          }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.