fix: 优化es商品搜索及es商品特定查询条件的分页

This commit is contained in:
paulGao 2022-10-24 14:37:36 +08:00
parent 7d5e609f7b
commit a272e53519
4 changed files with 17 additions and 9 deletions

View File

@ -90,9 +90,7 @@ public class FootprintServiceImpl extends ServiceImpl<FootprintMapper, FootPrint
return esGoodsIndexIPage; return esGoodsIndexIPage;
} else { } else {
List<EsGoodsIndex> list = esGoodsSearchService.getEsGoodsBySkuIds( List<EsGoodsIndex> list = esGoodsSearchService.getEsGoodsBySkuIds(
footPrintPages.getRecords().stream().map(FootPrint::getSkuId).collect(Collectors.toList())); footPrintPages.getRecords().stream().map(FootPrint::getSkuId).collect(Collectors.toList()), pageVO);
//去除为空的商品数据
list.removeIf(Objects::isNull);
esGoodsIndexIPage.setPages(footPrintPages.getPages()); esGoodsIndexIPage.setPages(footPrintPages.getPages());
esGoodsIndexIPage.setRecords(list); esGoodsIndexIPage.setRecords(list);

View File

@ -348,7 +348,7 @@ public class CartServiceImpl implements CartService {
if (tradeDTO.getSkuList() != null && !tradeDTO.getSkuList().isEmpty()) { if (tradeDTO.getSkuList() != null && !tradeDTO.getSkuList().isEmpty()) {
List<String> ids = tradeDTO.getSkuList().stream().filter(i -> Boolean.TRUE.equals(i.getChecked())).map(i -> i.getGoodsSku().getId()).collect(Collectors.toList()); List<String> ids = tradeDTO.getSkuList().stream().filter(i -> Boolean.TRUE.equals(i.getChecked())).map(i -> i.getGoodsSku().getId()).collect(Collectors.toList());
List<EsGoodsIndex> esGoodsList = esGoodsSearchService.getEsGoodsBySkuIds(ids); List<EsGoodsIndex> esGoodsList = esGoodsSearchService.getEsGoodsBySkuIds(ids, null);
for (EsGoodsIndex esGoodsIndex : esGoodsList) { for (EsGoodsIndex esGoodsIndex : esGoodsList) {
if (esGoodsIndex != null && esGoodsIndex.getPromotionMap() != null && !esGoodsIndex.getPromotionMap().isEmpty()) { if (esGoodsIndex != null && esGoodsIndex.getPromotionMap() != null && !esGoodsIndex.getPromotionMap().isEmpty()) {
List<String> couponIds = esGoodsIndex.getPromotionMap().keySet().stream().filter(i -> i.contains(PromotionTypeEnum.COUPON.name())).map(i -> i.substring(i.lastIndexOf("-") + 1)).collect(Collectors.toList()); List<String> couponIds = esGoodsIndex.getPromotionMap().keySet().stream().filter(i -> i.contains(PromotionTypeEnum.COUPON.name())).map(i -> i.substring(i.lastIndexOf("-") + 1)).collect(Collectors.toList());

View File

@ -40,7 +40,7 @@ public interface EsGoodsSearchService {
* @param skuIds SkuId列表 * @param skuIds SkuId列表
* @return ES商品列表 * @return ES商品列表
*/ */
List<EsGoodsIndex> getEsGoodsBySkuIds(List<String> skuIds); List<EsGoodsIndex> getEsGoodsBySkuIds(List<String> skuIds, PageVO pageVo);
/** /**
* 根据id获取商品索引 * 根据id获取商品索引

View File

@ -117,10 +117,19 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
} }
@Override @Override
public List<EsGoodsIndex> getEsGoodsBySkuIds(List<String> skuIds) { public List<EsGoodsIndex> getEsGoodsBySkuIds(List<String> skuIds, PageVO pageVo) {
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder(); NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
NativeSearchQuery build = searchQueryBuilder.build(); NativeSearchQuery build = searchQueryBuilder.build();
build.setIds(skuIds); build.setIds(skuIds);
if (pageVo != null) {
int pageNumber = pageVo.getPageNumber() - 1;
if (pageNumber < 0) {
pageNumber = 0;
}
Pageable pageable = PageRequest.of(pageNumber, pageVo.getPageSize());
//分页
searchQueryBuilder.withPageable(pageable);
}
return restTemplate.multiGet(build, EsGoodsIndex.class, restTemplate.getIndexCoordinatesFor(EsGoodsIndex.class)); return restTemplate.multiGet(build, EsGoodsIndex.class, restTemplate.getIndexCoordinatesFor(EsGoodsIndex.class));
} }
@ -396,8 +405,8 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
/** /**
* 商品推荐 * 商品推荐
* *
* @param filterBuilder * @param filterBuilder 过滤条件
* @param searchDTO * @param searchDTO 搜索条件
*/ */
private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) { private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) {
@ -557,7 +566,8 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore); FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore);
filterFunctionBuilders.add(skuNoBuilder); filterFunctionBuilders.add(skuNoBuilder);
FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3); // 修改分数算法为无数字最大分数越高
FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.NONE).setWeight(3);
FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(buyCountScore); FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(buyCountScore);
filterFunctionBuilders.add(buyCountBuilder); filterFunctionBuilders.add(buyCountBuilder);
return filterFunctionBuilders; return filterFunctionBuilders;