`
wx1568905209
  • 浏览: 25126 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

elasticsearch 实现搜索结果高亮,并在前端显示

 
阅读更多

    一开始以为es的高亮只要在dsl语句中添加一个highlight属性就够了,但结果并不是这样的,因为加上之后搜索结果并没有加上颜色样式,带有样式的结果在highlight对象中,所以需要将高亮属性值赋给搜索结果;

"highlight": { "require_field_match": false, "fields": { "*": { "pre_tags": [ "<font color='red'>" ], "post_tags": [ "</font>" ] } } },

"required_field_match"默认为true,要想实现上面的全字段高亮显示,这个属性一定要设置为false;

搜索结果如下:

"hits": [
{
"_index": "standard",
"_type": "all",
"_id": "88358",
"_score": 25.532839,
"_source": { "id": 88358,"bsyears": "2001","bsserialnumber": "SEC III D1 NMA APP S","bsorgtyperedundancy": "6","bsreleasecompany": null,"bsnoteappended": null,"bsscopeofapplication": null,"bsmodifydate": "2015-07-16T10:58:26.000Z","@version": "1","bsnotice": null,"bsreferencerelationship": null,"type": "all","bslanguages": "英语","bssubstitutesstandardredundancy": null,"bschinesesubjectwords": null,"bsenglishsubjectwords": null,"boid": "ASME","bscollection": null,"bzyptid": null,"bsprice": "0","bsstate": "ST","bsenddate": null,"bsadoptionrelationships": null,"bscountry": null,"bscode": null,"bscreationdate": "2015-07-16T10:58:26.000Z","bspublishingunit": null,"bsid": null,"bstransitiondate": null,"bsenglishname": "Appendix S Pump Shaft Design Methods","bsics": null,"bssource": "北京-201112北京全库_asme.mdb","bschinesename": "附录S.水泵轴设计法","@timestamp": "2019-01-17T11:46:13.098Z","bsfolder": null,"bsconfirmdate": null,"bspage": 0,"bsisfile": 1,"bsindexes": null,"bsstateindex": 9,"bsmemo": null,"bsnumber": "ASME SEC III D1 NMA APP S-2001","bsstatename": "现行","bsproposedunit": null,"bsisxml": null,"bsistxt": null,"bsoldname": null,"bsignoreattribute": "ASME SEC III D1 NMA APP S-2001","bstype": null,"bsreleasedate": "2001-06-30T16:00:00.000Z","bsispdf": 1,"orgid": null,"bsimplementdate": null,"bzysid": 88358,"boname": "美国机械工程师协会","bsfilepath": null,"bsfilesize": null,"bsdraftingunit": null,"bsorganizationtype": "国外标准","bsisforeign": null,"bsreviewyears": null,"bsclassify": "F69,J71","bsabolishdate": null,"bsinsteadstandardredundancy": null,"bsabstract": null},
"highlight": {
"bsnumber": [
"ASME SEC III D1 NMA APP <font color='red'>S</font>-2001"
],
"bsserialnumber": [
"SEC III D1 NMA APP <font color='red'>S</font>"
],
"bschinesename": [
"附录<font color='red'>S</font>.水泵轴设计法"
],
"bsenglishname": [
"Appendix <font color='red'>S</font> Pump Shaft Design Methods"
],
"bsignoreattribute": [
"ASME SEC III D1 NMA APP <font color='red'>S</font>-2001"
]
},
"sort": [
9
,
25.532839
]
}

highlight即高亮搜索结果对象,其属性是 名称为es 的index 的属性,值是数组;再遍历高亮结果赋值给搜索结果;

EsHitsItem<BzyStandardEntity>[] hits = resultEntity.getHits().getHits();
List<BzyStandardEntity> bzyStandardEntityList = new ArrayList<>();
for (EsHitsItem<BzyStandardEntity> esHitsItem : hits) {
    EsHighlight highlight = esHitsItem.getHighlight();// EsHighlight为自建高亮结果对象,
    if (null != highlight.getBsnumber() && highlight.getBsnumber().length > 0) {
        esHitsItem.get_source().setBsNumber(highlight.getBsnumber()[0]);
    }
    if (null != highlight.getBschinesename() && highlight.getBschinesename().length > 0) {
        esHitsItem.get_source().setBsChineseName(highlight.getBschinesename()[0]);
    }
    bzyStandardEntityList.add(esHitsItem.get_source());
}
public class EsHighlight {
    private String[] bsnumber;
    private String[] bschinesename;
    public String[] getBschinesename() { return bschinesename; }
    public void setBschinesename(String[] bschinesename) { this.bschinesename = bschinesename; }
    public String[] getBsnumber() { return bsnumber; }
    public void setBsnumber(String[] bsnumber) { this.bsnumber = bsnumber; }
}

转载于:https://my.oschina.net/u/3734816/blog/3006597

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics