elasticsearch5.2 高亮java代码

2018/07/03 22:46
阅读数 7

/**
* 高亮代码
*/

public static void search(QueryBuilder qb, String index) {
SearchRequestBuilder sr = transportClient.prepareSearch(index);// .setTypes("infor2");
HighlightBuilder highlightBuilder = new HighlightBuilder();// .field("*").requireFieldMatch(true);
highlightBuilder.preTags("<span style=\"color:red\">");
highlightBuilder.postTags("</span>");
highlightBuilder.field("perFoName");
try {
SearchResponse sResponse = sr.setQuery(qb).setSize(20).highlighter(highlightBuilder).get();

SearchHits hits = sResponse.getHits();

if (hits.totalHits() > 0) {
for (SearchHit hit : hits) {
Map<String, HighlightField> hig = hit.getHighlightFields();

HighlightField emailfiled = hit.getHighlightFields().get("perFoName");
if (emailfiled != null) {
Text[] text = emailfiled.getFragments();
for (Text str : text) {
System.out.println("88888==" + str.string());
}
}
}

}
} catch (Exception e) {
e.printStackTrace();
}

}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部