本文整理匯總了Java中org.elasticsearch.action.search.SearchRequestBuilder.setHighlighterPreTags方法的典型用法代碼示例。如果您正苦於以下問題:Java SearchRequestBuilder.setHighlighterPreTags方法的具體用法?Java SearchRequestBuilder.setHighlighterPreTags怎麽用?Java SearchRequestBuilder.setHighlighterPreTags使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.elasticsearch.action.search.SearchRequestBuilder
的用法示例。
在下文中一共展示了SearchRequestBuilder.setHighlighterPreTags方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: queryForObject
import org.elasticsearch.action.search.SearchRequestBuilder; //導入方法依賴的package包/類
/**
* 查詢返回一個Map對象
*
* @param esIndexName
* @param type
* @param fields
* @param content
* @param sortField
* @param order
* @param from
* @param size
* @return
*/
public List<Map<String, Object>> queryForObject(String esIndexName, String type, String[] fields, String content, String sortField, SortOrder order, List<String> heightFields, int from, int size) {
SearchRequestBuilder reqBuilder = esClient.prepareSearch(esIndexName)
.setTypes(type).setSearchType(SearchType.DEFAULT)
.setExplain(true);
QueryStringQueryBuilder queryString = QueryBuilders.queryStringQuery("\"" + content + "\"");
for (String k : fields) {
queryString.field(k);
}
queryString.minimumShouldMatch("10");
reqBuilder.setExplain(true);
reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
.setExplain(true);
if (StringUtils.isNotEmpty(sortField) && order != null) {
reqBuilder.addSort(sortField, order);
}
if (from >= 0 && size > 0) {
reqBuilder.setFrom(from).setSize(size);
}
//設置高亮顯示
if (heightFields != null) {
for (String hs : heightFields)
reqBuilder.addHighlightedField(hs);
}
reqBuilder.setHighlighterPreTags("<span style=\"color:red\">");
reqBuilder.setHighlighterPostTags("</span>");
SearchResponse resp = reqBuilder.execute().actionGet();
SearchHit[] hits = resp.getHits().getHits();
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
Map<String, Object> map = new HashMap<String, Object>();
for (String key : hit.getSource().keySet()) {
if (heightFields != null && heightFields.contains(key)) {
map.put(key, hit.getHighlightFields().get(key).fragments()[0]);
} else
map.put(key, hit.getSource().get(key));
}
results.add(map);
}
return results;
}