NEST (elasticsearch) Highlighting in multiple fields

别等时光非礼了梦想. 提交于 2019-12-10 13:49:41

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18361433/nest-elasticsearch-highlighting-in-multiple-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!