分词高亮搜索代码
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);
}
来源:https://www.cnblogs.com/reverseapplepie/p/16779243.html
本站部分图文来源于网络,如有侵权请联系删除。