如何使用NEST在Easticsearch中按索引获取所有文档? [英] how to get all documents by index in Easticsearch using NEST?
问题描述
我想按索引获取我的所有文档.我尝试了以下方法:
I want to GET all my documents by Index. I have tried the following:
var response = client.Search(s => s.Index("test").MatchAll());
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.
推荐答案
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.
NEST中有一个帮助程序,它可以简化此操作, ScrollAll()
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;
}
这篇关于如何使用NEST在Easticsearch中按索引获取所有文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!