1.1.1 需求分析
所谓高亮,就是使用特别的样式修饰某字段中包含的搜索关键字。
需求:实现搜索高亮,商品名称使用红色显示搜索关键字。
1.1.2实现思路
使用默认高亮显示来获取每个搜索命中title字段的高亮显示,在指定title字段的查询请求中包含高亮显示对象。
1.1.3 代码实现
(1)changgou_service_search工程接口 SearchService 新增方法定义
/*** 全文检索* @param paramMap 查询参数* @return*/public Map search(Map<String, String> paramMap) throws Exception;
(2)SearchServiceImpl 实现此方法,实现关键字查询
package com.changgou.service;import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @author :lijunxuan* @date :Created in 2019/7/17 19:00* @description :* @version: 1.0*/
@Service
public class SearchServiceImpl implements SearchService {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Overridepublic Map search(Map<String, String> paramMap) throws Exception {Map<String,Object> resultMap=new HashMap<>();/*** 1.获取查询参数*/String keywords = paramMap.get("keywords");/*** 2.获取查询需要的对象*///创建查询对象,指定索引库名称,_indexSearchRequest searchRequest = new SearchRequest("sku");//指定查询索引库中的类型searchRequest.types("doc");//创建查询构造对象SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//创建布尔查询(组合查询对象)BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();/*** 3.设置根据关键字查询(关键字需要中文分词)*/MatchQueryBuilder nameQueryBuilder = QueryBuilders.matchQuery("name", keywords);//根据名称查询条件放入组合查询对象中boolQueryBuilder.must(nameQueryBuilder);/*** 4. 设置高亮查询*/HighlightBuilder highlightBuilder = new HighlightBuilder();//设置在哪个域中高亮显示highlightBuilder.field("name");//设置高亮前缀highlightBuilder.preTags("<em style=\"color:red\">");//设置高亮后缀highlightBuilder.postTags("</em>");searchSourceBuilder.highlighter(highlightBuilder);/*** 12.查询并返回的结果*///将组合查询条件放入查询构造对象中searchSourceBuilder.query(boolQueryBuilder);//将组合查询条件放入查询请求中searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);/*** 13. 获取查询到的结果集, 封装后返回*/SearchHits searchHits = searchResponse.getHits();//获取查询到的总条数long totalHits = searchHits.getTotalHits();resultMap.put("total",totalHits);//获取查询结果集SearchHit[] hits = searchHits.getHits();List rows = new ArrayList<>();if (hits!=null){for (SearchHit hit : hits) {//获取普通的查询出来的一条数据(不带高亮名称)Map<String, Object> sourceAsMap = hit.getSourceAsMap();//获取高亮结果Map<String, HighlightField> highlightFields = hit.getHighlightFields();if (highlightFields != null) {HighlightField highlightField = highlightFields.get("name");Text[] fragments = highlightField.fragments();if (fragments != null && fragments.length > 0) {//将高亮名称放入查询出来的数据中, 覆盖原来不带高亮的名称sourceAsMap.put("name", fragments[0].toString());}}rows.add(sourceAsMap);}}resultMap.put("rows",rows);return resultMap;}
}
(3)changgou_service_search新增SearchController类
package com.changgou.controller;import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.service.EsManagerService;import com.changgou.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;/*** @author :lijunxuan* @date :Created in 2019/7/14 21:41* @description :* @version: 1.0*/
@RestController
@RequestMapping("/search")
public class SearchController {@Autowiredprivate SearchService searchService;/*** 全文检索* @return*/@GetMappingpublic Result search(@RequestParam Map<String, String> paramMap) throws Exception {Map resultMap = searchService.search(paramMap);return new Result(true, StatusCode.OK, "查询成功", resultMap);}
}