2015-05-25 66 views
1

我有:Elasticsearch.NET NEST對象初始化語法

 var result = _client.Search<ElasticFilm>(new SearchRequest("blaindex", "blatype") 
     { 
      From = 0, 
      Size = 100, 
      Query = titleQuery || pdfQuery, 
      Source = new SourceFilter 
      { 
       Include = new [] 
       { 
        Property.Path<ElasticFilm>(p => p.Url), 
        Property.Path<ElasticFilm>(p => p.Title), 
        Property.Path<ElasticFilm>(p => p.Language), 
        Property.Path<ElasticFilm>(p => p.Details), 
        Property.Path<ElasticFilm>(p => p.Id) 
       } 
      }, 
      Timeout = "20000" 
     }); 

而且我嘗試添加高亮過濾器,但我不認爲熟悉的對象初始化(OIS) C#語法。我檢查了NEST official pages和SO,但似乎無法返回專門針對(OIS)的任何結果。

我可以看到Nest.SearchRequest類中的Highlight屬性,但我沒有足夠的經驗(我猜)只是簡單地從那裏構造我需要的東西 - 一些示例和解釋如何在OIS中使用熒光筆會很熱!

回答

2

這是一口流利的語法:

var response= client.Search<Document>(s => s 
    .Query(q => q.Match(m => m.OnField(f => f.Name).Query("test"))) 
    .Highlight(h => h.OnFields(fields => fields.OnField(f => f.Name).PreTags("<tag>").PostTags("</tag>")))); 

,這是由對象初始化:

var searchRequest = new SearchRequest 
{ 
    Query = new QueryContainer(new MatchQuery{Field = Property.Path<Document>(p => p.Name), Query = "test"}), 
    Highlight = new HighlightRequest 
    { 
     Fields = new FluentDictionary<PropertyPathMarker, IHighlightField> 
     { 
      { 
       Property.Path<Document>(p => p.Name), 
       new HighlightField {PreTags = new List<string> {"<tag>"}, PostTags = new List<string> {"</tag>"}} 
      } 
     } 
    } 
}; 

var searchResponse = client.Search<Document>(searchRequest); 

我的文檔類:

public class Document 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
}