突出显示请求的Elasticsearch.NET NEST对象初始化器语法 [英] Elasticsearch.NET NEST Object Initializer syntax for a highlight request

查看:153
本文介绍了突出显示请求的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官方页面,因此似乎无法返回任何内容(OIS)的结果.

And I'm trying to add a highlighter filter but I'm not that familiar with the Object Initializer (OIS) C# syntax. I've checked NEST official pages and SO but can't seem to return any results for specifically the (OIS).

我可以在Nest.SearchRequest类中看到Highlight属性,但是我没有足够的经验(我想)仅从那里构造我所需要的内容-有关如何使用荧光笔的一些示例和解释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!

推荐答案

这是流利的语法:

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

更新

NEST 7.x语法:

NEST 7.x syntax:

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

我的文档类别:

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

这篇关于突出显示请求的Elasticsearch.NET NEST对象初始化器语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆