0
votes

im just starting to use elasticsearch within a C# project. i want to show the searched terms as highlights in the results page but dont know how i handle the display of them.

my query is as follows

result = client.Search<MyContentClass>(s => s
            .Query(a => 

            a.MatchPhrase(m => m.OnField("_all").Query(m_strSearchQuery))

            .From(intFrom)
            .Size(intSize)
            .Highlight(h => h
            .PreTags("<b style='color:orange'>")
            .PostTags("</b>")
            .OnFields(f => f
            .OnField(e => e.Title)
            .OnField(e => e.Content)                
            )
            )
            ); 

Then i set my results to a variable that is the data for my repeater

var documents = result.Hits.Select(h => h.Source);

this.rptSearch.DataSource = documents;
    this.rptSearch.DataBind();
    this.rptSearch.Visible = true;

i dont see any of the terms highlighted in my results nor do i see the terms wrapped in the highlight tags...

what have i not done right?

1
did you ever find a solution for that? - Emil

1 Answers

-1
votes

Highlights are stored in Hightlights property of Hit object.

This is how you may want to access them:

result.Hits.Select(h => h.Highlights.Values.Select(v => string.Join(", ", v.Highlights)))

Hope it helps.