fix: 优化es商品搜索返回结果。减少重复返回结果

This commit is contained in:
misworga831 2023-03-31 16:49:42 +08:00
parent ab2bb11eee
commit b07fc790e5
3 changed files with 30 additions and 9 deletions

View File

@ -18,14 +18,13 @@ import cn.lili.modules.search.service.HotWordsService;
import cn.lili.modules.statistics.aop.PageViewPoint;
import cn.lili.modules.statistics.aop.enums.PageViewEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@ -104,10 +103,9 @@ public class GoodsBuyerController {
@ApiOperation(value = "从ES中获取商品信息")
@GetMapping("/es")
public ResultMessage<SearchPage<EsGoodsIndex>> getGoodsByPageFromEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
public ResultMessage<Page<EsGoodsIndex>> getGoodsByPageFromEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
pageVO.setNotConvert(true);
SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(goodsSearchParams, pageVO);
return ResultUtil.data(esGoodsIndices);
return ResultUtil.data(goodsSearchService.searchGoodsByPage(goodsSearchParams, pageVO));
}
@ApiOperation(value = "从ES中获取相关商品品牌名称分类名称及属性")

View File

@ -4,6 +4,7 @@ import cn.lili.common.vo.PageVO;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.elasticsearch.core.SearchPage;
import java.util.List;
@ -25,6 +26,15 @@ public interface EsGoodsSearchService {
*/
SearchPage<EsGoodsIndex> searchGoods(EsGoodsSearchDTO searchDTO, PageVO pageVo);
/**
* 商品搜索
*
* @param searchDTO 搜索参数
* @param pageVo 分页参数
* @return 搜索结果
*/
Page<EsGoodsIndex> searchGoodsByPage(EsGoodsSearchDTO searchDTO, PageVO pageVo);
/**
* 获取筛选器
*

View File

@ -17,6 +17,7 @@ import cn.lili.modules.search.entity.dto.SelectorOptions;
import cn.lili.modules.search.service.EsGoodsSearchService;
import cn.lili.modules.search.utils.SqlFilter;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
@ -37,15 +38,13 @@ import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHitSupport;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.*;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* ES商品搜索业务层实现
@ -95,6 +94,20 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
return SearchHitSupport.searchPageFor(search, searchQuery.getPageable());
}
@Override
public Page<EsGoodsIndex> searchGoodsByPage(EsGoodsSearchDTO searchDTO, PageVO pageVo) {
SearchPage<EsGoodsIndex> esGoodsIndices = this.searchGoods(searchDTO, pageVo);
Page<EsGoodsIndex> resultPage = new Page<>();
if (esGoodsIndices != null && !esGoodsIndices.getContent().isEmpty()) {
List<EsGoodsIndex> collect = esGoodsIndices.getSearchHits().getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
resultPage.setRecords(collect);
resultPage.setPages(esGoodsIndices.getTotalPages());
resultPage.setCurrent(esGoodsIndices.getNumber() + 1L);
resultPage.setSize(esGoodsIndices.getSize());
resultPage.setTotal(esGoodsIndices.getTotalElements());
}
return resultPage;
}
@Override
public EsGoodsRelatedInfo getSelector(EsGoodsSearchDTO goodsSearch, PageVO pageVo) {