简体   繁体   English

如何在巢中为Elasticsearch添加动态突出显示字段?

[英]how do i add dynamic highlight fields in nest for elasticsearch?

i want to be able to dynamically add fields for highlighting in elasticsearch using nest. 我希望能够动态添加字段,以便在使用巢的elasticsearch中突出显示。 currently it looks like it's not a able to be iterated in any fashion. 目前看来,它无法以任何方式进行迭代。

i've tried iterating within the .OnFields function in order to produce a list of .OnField functions, but it says it's not iterable. 我已经尝试在.OnFields函数内进行迭代以生成.OnField函数的列表,但是它说这是不可迭代的。

in this example, i want to dynamically add 'artist' and 'title' and add/remove others based on user input. 在此示例中,我想动态添加“艺术家”和“标题”,并根据用户输入添加/删除其他人。 is this possible? 这可能吗?

s.Highlight(h => h
    .OnFields(f => f
        .OnField("artist")
        .OnField("title")
        .PreTags("<em>")
        .PostTags("</em>")
    ));

Highlight takes an array of Action<HighlightFieldDescriptor<T>> . 高亮显示一个Action<HighlightFieldDescriptor<T>>数组。 You are only passing a single Action<HighlightFieldDescriptor<T>> and calling OnField multiple times on it, which keeps replacing the last value. 您只传递了一个Action<HighlightFieldDescriptor<T>>并对其多次调用OnField,这将继续替换最后一个值。

It should be this instead: 应该是这样的:

s.Highlight(h => h
    .OnFields(
        f => f.OnField("artist").PreTags("<em>").PostTags("</em>"),
        f => f.OnField("title").PreTags("<em>").PostTags("</em>")
    ));

From the code in your follow up post, here's a solution using LINQ: 根据后续文章中的代码,这是使用LINQ的解决方案:

s.Highlight(h => h
    .OnFields(
            SearchFields(searchDescriptor.SearchModifier).Select(x => new Action<HighlightFieldDescriptor>(f => f.OnField(x))).ToArray()
        ));

i realized i had confused a couple of types: 我意识到我已经混淆了两种类型:

HighlightFieldDescriptor and HighlightDescriptor. HighlightFieldDescriptor和HighlightDescriptor。 sorry. 抱歉。 here's my implementation (so i can mark as answered) 这是我的实现(所以我可以标记为已回答)

s.Highlight(h => h
            .OnFields(f => 
                GetFieldsHighligthDescriptor(searchDescriptor, f)
            )
        );

private void GetFieldsHighligthDescriptor(SearchQueryDescriptor searchDescriptor, HighlightFieldDescriptor<Product> f)
    {
        foreach (var b in SearchFields(searchDescriptor.SearchModifier))
        {
            f.OnField(b);
        }
    }

EDIT 编辑

actually, this isn't working because it's only return the last entry in my SearchFields array... back to the drawing board? 实际上,这是行不通的,因为它只返回了SearchFields数组中的最后一个条目...回到了绘图板上?

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

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