2015-07-09 45 views
7

我正在使用.From()和.Size()方法从Elastic Search结果中检索所有文档。在ElasticSearch NEST API中滚动示例

下面是样品的例子 -

ISearchResponse<dynamic> bResponse = ObjElasticClient.Search<dynamic>(s => s.From(0).Size(25000).Index("accounts").AllTypes().Query(Query)); 

最近我碰到弹性搜索的滚动功能。这看起来比From()和Size()方法专门用于获取大数据更好。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

我找例如在滚动功能巢API。

有人可以提供NEST例子吗?

谢谢,萨米尔

回答

4

内部实现NEST Reindex的使用滚动到文件从一个索引移动到另一个。

这应该是一个好的起点。

以下你可以找到感兴趣的github。你

var page = 0; 
var searchResult = this.CurrentClient.Search<T>(
    s => s 
     .Index(fromIndex) 
     .AllTypes() 
     .From(0) 
     .Size(size) 
     .Query(this._reindexDescriptor._QuerySelector ?? (q=>q.MatchAll())) 
     .SearchType(SearchType.Scan) 
     .Scroll(scroll) 
    ); 
if (searchResult.Total <= 0) 
    throw new ReindexException(searchResult.ConnectionStatus, "index " + fromIndex + " has no documents!"); 
IBulkResponse indexResult = null; 
do 
{ 
    var result = searchResult; 
    searchResult = this.CurrentClient.Scroll<T>(s => s 
     .Scroll(scroll) 
     .ScrollId(result.ScrollId) 
    ); 
    if (searchResult.Documents.HasAny()) 
     indexResult = this.IndexSearchResults(searchResult, observer, toIndex, page); 
    page++; 
} while (searchResult.IsValid && indexResult != null && indexResult.IsValid && searchResult.Documents.HasAny()); 

也可以看看integration testScroll

[Test] 
public void SearchTypeScan() 
{ 
    var scanResults = this.Client.Search<ElasticsearchProject>(s => s 
     .From(0) 
     .Size(1) 
     .MatchAll() 
     .Fields(f => f.Name) 
     .SearchType(SearchType.Scan) 
     .Scroll("2s") 
    ); 
    Assert.True(scanResults.IsValid); 
    Assert.False(scanResults.FieldSelections.Any()); 
    Assert.IsNotNullOrEmpty(scanResults.ScrollId); 

    var results = this.Client.Scroll<ElasticsearchProject>(s=>s 
     .Scroll("4s") 
     .ScrollId(scanResults.ScrollId) 
    ); 
    var hitCount = results.Hits.Count(); 
    while (results.FieldSelections.Any()) 
    { 
     Assert.True(results.IsValid); 
     Assert.True(results.FieldSelections.Any()); 
     Assert.IsNotNullOrEmpty(results.ScrollId); 
     var localResults = results; 
     results = this.Client.Scroll<ElasticsearchProject>(s=>s 
      .Scroll("4s") 
      .ScrollId(localResults.ScrollId)); 
     hitCount += results.Hits.Count(); 
    } 
    Assert.AreEqual(scanResults.Total, hitCount); 
} 
+0

谢谢。 不支持聚合查询查询类型“扫描”。那么使用没有搜索类型'扫描'的滚动仍然很好? –

+0

是的,但滚动效率会降低https://www.elastic.co/guide/en/elasticsearch/guide/current/scan-scroll.html#scan-scroll。取决于你的用例。 – Rob