elasticsearch 5.x highlight 高亮
阿新 • • 發佈:2019-01-09
public static Map<String, Object> search(String key,String index,String type,int start,int row){
//建立查詢索引,要查詢的索引庫為index
SearchRequestBuilder builder = getClient().prepareSearch(index);
builder.setTypes(type);
builder.setFrom(start);
builder.setSize(row);
//設定查詢型別:1.SearchType.DFS_QUERY_THEN_FETCH 精確查詢; 2.SearchType.SCAN 掃描查詢,無序
builder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
if(StringUtils.isNotBlank(key)){
// 設定查詢關鍵詞
builder.setQuery(QueryBuilders.multiMatchQuery(key, "title","describe"));
}
//設定是否按查詢匹配度排序
HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
highlightBuilder.preTags("<span style=\"color:red\">");
highlightBuilder.postTags("</span>");
builder.highlighter(highlightBuilder);
//執行搜尋,返回搜尋響應資訊
SearchResponse searchResponse = builder.get();
SearchHits searchHits = searchResponse.getHits();
//總命中數
long total = searchHits.getTotalHits();
Map<String, Object> map = new HashMap<String,Object>();
SearchHit[] hits = searchHits.getHits();
map.put("count", total);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
//highlightFields.size=0??
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//title高亮
HighlightField titleField = highlightFields.get("title");
Map<String, Object> source = hit.getSource();
if(titleField!=null){
Text[] fragments = titleField.fragments();
String name = "";
for (Text text : fragments) {
name+=text;
}
source.put("title", name);
}
//describe高亮
HighlightField describeField = highlightFields.get("describe");
if(describeField!=null){
Text[] fragments = describeField.fragments();
String describe = "";
for (Text text : fragments) {
describe+=text;
}
source.put("describe", describe);
}
list.add(source);
}
map.put("dataList", list);
return map;
}
//建立查詢索引,要查詢的索引庫為index
SearchRequestBuilder builder = getClient().prepareSearch(index);
builder.setTypes(type);
builder.setFrom(start);
builder.setSize(row);
//設定查詢型別:1.SearchType.DFS_QUERY_THEN_FETCH 精確查詢; 2.SearchType.SCAN 掃描查詢,無序
builder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
if(StringUtils.isNotBlank(key)){
// 設定查詢關鍵詞
builder.setQuery(QueryBuilders.multiMatchQuery(key, "title","describe"));
}
//設定是否按查詢匹配度排序
builder.setExplain(true);
//設定高亮顯示HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
highlightBuilder.preTags("<span style=\"color:red\">");
highlightBuilder.postTags("</span>");
builder.highlighter(highlightBuilder);
//執行搜尋,返回搜尋響應資訊
SearchResponse searchResponse = builder.get();
SearchHits searchHits = searchResponse.getHits();
//總命中數
long total = searchHits.getTotalHits();
Map<String, Object> map = new HashMap<String,Object>();
SearchHit[] hits = searchHits.getHits();
map.put("count", total);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
//highlightFields.size=0??
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//title高亮
HighlightField titleField = highlightFields.get("title");
Map<String, Object> source = hit.getSource();
if(titleField!=null){
Text[] fragments = titleField.fragments();
String name = "";
for (Text text : fragments) {
name+=text;
}
source.put("title", name);
}
//describe高亮
HighlightField describeField = highlightFields.get("describe");
if(describeField!=null){
Text[] fragments = describeField.fragments();
String describe = "";
for (Text text : fragments) {
describe+=text;
}
source.put("describe", describe);
}
list.add(source);
}
map.put("dataList", list);
return map;
}