简体   繁体   中英

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. 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.

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>> . You are only passing a single Action<HighlightFieldDescriptor<T>> and calling OnField multiple times on it, which keeps replacing the last value.

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:

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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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