diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index 5cc77b32..558b7c30 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -394,7 +394,7 @@ public class GoodsMessageListener implements RocketMQListener { } } - if (goodsIndex.getPromotionMap() == null || goodsIndex.getPromotionMap().isEmpty()) { + if (goodsIndex.getOriginPromotionMap() == null || goodsIndex.getOriginPromotionMap().isEmpty()) { Map goodsCurrentPromotionMap = promotionService.getGoodsSkuPromotionMap(goodsIndex.getStoreId(), goodsIndex.getId()); goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(goodsCurrentPromotionMap)); } diff --git a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java index 5cc50ec4..11a20fab 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java @@ -165,14 +165,8 @@ public abstract class BaseElasticsearchService { " \"type\": \"long\"\n" + " },\n" + " \"releaseTime\": {\n" + - " \"type\": \"text\",\n" + - " \"fielddata\": true, \n" + - " \"fields\": {\n" + - " \"keyword\": {\n" + - " \"type\": \"keyword\",\n" + - " \"ignore_above\": 256\n" + - " }\n" + - " }\n" + + " \"type\": \"date\",\n" + + " \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" + " },\n" + " \"categoryPath\": {\n" + " \"type\": \"text\",\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 cd5372de..b4cdf3fd 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 @@ -6,14 +6,12 @@ import cn.lili.elasticsearch.EsSuffix; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.promotion.tools.PromotionTools; -import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.Accessors; import org.springframework.data.annotation.Id; -import org.springframework.data.elasticsearch.annotations.DateFormat; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @@ -99,42 +97,42 @@ public class EsGoodsIndex implements Serializable { /** * 品牌id */ - @Field(type = FieldType.Integer, fielddata = true) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("品牌id") private String brandId; /** * 品牌名称 */ - @Field(type = FieldType.Keyword, fielddata = true) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("品牌名称") private String brandName; /** * 品牌图片地址 */ - @Field(type = FieldType.Keyword, fielddata = true) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("品牌图片地址") private String brandUrl; /** * 分类path */ - @Field(type = FieldType.Keyword) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("分类path") private String categoryPath; /** * 分类名称path */ - @Field(type = FieldType.Keyword) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("分类名称path") private String categoryNamePath; /** * 店铺分类id */ - @Field(type = FieldType.Keyword) + @Field(type = FieldType.Text, fielddata = true) @ApiModelProperty("店铺分类id") private String storeCategoryPath; @@ -251,9 +249,8 @@ public class EsGoodsIndex implements Serializable { private String goodsVideo; @ApiModelProperty("商品发布时间") - @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") - @Field(type = FieldType.Date, format = DateFormat.basic_date_time) - private Date releaseTime; + @Field(type = FieldType.Date) + private Long releaseTime; /** * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum @@ -309,7 +306,7 @@ public class EsGoodsIndex implements Serializable { this.grade = sku.getGrade(); this.recommend = sku.getRecommend(); this.goodsType = sku.getGoodsType(); - this.releaseTime = new Date(); + this.releaseTime = new Date().getTime(); } } @@ -374,10 +371,14 @@ public class EsGoodsIndex implements Serializable { this.authFlag = sku.getAuthFlag(); this.intro = sku.getIntro(); this.grade = sku.getGrade(); - this.releaseTime = new Date(); + this.releaseTime = new Date().getTime(); } } + public Map getOriginPromotionMap() { + return JSONUtil.parseObj(this.promotionMapJson); + } + public Map getPromotionMap() { return PromotionTools.filterInvalidPromotionsMap(JSONUtil.parseObj(this.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 ffe12d04..3adff571 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 @@ -131,14 +131,6 @@ public interface EsGoodsIndexService { */ void updateEsGoodsIndexAllByList(BasePromotions promotion, String key); - /** - * 删除指定商品的促销信息 - * - * @param skuIds skuId列表 - * @param promotionType 促销类型 - */ - void deleteEsGoodsPromotionIndexByList(List skuIds, PromotionTypeEnum promotionType); - /** * 删除索引中指定的促销活动id的促销活动 * 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 d231f0df..8ae29f80 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 @@ -277,7 +277,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements UpdateRequest updateRequest = new UpdateRequest(indexName, goodsIndex.getId()); JSONObject jsonObject = JSONUtil.parseObj(goodsIndex); - jsonObject.set("releaseTime", goodsIndex.getReleaseTime().getTime()); + jsonObject.set("releaseTime", goodsIndex.getReleaseTime()); updateRequest.doc(jsonObject); request.add(updateRequest); } @@ -431,45 +431,26 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements */ @Override public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) { - List goodsIndices = new ArrayList<>(); - //如果storeId不为空,则表示是店铺活动 - if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { - EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); - searchDTO.setStoreId(promotion.getStoreId()); - //查询出店铺商品 - SearchPage esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null); - for (SearchHit searchHit : esGoodsIndices.getContent()) { - goodsIndices.add(searchHit.getContent()); - } - } else { - //否则是平台活动 - Iterable all = goodsIndexRepository.findAll(); - //查询出全部商品 - goodsIndices = new ArrayList<>(IterableUtil.toCollection(all)); - } - List skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); - this.updateEsGoodsIndexPromotions(skuIds, promotion, key); - } - - @Override - public void deleteEsGoodsPromotionIndexByList(List skuIds, PromotionTypeEnum promotionType) { - //批量删除活动索引 - for (String skuId : skuIds) { - EsGoodsIndex goodsIndex = findById(skuId); - //商品索引不为空 - if (goodsIndex != null) { - Map promotionMap = goodsIndex.getPromotionMap(); - if (promotionMap != null && !promotionMap.isEmpty()) { - //如果存在同类型促销活动删除 - List collect = promotionMap.keySet().stream().filter(i -> i.contains(promotionType.name())).collect(Collectors.toList()); - collect.forEach(promotionMap::remove); - goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap)); - updateIndex(goodsIndex); + ThreadUtil.execAsync(() -> { + List goodsIndices = new ArrayList<>(); + //如果storeId不为空,则表示是店铺活动 + if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { + EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); + searchDTO.setStoreId(promotion.getStoreId()); + //查询出店铺商品 + SearchPage esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null); + for (SearchHit searchHit : esGoodsIndices.getContent()) { + goodsIndices.add(searchHit.getContent()); } } else { - log.error("更新索引商品促销信息失败!skuId 为 【{}】的索引不存在!", skuId); + //否则是平台活动 + Iterable all = goodsIndexRepository.findAll(); + //查询出全部商品 + goodsIndices = new ArrayList<>(IterableUtil.toCollection(all)); } - } + List skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); + this.updateEsGoodsIndexPromotions(skuIds, promotion, key); + }); } @Override @@ -503,14 +484,16 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements */ @Override public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) { - BulkRequest bulkRequest = new BulkRequest(); - for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) { - UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey); - if (updateRequest != null) { - bulkRequest.add(updateRequest); + ThreadUtil.execAsync(() -> { + BulkRequest bulkRequest = new BulkRequest(); + for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) { + UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey); + if (updateRequest != null) { + bulkRequest.add(updateRequest); + } } - } - this.executeBulkUpdateRequest(bulkRequest); + this.executeBulkUpdateRequest(bulkRequest); + }); } /** @@ -520,13 +503,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements * @param promotionsKey 促销活动key */ private UpdateRequest removePromotionByPromotionKey(EsGoodsIndex goodsIndex, String promotionsKey) { - Map promotionMap = goodsIndex.getPromotionMap(); + Map promotionMap = goodsIndex.getOriginPromotionMap(); if (promotionMap != null && !promotionMap.isEmpty()) { //如果存在同促销ID的活动删除 - List collect = promotionMap.keySet().stream().filter(i -> i.equals(promotionsKey)).collect(Collectors.toList()); - collect.forEach(promotionMap::remove); - goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap)); - return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), promotionMap); + Map filterPromotionMap = promotionMap.entrySet().stream().filter(i -> !i.getKey().equals(promotionsKey)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), filterPromotionMap); } return null; } @@ -538,7 +519,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements public void cleanInvalidPromotion() { Iterable all = goodsIndexRepository.findAll(); for (EsGoodsIndex goodsIndex : all) { - Map promotionMap = goodsIndex.getPromotionMap(); + Map promotionMap = goodsIndex.getOriginPromotionMap(); //获取商品索引 if (promotionMap != null && !promotionMap.isEmpty()) { //促销不为空则进行清洗 @@ -574,7 +555,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements //如果商品索引不为空,返回促销信息,否则返回空 if (goodsIndex != null) { - Map promotionMap = goodsIndex.getPromotionMap(); + Map promotionMap = goodsIndex.getOriginPromotionMap(); if (promotionMap == null || promotionMap.isEmpty()) { return new HashMap<>(16); } @@ -639,10 +620,10 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements private UpdateRequest updateGoodsIndexPromotion(EsGoodsIndex goodsIndex, String key, BasePromotions promotion) { Map promotionMap; //数据非空处理,如果空给一个新的信息 - if (goodsIndex.getPromotionMap() == null || goodsIndex.getPromotionMap().isEmpty()) { + if (goodsIndex.getOriginPromotionMap() == null || goodsIndex.getOriginPromotionMap().isEmpty()) { promotionMap = new HashMap<>(1); } else { - promotionMap = goodsIndex.getPromotionMap(); + promotionMap = goodsIndex.getOriginPromotionMap(); } log.info("ES修改商品活动索引-活动信息:{}", promotion); @@ -670,7 +651,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index(getIndexName()); updateRequest.id(id); -// updateRequest.retryOnConflict(5); + updateRequest.retryOnConflict(3); // updateRequest.version(promotionMap.size()); Map params = new HashMap<>(); params.put("promotionMap", JSONUtil.toJsonStr(promotionMap));