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
}