elasticsearch 如何在Elastic Search中对聚合进行分页?

oyjwcjzk  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(166)

我有一个aggergation,即自动完成下拉搜索。

GET org-members /_search
{
  "size": 0, 
  "aggs": {
    "members": {
      "terms": {
        "field": "members.keyword",
        "size": 1000
      }
     }
    }
  }
}

您接收所有1000个聚合值。

jpfvwuh4

jpfvwuh41#

简单地使用 * 基数 * 和 bucket_sort
基数:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
bucket_sort排序:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html

GET org-members/_search
{
  "size": 0, 
  "aggs": {
    "total_count_of_aggergation": {
      "cardinality": {
        "field": "members.keyword"
      }
    },
    "members_aggs": {
      "terms": {
        "field": "members.keyword",
        "size": 10000 //put a very high value 
      },
      "aggs": {
        "bucket_sort": {
          "bucket_sort": {
            "from": 0,
            "size": 10
          }
        }
      }
    },
    "totalIds": {
      "stats_bucket": {
        "buckets_path": "members._count"
      }
    }
  }
}

相关问题