Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.24 KB

File metadata and controls

53 lines (42 loc) · 1.24 KB
endpoint scroll
lang dotnet
es_version 9.3
client Elastic.Clients.Elasticsearch==9.3.0

Elasticsearch 9.3 scroll endpoint (.NET example)

Use client.ScrollAsync() to retrieve the next batch of results from a scrolling search. Start by calling SearchAsync with a Scroll parameter, then repeatedly call ScrollAsync until no more hits are returned:

public record Product(string Name, string Brand, double Price,
                      string Category, bool InStock, double Rating);

var searchResp = await client.SearchAsync<Product>(s => s
    .Indices("products")
    .Scroll("1m")
    .Size(2)
    .Query(q => q.MatchAll(new MatchAllQuery()))
);

var scrollId = searchResp.ScrollId;
var hits = searchResp.Hits;

while (hits.Count > 0)
{
    foreach (var hit in hits)
    {
        Console.WriteLine($"{hit.Id}: {hit.Source.Name}");
    }

    var scrollResp = await client.ScrollAsync<Product>(sc => sc
        .ScrollId(scrollId)
        .Scroll("1m")
    );

    scrollId = scrollResp.ScrollId;
    hits = scrollResp.Hits;
}

Cleaning up

Always clear the scroll context when you are done to free server resources. Pass the last scroll ID you received:

await client.ClearScrollAsync(c => c.ScrollId(scrollId));