package com.kwan.shuyu.service; import com.kwan.shuyu.domain.*; import com.kwan.shuyu.service.impl.EsCourseService; import org.apache.commons.lang3.StringUtils; 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.MultiMatchQueryBuilder; 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.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @Service public class EsCourseServiceImpl implements EsCourseService { @Value("${kwan.course.source_field}") private String source_field; @Autowired private RestHighLevelClient restHighLevelClient; /** * 课程搜索 * * @param page * @param size * @param courseSearchParam * @return */ @Override public QueryResponseResult list(int page, int size, CourseSearchParam courseSearchParam) { if (courseSearchParam == null) { courseSearchParam = new CourseSearchParam(); } //1创建搜索请求对象 SearchRequest searchRequest = new SearchRequest("xc_course"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //过虑源字段 String[] source_field_array = source_field.split(","); searchSourceBuilder.fetchSource(source_field_array, new String[]{}); //创建布尔查询对象 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); //搜索条件 //根据关键字搜索 if (StringUtils.isNotEmpty(courseSearchParam.getKeyword())) { MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders .multiMatchQuery(courseSearchParam.getKeyword(), "name", "description", "teachplan") .minimumShouldMatch("70%") .field("name", 10); boolQueryBuilder.must(multiMatchQueryBuilder); } if (StringUtils.isNotEmpty(courseSearchParam.getMt())) { //根据一级分类 boolQueryBuilder.filter(QueryBuilders.termQuery("mt", courseSearchParam.getMt())); } if (StringUtils.isNotEmpty(courseSearchParam.getSt())) { //根据二级分类 boolQueryBuilder.filter(QueryBuilders.termQuery("st", courseSearchParam.getSt())); } if (StringUtils.isNotEmpty(courseSearchParam.getGrade())) { //根据难度等级 boolQueryBuilder.filter(QueryBuilders.termQuery("grade", courseSearchParam.getGrade())); } //设置boolQueryBuilder到searchSourceBuilder searchSourceBuilder.query(boolQueryBuilder); //设置分页参数 if (page <= 0) { page = 1; } if (size <= 0) { size = 12; } //起始记录下标 int from = (page - 1) * size; searchSourceBuilder.from(from); searchSourceBuilder.size(size); //设置高亮 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.preTags(""); highlightBuilder.postTags(""); //设置高亮字段 highlightBuilder.fields().add(new HighlightBuilder.Field("name")); searchSourceBuilder.highlighter(highlightBuilder); searchRequest.source(searchSourceBuilder); QueryResult queryResult = new QueryResult(); List list = new ArrayList<>(); try { //2执行搜索 SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); //3获取响应结果 SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits().value; //匹配的总记录数 queryResult.setTotal(totalHits); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { CoursePub coursePub = new CoursePub(); //源文档 Map sourceAsMap = hit.getSourceAsMap(); //取出id String id = (String) sourceAsMap.get("id"); coursePub.setId(id); //取出name String name = (String) sourceAsMap.get("name"); //取出高亮字段name Map highlightFields = hit.getHighlightFields(); if (highlightFields != null) { HighlightField highlightFieldName = highlightFields.get("name"); if (highlightFieldName != null) { Text[] fragments = highlightFieldName.fragments(); StringBuffer stringBuffer = new StringBuffer(); for (Text text : fragments) { stringBuffer.append(text); } name = stringBuffer.toString(); } } coursePub.setName(name); //图片 String pic = (String) sourceAsMap.get("pic"); coursePub.setPic(pic); //价格 Double price = null; try { if (sourceAsMap.get("price") != null) { price = (Double) sourceAsMap.get("price"); } } catch (Exception e) { e.printStackTrace(); } coursePub.setPrice(price); //旧价格 Double price_old = null; try { if (sourceAsMap.get("price_old") != null) { price_old = (Double) sourceAsMap.get("price_old"); } } catch (Exception e) { e.printStackTrace(); } coursePub.setPrice_old(price_old); //将coursePub对象放入list list.add(coursePub); } } catch (IOException e) { e.printStackTrace(); } queryResult.setList(list); QueryResponseResult queryResponseResult = new QueryResponseResult<>(CommonCode.SUCCESS, queryResult); return queryResponseResult; } }