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

依然范特西╮ 提交于 2020-07-23 14:11:14

问题


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 "successful operation" but it hits no document despite the fact that there are many documents under that index.


回答1:


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<Person>(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<Person>(
    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;    
}


来源:https://stackoverflow.com/questions/55115517/how-to-get-all-documents-by-index-in-easticsearch-using-nest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!