elasticsearch 實現搜索結果高亮,並在前端顯示

    一開始覺得es的高亮只要在dsl語句中添加一個highlight屬性就夠了,但結果並非這樣的,由於加上以後搜索結果並無加上顏色樣式,帶有樣式的結果在highlight對象中,因此須要將高亮屬性值賦給搜索結果;數組

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

"required_field_match"默認爲true,要想實現上面的全字段高亮顯示,這個屬性必定要設置爲false;app

搜索結果以下:post

"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 的屬性,值是數組;再遍歷高亮結果賦值給搜索結果;ui

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; }
}
相關文章
相關標籤/搜索