Highlighting in Percolate function written in NEST Elasticsearch

165 Views Asked by At

I have a percolate function written in NEST (c#). I want to enable the highlighting functionality but did not work. The percolator works fine but the highlight did not return anything, and nothing found in NEST documentation. Any help is very appreciated.

var searchResponseDoc = client.Search<PercolatedQuery>(s => s
           .Query(q => q
           .Percolate(f => f
             .Field(p => p.Query)
             .DocumentType<Document>() //I have a class called Document
             .Document(myDocument))) // myDocument is an object of type Document

           .Highlight(h => h
              .Fields(f => f
              .Field(p => p.Query))
              .PreTags("<em>")
              .PostTags("</em>")
              .FragmentSize(20)));
1

There are 1 best solutions below

0
On BEST ANSWER

The Elasticsearch documentation gives a good example of how to use percolate queries with highlighting. Percolate queries with highlighting work a little differently in that the Fields() in Highlight() should be the fields on the document type to be highlighted.

For example, given

public class Document
{
    public string Content { get; set; }
}

A percolate query with highlighting might look like

var searchResponseDoc = client.Search<PercolatedQuery>(s => s
    .Query(q => q
        .Percolate(f => f
            .Field(p => p.Query)
            .DocumentType<Document>() //I have a class called Document
            .Document(myDocument) // myDocument is an object of type Document
        )
    ) 
    .Highlight(h => h
        .Fields(f => f
            .Field(Infer.Field<Document>(ff => ff.Content))
        )
        .PreTags("<em>")
        .PostTags("</em>")
        .FragmentSize(20)
    )
);

Infer.Field<T>() is used to get the name of the content field on Document as Highlight<T>() is strongly typed to the response type, in this case, PercolatedQuery.