2015-07-09 74 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