Elasticsearch实现商品搜索
商品搜索
核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
1、根据关键字查询
(1) changgou_service_search项目创建SearchService接口
public interface SearchService {
/**
* 全文检索
* @param paramMap 查询参数
* @return
*/
public Map search(Map<String, String> paramMap) throws Exception;
}
(2) changgou_service_search项目创建SearchService接口实现类SearchServiceImpl
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private ElasticsearchTemplate esTemplate;
//设置每页查询条数据
public final static Integer PAGE_SIZE = 20;
@Override
public Map search(Map<String, String> searchMap) throws Exception {
Map<String, Object> resultMap = new HashMap<>();
//有条件才查询Es
if (null != searchMap) {
//组合条件对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//0:关键词 模糊查询,相当于%关键词% 后面的and可用于多个拼接
if (!StringUtils.isEmpty(searchMap.get("keywords"))) {
boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND));
}
//4. 原生搜索实现类 把查询条件丢进去
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(boolQuery);
//10: 执行查询, 返回结果对象
AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
List<T> list = new ArrayList<>();
//拿到查询到的所有数据
SearchHits hits = searchResponse.getHits();
if (null != hits) {
for (SearchHit hit : hits) {
//格式转换后放入集合中
SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class);
list.add((T) skuInfo);
}
}
return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations());
}
});
//11. 总条数
resultMap.put("total", aggregatedPage.getTotalElements());
//12. 总页数
resultMap.put("totalPages", aggregatedPage.getTotalPages());
//13. 查询结果集合
resultMap.put("rows", aggregatedPage.getContent());
//查询的结果返回给前端
return resultMap;
}
return null;
}
}
(3) changgou_service_search项目创建SearchController
@RestController
@RequestMapping("/sku_search")
public class SearchController {
@Autowired
private EsManagerService esManagerService;
@Autowired
private SearchService searchService;
//对搜索入参带有特殊符号进行处理
public void handlerSearchMap(Map<String,String> searchMap){
if(null != searchMap){
Set<Map.Entry<String, String>> entries = searchMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
if(entry.getKey().startsWith("spec_")){
searchMap.put(entry.getKey(),entry.getValue().replace("+","%2B"));
}
}
}
}
/**
* 全文检索
* @return
*/
@GetMapping
public Map search(@RequestParam Map<String, String> paramMap) throws Exception {
//特殊符号处理
handlerSearchMap(searchMap);
Map resultMap = searchService.search(paramMap);
return resultMap;
}
}
(4)测试的时候可以多拼接keywords=xxx来实现关键字的搜索
2、条件筛选
用户有可能会根据分类搜索、品牌搜索,还有可能根据规格搜索,以及价格搜索和排序操作。根据分类和品牌搜索的时候,可以直接根据指定域搜索,而规格搜索的域数据是不确定的,价格是一个区间搜索,所以我们可以分为三段实现,先实现分类、品牌搜素,再实现规格搜索,然后实现价格区间搜索。
2.1 品牌筛选
页面每次向后台传入对应的分类和品牌,后台据分类和品牌进行条件过滤即可。
修改搜索微服务com.changgou.service.SearchServiceImpl的搜索方法,添加品牌过滤,代码如下:
代码如下:
@Override
public Map search(Map<String, String> searchMap) throws Exception {
Map<String, Object> resultMap = new HashMap<>();
//有条件才查询Es
if (null != searchMap) {
//组合条件对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//0:关键词
if (StringUtils.isNotEmpty(searchMap.get("keywords"))) {
boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND));
}
//1:条件 品牌 精准匹配条件 比如 苹果
if (StringUtils.isNotEmpty(searchMap.get("brand"))) {
boolQuery.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand")));
}
//4. 原生搜索实现类
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(boolQuery);
//6. 品牌聚合(分组)查询 对于这个字段进行聚合查询,类似于group by
String skuBrand = "skuBrand";
nativeSearchQueryBuilder.addAggregation(AggregationBuilders.terms(skuBrand).field("brandName"));
//10: 执行查询, 返回结果对象
AggregatedPage<SkuInfo> aggregatedPage = esTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class, new SearchResultMapper() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
List<T> list = new ArrayList<>();
SearchHits hits = searchResponse.getHits();
if (null != hits) {
for (SearchHit hit : hits) {
SkuInfo skuInfo = JSON.parseObject(hit.getSourceAsString(), SkuInfo.class);
list.add((T) skuInfo);
}
}
return new AggregatedPageImpl<T>(list, pageable, hits.getTotalHits(), searchResponse.getAggregations());
}
});
//11. 总条数
resultMap.put("total", aggregatedPage.getTotalElements());
//12. 总页数
resultMap.put("totalPages", aggregatedPage.getTotalPages());
//13. 查询结果集合
resultMap.put("rows", aggregatedPage.getContent());
//14. 获取品牌聚合结果
StringTerms brandTerms = (StringTerms) aggregatedPage.getAggregation(skuBrand);
List<String> brandList = brandTerms.getBuckets().stream().map(bucket -> bucket.getKeyAsString()).collect(Collectors.toList());
resultMap.put("brandList", brandList);
return resultMap;
}
return null;
}
测试时可多加入brand来实现品牌的指定查询
2.2 规格过滤
规格这一部分,需要向后台发送规格名字以及规格值,我们可以按照一定要求来发送数据,例如规格名字以特殊前缀提交到后台:spec_网络制式:电信4G、spec_显示屏尺寸:4.0-4.9英寸
后台接到数据后,可以根据前缀spec_来区分是否是规格,如果以spec_xxx
开始的数据则为规格数据,需要根据指定规格找信息。
spechMap.规格名字.keyword
修改com.changgou.service.SearchServiceImpl的搜索方法,增加规格查询操作,代码如下:
代码如下:
@Override
public Map search(Map<String, String> searchMap) throws Exception {
Map<String, Object> resultMap = new HashMap<>();
//有条件才查询Es
if (null != searchMap) {
//组合条件对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//0:关键词
if (!StringUtils.isEmpty(searchMap.get("keywords"))) {
boolQuery.must(QueryBuilders.matchQuery("name", searchMap.get("keywords")).operator(Operator.AND));
}
//1:条件 品牌
if (!String