ElasticSearch NEST API 中的滚动示例 [英] Scroll example in ElasticSearch NEST API
问题描述
我正在使用 .From() 和 .Size() 方法从 Elastic Search 结果中检索所有文档.
I am using .From() and .Size() methods to retrieve all documents from Elastic Search results.
以下是示例 -
ISearchResponse<dynamic> bResponse = ObjElasticClient.Search<dynamic>(s => s.From(0).Size(25000).Index("accounts").AllTypes().Query(Query));
最近我遇到了弹性搜索的滚动功能.这看起来比专门用于获取大数据的 From() 和 Size() 方法更好.
Recently i came across scroll feature of Elastic Search. This looks better approach than From() and Size() methods specifically to fetch large data.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
我正在寻找 NEST API 中滚动功能的示例.
I looking for example on Scroll feature in NEST API.
有人可以提供 NEST 示例吗?
Can someone please provide NEST example?
谢谢,萨默尔
推荐答案
下面是一个在 NEST 和 C# 中使用滚动的示例.适用于 5.x 和 6.x
Here's an example of using scroll with NEST and C#. Works with 5.x and 6.x
public IEnumerable<T> GetAllDocumentsInIndex<T>(string indexName, string scrollTimeout = "2m", int scrollSize = 1000) where T : class
{
ISearchResponse<T> initialResponse = this.ElasticClient.Search<T>
(scr => scr.Index(indexName)
.From(0)
.Take(scrollSize)
.MatchAll()
.Scroll(scrollTimeout));
List<T> results = new List<T>();
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<T> loopingResponse = this.ElasticClient.Scroll<T>(scrollTimeout, scrollid);
if (loopingResponse.IsValid)
{
results.AddRange(loopingResponse.Documents);
scrollid = loopingResponse.ScrollId;
}
isScrollSetHasData = loopingResponse.Documents.Any();
}
this.ElasticClient.ClearScroll(new ClearScrollRequest(scrollid));
return results;
}
它来自:http://telegraphrepaircompany.com/elasticsearch-nest-scroll-api-c/
这篇关于ElasticSearch NEST API 中的滚动示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!