Elasticsearch.NET NEST对象初始化语法用于高亮请求

5

我有:

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

我正在尝试添加一个高亮过滤器,但是对于C#语法中的对象初始化程序(OIS),我并不太熟悉。我已经查看了NEST官方页面和SO,但似乎没有针对(OIS)的结果。

我可以在Nest.SearchRequest类中看到Highlight属性,但我没有足够的经验(我猜)仅从那里构建所需内容-一些关于如何使用高亮工具 with OIS 的示例和说明会很棒!

1个回答

5
这是流畅语法:
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);

更新

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

我的文档类:

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

自从版本6推出以来,NEST库已经放弃了 PropertyPathMarker 类。你有什么关于新版本该如何处理的想法吗? - CShark
非常感谢。我发现官方文档不够详细,正在寻找更全面的关于整个NEST API的文档/学习资源。你能推荐任何其他的文档/学习资源吗? - CShark
1
我认为官方文档越来越好了,每次都在进步。如果你在那里找不到任何帮助,我认为一个好主意是在仓库中创建一个 issue,这样开发人员就知道社区正在遇到哪些问题 :) - Rob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接