问题描述
我想按索引获取我的所有文档.我尝试了以下方法:
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.
推荐答案
要在索引中获取所有所有文档,您需要使用滚动API .请注意,根据我们正在讨论的文档数量,您可能会通过多个HTTP请求/响应分批接收它们.
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中按索引获取所有文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!