Using fluent lambda expressions with a scroll search request

TSlump
Thu, 09 Oct 2025 14:58:04 +0000
Elasticsearch

I’ve been unable to use fluent lambda expressions when searching with a scroll. I have the code working using direct creations of the request objects:

public async Task<List<int>> GetAllElasticIds()
{
   var ids = new List<int>();

    var searchResponse = await this.elasticsearchClient.SearchAsync<ElasticModel>(s => s
        .Index(“index1”)
        .Query(q => q
            .MatchAll(_ => { }))
        .Size(5)
        .Scroll(“1m”)
        .Source(new SourceConfig(new SourceFilter
        {
            Includes = "id"
        })));

    ids.AddRange(searchResponse.Documents.Select(d => d.Id));

    var scrollId = searchResponse.ScrollId;
    var docCount = searchResponse.Documents.Count;

    while (scrollId is not null && docCount != 0)
    {
        var scrollResponse = await this.elasticsearchClient.ScrollAsync<ElasticModel>(new ScrollRequest{ScrollId = scrollId, Scroll = ScrollTime});

        ids.AddRange(scrollResponse.Documents.Select(d => d.Id));

        scrollId = scrollResponse.ScrollId;
        docCount = scrollResponse.Documents.Count;
    }

    if (scrollId is not null)
    {
           var clearScrollResponse = await this.elasticsearchClient.ClearScrollAsync(new ClearScrollRequest {ScrollId = scrollId});
    }
}

Is there a way I can fluently create the SourceConfig / SourceFilter / ScrollRequest / ClearScrollRequest objects within the request ?

Elasticsearch package version: 8.18

3 posts - 2 participants