用于突出显示请求的Elasticsearch.NET NEST Object Initializer语法

时间:2015-05-25 10:36:38

标签: c# nest object-initializers elasticsearch-net

我有:

        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"
        });

我正在尝试添加一个荧光笔过滤器,但我并不熟悉Object Initializer(OIS)C#语法。我已经检查了NEST official pages和SO,但似乎无法返回任何具体的(OIS)结果。

我可以在Nest.SearchRequest类中看到Highlight属性,但我没有足够的经验(我猜)只是简单地构建我需要的东西 - 关于如何使用荧光笔的一些示例和解释OIS 会很热!

1 个答案:

答案 0 :(得分:3)

这是流利的语法:

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; } 
}