简体   繁体   English

突出显示请求的 Elasticsearch.NET NEST 对象初始值设定项语法

[英]Elasticsearch.NET NEST Object Initializer syntax for a highlight request

I've got:我有:

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

And I'm trying to add a highlighter filter but I'm not that familiar with the Object Initializer (OIS) C# syntax.我正在尝试添加一个荧光笔过滤器,但我对对象初始值设定项 (OIS) C# 语法不太熟悉。 I've checked NEST official pages and SO but can't seem to return any results for specifically the (OIS).我已经检查了NEST 官方页面和 SO,但似乎无法针对(OIS)返回任何结果。

I can see the Highlight property in the Nest.SearchRequest class but I'm not experienced enough (I guess) to simply construct what I need from there - some examples and explanations as to how to employ a highlighter with OIS would be hot!我可以在 Nest.SearchRequest 类中看到 Highlight 属性,但我没有足够的经验(我猜)从那里简单地构建我需要的东西 - 关于如何使用带有 OIS的荧光笔的一些示例和解释会很热门!

This is the fluent syntax:这是流畅的语法:

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

and this is by object initialization:这是通过对象初始化:

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

UPDATE更新

NEST 7.x syntax: NEST 7.x 语法:

var searchQuery = new SearchRequest
{
    Highlight = new Highlight
    {
        Fields = new FluentDictionary<Field, IHighlightField>()
            .Add(Nest.Infer.Field<Document>(d => d.Name),
                new HighlightField {PreTags = new[] {"<tag>"}, PostTags = new[] {"<tag>"}})
    }
};

My document class:我的文档类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM