!96 优化更新索引促销方式,以更新部分字段的方式更新。不在以刷新索引的方式更新。

Merge pull request !96 from OceansDeep/feature/pg
This commit is contained in:
OceansDeep 2021-12-29 09:55:05 +00:00 committed by Gitee
commit 68d03196af
4 changed files with 34 additions and 4 deletions

View File

@ -259,6 +259,7 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
searchParams.setCategoryPath(promotions.getScopeId());
List<GoodsSku> goodsSkuByList = this.goodsSkuService.getGoodsSkuByList(searchParams);
List<String> skuIds = goodsSkuByList.stream().map(GoodsSku::getId).collect(Collectors.toList());
this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(skuIds, promotions.getId());
this.goodsIndexService.updateEsGoodsIndexPromotions(skuIds, promotions, esPromotionKey);
} else if (PromotionsScopeTypeEnum.ALL.name().equals(promotions.getScopeType())) {
this.goodsIndexService.updateEsGoodsIndexAllByList(promotions, esPromotionKey);

View File

@ -7,6 +7,9 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
@ -35,10 +38,12 @@ public class BasePromotions extends BaseEntity {
@ApiModelProperty(value = "活动开始时间", required = true)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis")
private Date startTime;
@ApiModelProperty(value = "活动结束时间", required = true)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis")
private Date endTime;
/**

View File

@ -425,6 +425,8 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
// 查询出全部商品
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
}
List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
this.deleteEsGoodsPromotionByPromotionId(skuIds, promotion.getId());
//更新商品索引
for (EsGoodsIndex goodsIndex : goodsIndices) {
this.updateGoodsIndexPromotion(goodsIndex, key, promotion);
@ -481,11 +483,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
private void removePromotionByPromotionId(EsGoodsIndex goodsIndex, String promotionId) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
if (promotionMap != null && !promotionMap.isEmpty()) {
//如果存在同类型促销活动删除
//如果存在同促销ID的活动删除
List<String> collect = promotionMap.keySet().stream().filter(i -> i.split("-")[1].equals(promotionId)).collect(Collectors.toList());
collect.forEach(promotionMap::remove);
goodsIndex.setPromotionMap(promotionMap);
updateIndex(goodsIndex);
this.updatePromotionsByScript(goodsIndex.getId(), promotionMap);
}
}
@ -611,8 +613,25 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
} else {
promotionMap.put(key, promotion);
}
goodsIndex.setPromotionMap(promotionMap);
updateIndex(goodsIndex);
this.updatePromotionsByScript(goodsIndex.getId(), promotionMap);
}
/**
* 以更新部分字段的方式更新索引促销信息
*
* @param id 索引id
* @param promotionMap 促销信息
*/
private void updatePromotionsByScript(String id, Map<String, Object> promotionMap) {
JSONObject jsonObject = JSONUtil.parseObj(promotionMap);
jsonObject.setDateFormat("yyyy-MM-dd HH:mm:ss");
String s = jsonObject.toString();
String promotionsStr = s.replace("{", "[").replace("}", "]");
UpdateByQueryRequest update = new UpdateByQueryRequest(getIndexName());
update.setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery("id", id)));
update.setScript(new Script("ctx._source." + "promotionMap" + "=" + promotionsStr + ";"));
client.updateByQueryAsync(update, RequestOptions.DEFAULT, this.actionListener());
}
/**

View File

@ -13,6 +13,9 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
@ -50,6 +53,7 @@ public abstract class BaseEntity implements Serializable {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间", hidden = true)
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis")
private Date createTime;
@LastModifiedBy
@ -62,6 +66,7 @@ public abstract class BaseEntity implements Serializable {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.UPDATE)
@ApiModelProperty(value = "更新时间", hidden = true)
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis")
private Date updateTime;
@TableField(fill = FieldFill.INSERT)