0

I'm trying to understand why an elasticsearch.net NEST scroll call keeps returning the same results. I have an outer loop in my C# application that tracks the current page and it's passed in along with the batch size. I've simplified the code:

List<int> ids = GetIds();
int count = _batchSize;
int currentPage = 0;

while (count == _batchSize)
{
    var results = Execute(client => client.Search<Invoice>(s => s
                .Index(indexName)
                .Query(q => q
                .Terms(n => n
                .Field(f => f.Items.FirstOrDefault().MyInformation.FirstOrDefault().ItemID)
                .Terms(ids)))
                .Size(batchSize)
                .From(currentPage * batchSize)
                .Scroll("1m")
           ));

    DoSomethingWithResults();
    count = results.Count();
    currentPage++;
}

In the above call the list of ids are ids of nested elements in a one to many relationship with the objects they're contained within. That's why I want to use scroll as I don't know how many Invoice objects will be returned. Every time this is called currentPage is incremented by 1. I had assumed that the next batch from the scroll would be returned.

I think I'm doing something wrong as I'm looking at this as more of a paging flow.

1 Answer 1

4

This isn't quite how the Scroll API works.

  1. First request is to .Search<T>() (or SearchAsync<T>()), specifying .Scroll() time. the response will contain the first set of documents
  2. Subsequent requests are to .Scroll<T> (or ScrollAsync<T>()), passing in the scroll id from the previous request, and a scroll time to keep the scroll open on Elasticsearch. Loop sending scroll requests until a response does not contain any documents.

Take a look at this answer for an example

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again Russ! I hadn't noticed that the first and subsequent retrievals were using different call types. Everything is working working as planed now, I'm excited!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.