Scroll example in ElasticSearch NEST API

后端 未结 3 2096
故里飘歌
故里飘歌 2020-12-31 10:06

I am using .From() and .Size() methods to retrieve all documents from Elastic Search results.

Below is sample example -



        
3条回答
  •  萌比男神i
    2020-12-31 10:49

    Here's an example of using scroll with NEST and C#. Works with 5.x and 6.x

    public IEnumerable GetAllDocumentsInIndex(string indexName, string scrollTimeout = "2m", int scrollSize = 1000) where T : class
          {
              ISearchResponse initialResponse = this.ElasticClient.Search
                  (scr => scr.Index(indexName)
                       .From(0)
                       .Take(scrollSize)
                       .MatchAll()
                       .Scroll(scrollTimeout));
    
              List results = new List();
    
              if (!initialResponse.IsValid || string.IsNullOrEmpty(initialResponse.ScrollId))
                  throw new Exception(initialResponse.ServerError.Error.Reason);
    
              if (initialResponse.Documents.Any())
                  results.AddRange(initialResponse.Documents);
    
              string scrollid = initialResponse.ScrollId;
              bool isScrollSetHasData = true;
              while (isScrollSetHasData)
              {
                  ISearchResponse loopingResponse = this.ElasticClient.Scroll(scrollTimeout, scrollid);
                  if (loopingResponse.IsValid)
                  {
                      results.AddRange(loopingResponse.Documents);
                      scrollid = loopingResponse.ScrollId;
                  }
                  isScrollSetHasData = loopingResponse.Documents.Any();
              }
    
              this.ElasticClient.ClearScroll(new ClearScrollRequest(scrollid));
              return results;
          }
    

    It's from: http://telegraphrepaircompany.com/elasticsearch-nest-scroll-api-c/

提交回复
热议问题