how to get all documents by index in Easticsearch using NEST?

前端 未结 1 723
暗喜
暗喜 2021-01-27 10:17

I want to GET all my documents by Index. I have tried the following:

var response = client.Search(s => s.Index(\"test\").MatchAll());

the response returns \"succ

1条回答
  •  别那么骄傲
    2021-01-27 10:55

    To get all documents within an index, you'll want to use the Scroll API. Note that depending on how many documents we're talking about, it's likely that you'll receive them in batches through multiple HTTP requests/responses.

    There's a helper in NEST for making this easier, ScrollAll()

    Time processTimePerScroll = "20s";
    int numberOfSlices = Environment.ProcessorCount;
    
    var scrollAllObservable = client.ScrollAll(processTimePerScroll, numberOfSlices, sc => sc
        .MaxDegreeOfParallelism(numberOfSlices)
        .Search(s => s
            .Query(q => q
                .MatchAll()
            )
        )
    )
    
    var waitHandle = new ManualResetEvent(false);
    Exception exception = null;
    
    var scrollAllObserver = new ScrollAllObserver(
        onNext: response => 
        {
            // do something with the documents
            var documents = response.SearchResponse.Documents;
        },
        onError: e =>
        {
            exception = e;
            waitHandle.Set();
        },
        onCompleted: () => waitHandle.Set()
    );
    
    
    scrollAllObservable.Subscribe(scrollAllObserver);
    
    waitHandle.WaitOne();
    
    if (exception != null) 
    {
        throw exception;    
    }
    

    0 讨论(0)
提交回复
热议问题