Highlighting Using Nest

We are trying to use the Highlighting feature , but we just want to update each document that has the relevant highlights. For example:

var hits = searchResponse.Hits.Where(h => h.Highlight.Count > 0).Select(p => { p.Source.Keywords= String.Join(",", p.Highlight.Values); return p; });

Here we are throwing all the highlighted keywords into a field called Keywords.

The problem is that the hits items are updated but when you come to get the actual documents. i.e. response.Documents, the documents do not have this update.

How can we achieve this.
many thanks

The mutated documents will be available on hits

var hits = searchResponse.Hits.Where(h => h.Highlight.Count > 0).Select(p => { p.Source.Keywords= String.Join(",", p.Highlight.Values); return p; });

foreach (var hit in hits)
{
	var document = hit.Source;
    // do something with document
}

Hi
Thanks for responding.

Am I correct that if we ask for a highlight of a word/phrase, if there are 20 occurrences, then all 20 is returned?

If so, is there any way to ask Elastic to return just one instance as we only need to know the existence of it as opposed to wanting to highlight every occurrence.

Many thanks in advance.

Highlighting will highlight the number of fragments specified by number_of_fragments in the targeted fields. The default is 5.

As always, Many thanks.

Sorry to bother you again but setting the number_of_fragments produces incorrect results.
Imaging we are sending 3 keywords : keyword1, keyword2 and keyword3.

If there are 50 occurrences of all the keywords, then we should get back just 3items, keyword1, keyword2 and keyword3.

if there are 20 occurrences of keyword1 and one occurrence of keyword2, then we should get back just 2 items keyword1 and keyword2.

If we limit the number of fragments, ES just brings back the first ones it finds without caring which keywords had in fact been matched.

Something like this, but I don't know if thee is a more efficient way of achieving this:

var hits = searchResponse.Hits.Where(h => h.Highlight.Count > 0).Select(p => { p.Source.PostCode1 = String.Join(",", p.Highlight.Values.FirstOrDefault().Select(x=>x.ToLower()).Distinct()); return p; });

and here is the query:

.Highlight(h => h
.PreTags("")
.PostTags("")
.FragmentSize(1)
//.NumberOfFragments(3)
//.MaxFragmentLength(0)
.Fields(
fs => fs
.Field(p => p.Summary)
.HighlightQuery(q => q
.Match(m => m
//.Slop(2)
.Field(p => p.Summary)
.Query("keyword1 keyword2 keyword3")
)
)
)
)

Thank in advance.

You can use order to exert some influence over the fragment output order, based on score for example (default order is the order in which they are found).

Highlighting doesn't return distinct highlighted terms in this manner. Take a look at how highlighters work internally.

Thanks. Need to come up with another strategy to get this functionality.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.