1. 程式人生 > 實用技巧 >elasticsearch 分頁滾動搜尋結果 scroll

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=="
}