首页 > 其他 > 详细

elasticsearch NEST 聚合分页

时间:2020-10-27 16:17:55      阅读:84      评论:0      收藏:0      [点我收藏+]

NEST聚合分页

从代码里可以看见用的是Composite聚合,使用AfterKey可以轻松获取到分页数据。

var client = provider.GetClient();
CompositeBucketAggregate composite = null;
IReadOnlyCollection<CompositeBucket> postIdBuckets = null;
do
{
    CompositeKey after = null;
    if (composite != null)
    {
        after = composite.AfterKey;
    }
    var searchResponse = client.Search<PostComments>(s => s
        .Index("post_comments")
        .Size(0)
        .Aggregations(aggs => aggs
            .Composite("composite", c => c
                .Sources(t => t
                    .Terms("postId", t => t
                        .Field(t => t.PostId)
                        )
                    )
                .Size(5000)
                .After(after)
                )
            )
        );
    composite = searchResponse.Aggregations.Composite("composite");
    postIdBuckets = composite.Buckets;
    var postIds = postIdBuckets.Select(t => new { ruid = t.Key["postId"] });
                
} while (postIdBuckets.Count > 0);

Http请求

http请求中更加直观,composite-after-postId确定了分页的起始位置。

POST http://127.0.0.1:9200/post_comments/_search

{
  "aggs": {
    "composite": {
      "composite": {
        "after": {
          "postId": "f12e6967-b379-4ddb-a445-eb397ee41f20"
        },
        "size": 5000,
        "sources": [
          {
            "postId": {
              "terms": {
                "field": "postId"
              }
            }
          }
        ]
      }
    }
  },
  "size": 0
}

文档分页

文档分页

elasticsearch NEST 聚合分页

原文:https://www.cnblogs.com/naergaga/p/13884877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!