Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEST (elasticsearch) Highlighting in multiple fields

I have successfully obtained results and highlights using Nest but if I include two fields in which to search for highlights it only uses the last one in construction of the elasticsearch query. e.g. the following

.Query(qry => qry
    .QueryString(qs => qs
        .Query(qString)
    )
)
.Highlight(h => h
    .PreTags("<b>")
    .PostTags("</b>")
    .OnFields(f => f
        .OnField("Title")
        .OnField("Summary")
    )
)

means that I only get highlights returned from the "Summary" field. If I query elasticsearch directly with the equivalent query I can retrieve highlights from both fields. e.g.

{
  "query": {
    "query_string": {
      "query": "apple"
    }
  },
  "highlight": {
    "pre_tags": ["<b>"],
    "post_tags": ["</b>"],
    "fields": {
      "Title": {},
      "Summary": {}
    }
  }
}

Is it possible to do this with Nest? Am I doing something wrong?

like image 459
ceej Avatar asked Aug 21 '13 15:08

ceej


1 Answers

Each highlighted field needs a separate ".OnField".

.Highlight(h => h
    .PreTags("<b>")
    .PostTags("</b>")
    .OnFields(
        f => f.OnField("Title"),
        f => f.OnField("Summary")
    )
)

See another example here.

like image 111
Scott Rice Avatar answered Nov 05 '22 21:11

Scott Rice