分詞高亮搜索代碼 List<A> list = new ArrayList<>(); //設置高亮顯示 HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false); hi ...
分詞高亮搜索代碼
List<A> list = new ArrayList<>();
//設置高亮顯示
HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
highlightBuilder.preTags("<em>");
highlightBuilder.postTags("</em>");
QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(keyword);
queryBuilder.field("分詞欄位");
//搜索
SearchRequestBuilder searchRequestBuilder = elasticsearchTemplate.getClient().prepareSearch(PublicConst.MT_LAW_INDEX_NAME)
.setFrom(開始條數)
.setSize(獲取多少條)
.setTypes(數據所在type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder)
.highlighter(highlightBuilder)
.setExplain(true); //設置是否按查詢匹配度排序
SearchResponse searchResponse = searchRequestBuilder.get();
//獲取搜索結果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
page.setTotal(searchHits.totalHits);
// 迴圈搜索結果
if (hits != null && hits.length != 0) {
for (SearchHit hit : hits) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
Map<String, Object> source = hit.getSource();
//處理高亮 獲取高亮字元串
if (highlightFields != null && highlightFields.size() != 0) {
HighlightField titleField = highlightFields.get("content");
if (titleField != null) {
Text[] fragments = titleField.fragments();
if (fragments != null && fragments.length != 0) {
StringBuilder name = new StringBuilder();
for (Text text : fragments) {
name.append(text);
}
// 高亮設置完欄位
source.put("contentkey", name.toString());
}
}
}
// 組織返回對象
orderModel = new A();
orderModel.setId(Long.parseLong(source.get("id").toString()));
orderModel.setContent(source.get("content").toString());
orderModel.setCode(source.get("code").toString());
list.add(orderModel);
}
}
普通搜索
List<A> list = new ArrayList<>();
SearchRequestBuilder searchRequestBuilder = elasticsearchTemplate.getClient().prepareSearch(PublicConst.MT_LAW_INDEX_NAME);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("精確匹配欄位", "匹配的值");
searchRequestBuilder = searchRequestBuilder.setQuery(QueryBuilders.boolQuery().must(termQueryBuilder))
.setFrom(分頁開始)
.setSize(取多少個)
.addSort("排序欄位", SortOrder.ASC);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
//獲取搜索結果
SearchHits searchHits = searchResponse.getHits();
page.setTotal(searchHits.totalHits);
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSource();
A orderModel = new A();
orderModel.setId(Long.parseLong(source.get("id").toString()));
orderModel.setContent(source.get("content").toString());
orderModel.setCode(source.get("code").toString());
list.add(orderModel);
}
聚合搜索
List<A> resList = new ArrayList<>();
//封裝了獲取RestHighLevelClient的方法
Client client = elasticsearchTemplate.getClient();
// 查詢的索引
SearchRequest request = new SearchRequest("zuolin_mt_law_hotlaw");
// 構建查詢
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 設置查詢結果的一個別名隨便起 , 還有需要聚合的欄位(設置一個桶)
searchSourceBuilder.aggregation(AggregationBuilders.terms("classroom_term").field("hotkey"));
request.source(searchSourceBuilder);
ActionFuture<SearchResponse> search = client.search(request);
//獲取數據時首先對classroom分桶 , 如果前面設置多個欄位分組(多個桶) , 這裡需要嵌套for迴圈來獲取
Terms classroomTerm = search.get().getAggregations().get("classroom_term");
for (Terms.Bucket classroomBucket : classroomTerm.getBuckets()) {
System.out.println("classRoom:" + classroomBucket.getKeyAsString() + "count:" + classroomBucket.getDocCount());
A temp = new A();
temp.setHotkey(classroomBucket.getKeyAsString());
temp.setCount(Long.toString(classroomBucket.getDocCount()));
resList.add(temp);
}