• elasticsearch NEST 聚合分页


    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.Key["postId"]);
                    
    } while (postIdBuckets.Count > 0);
    

    Http请求

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

    POST http://127.0.0.1:9200/post_comments/_search
    Content-Type: application/json
    
    {
      "aggs": {
        "composite": {
          "composite": {
            "after": {
              "postId": "f12e6967-b379-4ddb-a445-eb397ee41f20"
            },
            "size": 5000,
            "sources": [
              {
                "postId": {
                  "terms": {
                    "field": "postId"
                  }
                }
              }
            ]
          }
        }
      },
      "size": 0
    }
    

    文档分页

    文档分页

  • 相关阅读:
    ASP.NET DropDownList 控件绑定数据
    Linux图形界面与命令行界面切换
    Linux性能分析
    UTF虚拟对象
    UFT场景恢复
    UFT参数化
    UFT检查点
    UFT三种录制方式
    UFT基本操作
    UFT安装目录简单介绍
  • 原文地址:https://www.cnblogs.com/naergaga/p/13884877.html
Copyright © 2020-2023  润新知