Reputation: 713
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>")
));
Upvotes: 2
Views: 939
Reputation: 3325
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()
));
Upvotes: 3
Reputation: 713
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?
Upvotes: 0