c# - 高亮请求的 Elasticsearch.NET NEST 对象初始化器语法

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

我正在尝试添加荧光笔过滤器,但我不太熟悉对象初始化器 (OIS) C# 语法。我检查过NEST official pages和 SO,但似乎无法返回任何专门针对 (OIS) 的结果。

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

最佳答案

这是流利的语法:

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

关于c# - 高亮请求的 Elasticsearch.NET NEST 对象初始化器语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30436373/

相关文章:

c# - 是否有将单个项目添加到 IEnumerable<T> 的 Linq 方法?

c# - 通过Nest在Elasticsearch中使用同义词

elasticsearch - 在ElasticSearch中序列化RegionInfo

elasticsearch - NEST:更新源过滤器

c# - 是否可以在 bool 上使用对象初始化器?

c# - 在请求正文中而不是在查询字符串中发送参数

c# - Newtonsoft JSON - 数组未正确序列化

c# - 动态数组 |展开对象 |使用压缩的初始化语法

c# - C#如何在匿名对象初始化时设置只读属性

c# - 在 switch/case 中使用 for 循环