JAVA全系列 教程
3762个小节阅读:7095.2k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
重写搜索产品功能
xxxxxxxxxx
@Override
public GoodsSearchResult search(GoodsSearchParam goodsSearchParam) {
// 1.构造ES搜索条件
NativeSearchQuery query = buildQuery(goodsSearchParam);
// 2.搜索
SearchHits<GoodsES> search = template.search(query, GoodsES.class);
// 3.将查询结果封装为Page对象
// 3.1 将SearchHits转为List
List<GoodsES> content = new ArrayList();
for (SearchHit<GoodsES> goodsESSearchHit : search) {
GoodsES goodsES = goodsESSearchHit.getContent();
content.add(goodsES);
}
// 3.2 将List转为MP的Page对象
Page<GoodsES> page = new Page();
page.setCurrent(goodsSearchParam.getPage()) // 当前页
.setSize(goodsSearchParam.getSize()) // 每页条数
.setTotal(search.getTotalHits()) // 总条数
.setRecords(content); // 结果集
// 4.封装结果对象
// 4.1 查询结果
GoodsSearchResult result = new GoodsSearchResult();
result.setGoodsPage(page);
// 4.2 查询参数
result.setGoodsSearchParam(goodsSearchParam);
// 4.3 查询面板
buildSearchPanel(goodsSearchParam,result);
return result;
}
编写封装查询面板功能,查询面板中包含品牌、分类、规格项。根据查询条件,查询出20条最符合的数据,遍历这些数据,找到它们的品牌、分类、规格项,最后封装到查询结果中。写法如下:
xxxxxxxxxx
/**
* 封装查询面板,即根据查询条件,找到查询结果关联度前20名的商品进行封装
* @param goodsSearchParam
* @param goodsSearchResult
*/
public void buildSearchPanel(GoodsSearchParam goodsSearchParam,GoodsSearchResult goodsSearchResult){
// 1.构造搜索条件
goodsSearchParam.setPage(1);
goodsSearchParam.setSize(20);
goodsSearchParam.setSort(null);
goodsSearchParam.setSortFiled(null);
NativeSearchQuery query = buildQuery(goodsSearchParam);
// 2.搜索
SearchHits<GoodsES> search = template.search(query, GoodsES.class);
// 3.将结果封装为List对象
List<GoodsES> content = new ArrayList();
for (SearchHit<GoodsES> goodsESSearchHit : search) {
GoodsES goodsES = goodsESSearchHit.getContent();
content.add(goodsES);
}
// 4.遍历集合,封装查询面板
// 商品相关的品牌列表
Set<String> brands = new HashSet();
// 商品相关的类型列表
Set<String> productTypes = new HashSet();
// 商品相关的规格列表
Map<String, Set<String>> specifications = new HashMap();
for (GoodsES goodsES : content) {
// 获取品牌
brands.add(goodsES.getBrand());
// 获取类型
List<String> productType = goodsES.getProductType();
productTypes.addAll(productType);
// 获取规格
Map<String, List<String>> specification = goodsES.getSpecification();
Set<Map.Entry<String, List<String>>> entries = specification.entrySet();
for (Map.Entry<String, List<String>> entry : entries) {
// 规格名
String key = entry.getKey();
// 规格值
List<String> value = entry.getValue();
// 如果没有遍历出该规格,新增键值对,如果已经遍历出该规格,则向规格中添加规格项
if (!specifications.containsKey(key)){
specifications.put(key,new HashSet(value));
}else{
specifications.get(key).addAll(value);
}
}
}
goodsSearchResult.setBrands(brands);
goodsSearchResult.setProductType(productTypes);
goodsSearchResult.setSpecifications(specifications);
}
编写搜索控制器
xxxxxxxxxx
/**
* 搜索商品
* @param goodsSearchParam 搜索条件
* @return 搜索结果
*/
@PostMapping("/search")
public BaseResult<GoodsSearchResult> search(@RequestBody GoodsSearchParam goodsSearchParam){
GoodsSearchResult result = searchService.search(goodsSearchParam);
return BaseResult.ok(result);
}
测试接口