本篇文章为你整理了ES搜索高亮分词排序聚合搜索(es全文检索 分词策略)的详细内容,包含有es 搜索排序 es全文检索 分词策略 es分词查询精确匹配 elasticsearch 分词搜索 ES搜索高亮分词排序聚合搜索,希望能帮助你了解 ES搜索高亮分词排序聚合搜索。
//设置高亮显示
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);
以上就是ES搜索高亮分词排序聚合搜索(es全文检索 分词策略)的详细内容,想要了解更多 ES搜索高亮分词排序聚合搜索的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。