ES基础-ES实现高亮显示
许多应用都倾向于在每个搜索结果中 高亮 部分文本片段
GET zkgdsgoods/_search
{
"query": {
"match": {
"goodsName": "数码电器"
}
},
"highlight": {
"fields" : {
"goodsName" : {},//要高亮的字段
"goodsIntro" : {}//不会高亮
}
}
}
结构显示
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 491,
"max_score" : 17.428253,
"hits" : [
{
"_index" : "zkgdsgoods",
"_type" : "mchgoods",
"_id" : "189651",
"_score" : 17.428253,
"_source" : {
"goodsImg" : null,
"modifiedTime" : "2019-05-18T09:01:46.000",
"addTime" : "2018-08-17T15:24:10.000",
"goodsSn" : "G57279",
"spec" : "1*1",
"goodsGalleryImgs" : null,
"latestSalePrice" : 7.5,
"latestProcPrice" : null,
"mchGoodsId" : 189651,
"barcode" : "6925787410481",
"goodsName" : "OY-623电源充电器",
"mchNo" : "8181224167928",
"brandCode" : null,
"custNo" : "50000617415",
"isDelete" : "1",
"isAll" : "",
"version" : 57,
"addType" : "01",
"catCode" : "70398",
"unit" : "个",
"createTime" : "2018-08-17T15:24:10.000",
"goodsIntro" : "OY-623电源充电器",
"parentCatCode" : "70383",
"sortOrder" : 1,
"goodsThumb" : null,
"isMarket" : "1"
},
"highlight" : {
"goodsName" : [
"OY-623<em>电</em>源充<em>电</em><em>器</em>"
]
}
},
{
"_index" : "zkgdsgoods",
"_type" : "mchgoods",
"_id" : "188063",
"_score" : 17.098892,
java 版高亮:
SearchResponse response = client.prepareSearch("zkgdsgoods")
.setTypes("mchgoods")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.matchQuery("goodsName","数码电器"))
.addHighlightedField("goodsName")
.setHighlighterPreTags("<font color='red'>")
.setHighlighterPostTags("</font>")
.setSize(2)
.get();
SearchHits hits = response.getHits();
System.out.println("总量"+hits.getTotalHits());