| | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.text.CharSequenceUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.lili.base.Result; |
| | | import cn.lili.cache.Cache; |
| | | import cn.lili.cache.CachePrefix; |
| | | import cn.lili.common.exception.ServiceException; |
| | | import cn.lili.common.security.context.UserContext; |
| | | import cn.lili.common.vo.PageVO; |
| | | import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; |
| | | import cn.lili.modules.goods.entity.enums.GoodsStatusEnum; |
| | | import cn.lili.modules.lmk.domain.query.VideoGoodsEsQuery; |
| | | 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 org.elasticsearch.index.query.functionscore.FieldValueFactorFunctionBuilder; |
| | | import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; |
| | | import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; |
| | | import org.elasticsearch.index.search.MultiMatchQuery; |
| | | import org.elasticsearch.search.aggregations.*; |
| | | import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested; |
| | | import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.domain.PageRequest; |
| | | import org.springframework.data.domain.Pageable; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.data.elasticsearch.core.*; |
| | | import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; |
| | | import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; |
| | |
| | | return this.restTemplate.get(id, EsGoodsIndex.class); |
| | | } |
| | | |
| | | @Override |
| | | public Result videoGoodsEsPage(VideoGoodsEsQuery q) { |
| | | // 判断商品索引是否存在 |
| | | if (!restTemplate.indexOps(EsGoodsIndex.class).exists()) { |
| | | return Result.ok(); |
| | | } |
| | | // 根据销量倒序排列 |
| | | Pageable pageable = PageRequest.of(q.getPageNumber(), q.getPageSize(), Sort.by("buyCount").descending()); |
| | | |
| | | NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); |
| | | queryBuilder.withPageable(pageable); |
| | | |
| | | BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); |
| | | |
| | | // 关键词匹配商品名称、商品编号 |
| | | if (!StringUtils.isEmpty(q.getKeyword())) { |
| | | boolQuery.must(QueryBuilders.multiMatchQuery(q.getKeyword(), "goodsName", "sn")); |
| | | } |
| | | |
| | | if (q.getSearchFromSelfStore()) { |
| | | // 如果只查自家店铺商品 |
| | | boolQuery.must(QueryBuilders.termQuery("storeId", UserContext.getCurrentUser().getStoreId())); |
| | | } |
| | | |
| | | queryBuilder.withQuery(boolQuery); |
| | | |
| | | NativeSearchQuery query = queryBuilder.build(); |
| | | SearchHits<EsGoodsIndex> searchHits = restTemplate.search(query, EsGoodsIndex.class); |
| | | List<EsGoodsIndex> data = searchHits.stream().map(hit -> hit.getContent()).collect(Collectors.toList()); |
| | | return Result.ok().data(data).total(searchHits.getTotalHits()); |
| | | } |
| | | |
| | | /** |
| | | * 转换搜索结果为聚合商品展示信息 |
| | | * |