elasticsearch 分页滚动搜索结果 scroll

目的

scroll用于获取大批量数据。由于elasticsearch获取文档数量有限制,所以需要使用scroll

NEST

scrolling-documents

var searchResponse = client.Search<PostComment>(s => s
    .Index("post_comments")
    .Scroll("10s")
    .Size(100)
    .Query(q => q
        .Match(m => m
            .Field(f => f.Content)
            .Query("text")
            )
        )
    );

List<PostComment> list = new List<PostComment>();
while (searchResponse.Documents.Any())
{
    list.AddRange(searchResponse.Documents);
    searchResponse = client.Scroll<PostComment>("10s", searchResponse.ScrollId);
}

Console.WriteLine(list.Count);

HTTP

第一次请求

POST http://localhost:9200/post_comments/_search?typed_keys=true&scroll=10s HTTP/1.1
Content-Type: application/json

{
  "query": { "match": { "content": { "query": "text" } } },
  "size": 100
}

后续的多次请求

POST http://localhost:9200/_search/scroll HTTP/1.1
Content-Type: application/json

{
  "scroll": "10s",
  "scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFEpvYm9oblVCUFFkMXN4Q3hNbTQzAAAAAAAAAA0WcV80QUZCRmZUSDJlV2RzZGo3anFvZw=="
}
posted @ 2020-11-02 11:31  slowstart  阅读(240)  评论(0编辑  收藏  举报