From d14763d52c25fb0c17c2aba0267668afd5c339b3 Mon Sep 17 00:00:00 2001 From: paulGao Date: Thu, 9 Jun 2022 15:32:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0buyer=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E6=A3=80=E6=B5=8Bes=E7=B4=A2=E5=BC=95=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E4=B8=8D=E5=AD=98=E5=9C=A8=E5=88=99=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lili/init/EsGoodsIndexInitRunner.java | 30 +++++++++++++++++++ .../lili/cache/config/redis/RedisConfig.java | 2 +- .../BaseElasticsearchService.java | 2 +- .../search/entity/dos/EsGoodsIndex.java | 2 +- .../search/service/EsGoodsIndexService.java | 8 ++++- .../serviceimpl/EsGoodsIndexServiceImpl.java | 19 ++++++++++++ .../serviceimpl/EsGoodsSearchServiceImpl.java | 23 +++++--------- 7 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 buyer-api/src/main/java/cn/lili/init/EsGoodsIndexInitRunner.java diff --git a/buyer-api/src/main/java/cn/lili/init/EsGoodsIndexInitRunner.java b/buyer-api/src/main/java/cn/lili/init/EsGoodsIndexInitRunner.java new file mode 100644 index 00000000..8b3e9952 --- /dev/null +++ b/buyer-api/src/main/java/cn/lili/init/EsGoodsIndexInitRunner.java @@ -0,0 +1,30 @@ +package cn.lili.init; + +import cn.lili.modules.search.service.EsGoodsIndexService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +/** + * @author paulG + * @since 2022/6/9 + **/ +@Component +@Slf4j +public class EsGoodsIndexInitRunner implements ApplicationRunner { + + @Autowired + private EsGoodsIndexService esGoodsIndexService; + + + @Override + public void run(ApplicationArguments args) { + try { + esGoodsIndexService.initIndex(); + } catch (Exception e) { + log.error("检测ES商品索引失败", e); + } + } +} diff --git a/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java b/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java index 659c1ece..0551f26b 100644 --- a/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java +++ b/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java @@ -115,7 +115,6 @@ public class RedisConfig extends CachingConfigurerSupport { @Bean(destroyMethod = "shutdown") public RedissonClient redisson() { Config config = new Config(); - if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) { // 哨兵模式 SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); @@ -145,6 +144,7 @@ public class RedisConfig extends CachingConfigurerSupport { if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) { singleServerConfig.setPassword(redisProperties.getPassword()); } + singleServerConfig.setPingConnectionInterval(1000); } return Redisson.create(config); diff --git a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java index 8421156a..fec64277 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java @@ -123,7 +123,7 @@ public abstract class BaseElasticsearchService { " \"type\": \"keyword\"\n" + " },\n" + " \"type\": {\n" + - " \"type\": \"long\"\n" + + " \"type\": \"integer\"\n" + " },\n" + " \"value\": {\n" + " \"type\": \"keyword\"\n" + diff --git a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java index 71ba03c0..9687f832 100644 --- a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java +++ b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java @@ -277,7 +277,7 @@ public class EsGoodsIndex implements Serializable { * @see PromotionTypeEnum * value 为 促销活动实体信息 */ - @Field(type = FieldType.Nested) + @Field(type = FieldType.Text) @ApiModelProperty("商品促销活动集合JSON,key 为 促销活动类型,value 为 促销活动实体信息 ") private String promotionMapJson; diff --git a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java index aad7893e..ffb8d56a 100644 --- a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java +++ b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java @@ -20,7 +20,7 @@ import java.util.Map; public interface EsGoodsIndexService { /** - * 全局索引初始化 + * 全局索引数据初始化 */ void init(); @@ -31,6 +31,12 @@ public interface EsGoodsIndexService { */ Map getProgress(); + + /** + * 全局索引初始化 + */ + void initIndex(); + /** * 添加商品索引 * diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 6c4626d2..063de202 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -200,6 +200,25 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements return map; } + @Override + public void initIndex() { + //索引名称拼接 + String indexName = this.getIndexName(); + + //索引初始化,因为mapping结构问题: + //但是如果索引已经自动生成过,这里就不会创建索引,设置mapping,所以这里决定在初始化索引的同时,将已有索引删除,重新创建 + + boolean indexExist = this.indexExist(indexName); + log.info("检测 {} 索引结构是否存在:{}", indexName, indexExist); + if (!indexExist) { + + log.info("初始化索引结构 {}", indexName); + //如果索引不存在,则创建索引 + createIndexRequest(indexName); + } + + } + @Override public void addIndex(EsGoodsIndex goods) { try { diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java index e91bd477..3b9d18cc 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java @@ -11,10 +11,8 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum; 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 cn.lili.modules.search.entity.dto.HotWordsDTO; import cn.lili.modules.search.entity.dto.ParamOptions; import cn.lili.modules.search.entity.dto.SelectorOptions; -import cn.lili.modules.search.service.EsGoodsIndexService; import cn.lili.modules.search.service.EsGoodsSearchService; import com.alibaba.druid.util.StringUtils; import lombok.extern.slf4j.Slf4j; @@ -44,7 +42,6 @@ import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Service; import java.util.*; @@ -73,9 +70,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { */ @Autowired private ElasticsearchOperations restTemplate; - - @Autowired - private EsGoodsIndexService esGoodsIndexService; /** * 缓存 */ @@ -84,10 +78,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { @Override public SearchPage searchGoods(EsGoodsSearchDTO searchDTO, PageVO pageVo) { - boolean exists = restTemplate.indexOps(EsGoodsIndex.class).exists(); - if (!exists) { - esGoodsIndexService.init(); - } if (CharSequenceUtil.isNotBlank(searchDTO.getKeyword())) { cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), searchDTO.getKeyword()); } @@ -364,7 +354,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { this.commonSearch(filterBuilder, searchDTO); //智能推荐 - this.recommended(filterBuilder,searchDTO); + this.recommended(filterBuilder, searchDTO); //未上架的商品不显示 filterBuilder.must(QueryBuilders.matchQuery("marketEnable", GoodsStatusEnum.UPPER.name())); @@ -406,30 +396,31 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { /** * 商品推荐 + * * @param filterBuilder * @param searchDTO */ private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) { String currentGoodsId = searchDTO.getCurrentGoodsId(); - if(CharSequenceUtil.isEmpty(currentGoodsId)) { + if (CharSequenceUtil.isEmpty(currentGoodsId)) { return; } //排除当前商品 - filterBuilder.mustNot(QueryBuilders.matchQuery("id",currentGoodsId)); + filterBuilder.mustNot(QueryBuilders.matchQuery("id", currentGoodsId)); //查询当前浏览商品的索引信息 EsGoodsIndex esGoodsIndex = restTemplate.get(currentGoodsId, EsGoodsIndex.class); - if(esGoodsIndex==null) { + if (esGoodsIndex == null) { return; } //推荐与当前浏览商品相同一个二级分类下的商品 String categoryPath = esGoodsIndex.getCategoryPath(); - if(CharSequenceUtil.isNotEmpty(categoryPath)){ + if (CharSequenceUtil.isNotEmpty(categoryPath)) { //匹配二级分类 String substring = categoryPath.substring(0, categoryPath.lastIndexOf(",")); - filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath",substring+"*")); + filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath", substring + "*")); } } From 4316fa8953c3f05a890c08992aa5e916a8d491e1 Mon Sep 17 00:00:00 2001 From: paulGao Date: Thu, 9 Jun 2022 16:43:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=95=86=E5=93=81=E7=B4=A2=E5=BC=95=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=BC=96=E8=BE=91=E5=95=86=E5=93=81=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9F=E5=95=86=E5=93=81=E7=9A=84=E9=94=80?= =?UTF-8?q?=E9=87=8F=E3=80=81=E5=A5=BD=E8=AF=84=E7=8E=87=E5=92=8C=E8=AF=84?= =?UTF-8?q?=E4=BB=B7=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/GoodsSkuServiceImpl.java | 32 ++++++++++++++++--- .../search/entity/dos/EsGoodsIndex.java | 5 +-- .../serviceimpl/EsGoodsIndexServiceImpl.java | 3 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java index aa6bc4c4..c4072466 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java @@ -14,6 +14,7 @@ import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.security.context.UserContext; import cn.lili.common.utils.SnowFlake; import cn.lili.modules.goods.entity.dos.Goods; +import cn.lili.modules.goods.entity.dos.GoodsGallery; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dto.GoodsOperationDTO; import cn.lili.modules.goods.entity.dto.GoodsSearchParams; @@ -166,8 +167,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i } else { skuList = new ArrayList<>(); for (Map map : goodsOperationDTO.getSkuList()) { - GoodsSku sku = null; - sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO); + GoodsSku sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO); renderGoodsSku(sku, goodsOperationDTO); skuList.add(sku); //如果商品状态值不对,则es索引移除 @@ -636,6 +636,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i // 商品销售模式渲染器 salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderBatch(goodsSkuList, goodsOperationDTO)); for (GoodsSku goodsSku : goodsSkuList) { + extendOldSkuValue(goodsSku); this.renderImages(goodsSku); } } @@ -647,22 +648,43 @@ public class GoodsSkuServiceImpl extends ServiceImpl i * @param goodsOperationDTO 商品操作DTO */ void renderGoodsSku(GoodsSku goodsSku, GoodsOperationDTO goodsOperationDTO) { + extendOldSkuValue(goodsSku); // 商品销售模式渲染器 salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderSingle(goodsSku, goodsOperationDTO)); this.renderImages(goodsSku); } + /** + * 将原sku的一些不会直接传递的值放到新的sku中 + * + * @param goodsSku 商品sku + */ + private void extendOldSkuValue(GoodsSku goodsSku) { + if (CharSequenceUtil.isNotEmpty(goodsSku.getGoodsId())) { + GoodsSku oldSku = this.getGoodsSkuByIdFromCache(goodsSku.getId()); + if (oldSku != null) { + goodsSku.setCommentNum(oldSku.getCommentNum()); + goodsSku.setViewCount(oldSku.getViewCount()); + goodsSku.setBuyCount(oldSku.getBuyCount()); + goodsSku.setGrade(oldSku.getGrade()); + } + } + } + /** * 渲染sku图片 * - * @param goodsSku + * @param goodsSku sku */ void renderImages(GoodsSku goodsSku) { JSONObject jsonObject = JSONUtil.parseObj(goodsSku.getSpecs()); List> images = jsonObject.get("images", List.class); if (images != null && !images.isEmpty()) { - goodsSku.setThumbnail(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getThumbnail()); - goodsSku.setSmall(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getSmall()); + GoodsGallery goodsGallery = goodsGalleryService.getGoodsGallery(images.get(0).get("url")); + goodsSku.setBig(goodsGallery.getOriginal()); + goodsSku.setOriginal(goodsGallery.getOriginal()); + goodsSku.setThumbnail(goodsGallery.getThumbnail()); + goodsSku.setSmall(goodsGallery.getSmall()); } } diff --git a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java index 9687f832..743b5dd5 100644 --- a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java +++ b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java @@ -28,7 +28,7 @@ import java.util.Map; * @author paulG **/ @Data -@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME) +@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME, createIndex = false) @ToString @NoArgsConstructor @Accessors(chain = true) @@ -215,6 +215,7 @@ public class EsGoodsIndex implements Serializable { /** * 销售模式 + * * @see cn.lili.modules.goods.entity.enums.GoodsSalesModeEnum */ @Field(type = FieldType.Text) @@ -316,7 +317,7 @@ public class EsGoodsIndex implements Serializable { /** * 参数索引增加 * - * @param sku 商品sku信息 + * @param sku 商品sku信息 * @param goodsParamDTOS 商品参数信息 */ public EsGoodsIndex(GoodsSku sku, List goodsParamDTOS) { diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 063de202..2c999f64 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -66,6 +66,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; import java.lang.reflect.Field; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -127,7 +128,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix()); //为空则默认写入没有任务 if (flag == null) { - cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false); + cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false, 10L, TimeUnit.MINUTES); } //有正在初始化的任务,则提示异常 if (Boolean.TRUE.equals(flag)) {