优化更新全部商品索引时的性能

This commit is contained in:
paulGao 2022-03-30 17:21:57 +08:00
parent c093261369
commit 210d56447c

View File

@ -431,24 +431,26 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) { public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {
List<EsGoodsIndex> goodsIndices = new ArrayList<>(); ThreadUtil.execAsync(() -> {
//如果storeId不为空则表示是店铺活动 List<EsGoodsIndex> goodsIndices = new ArrayList<>();
if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { //如果storeId不为空则表示是店铺活动
EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
searchDTO.setStoreId(promotion.getStoreId()); EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO();
//查询出店铺商品 searchDTO.setStoreId(promotion.getStoreId());
SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null); //查询出店铺商品
for (SearchHit<EsGoodsIndex> searchHit : esGoodsIndices.getContent()) { SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null);
goodsIndices.add(searchHit.getContent()); for (SearchHit<EsGoodsIndex> searchHit : esGoodsIndices.getContent()) {
goodsIndices.add(searchHit.getContent());
}
} else {
//否则是平台活动
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
//查询出全部商品
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
} }
} else { List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
//否则是平台活动 this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll(); });
//查询出全部商品
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
}
List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
} }
@Override @Override
@ -482,14 +484,16 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) { public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) {
BulkRequest bulkRequest = new BulkRequest(); ThreadUtil.execAsync(() -> {
for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) { BulkRequest bulkRequest = new BulkRequest();
UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey); for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) {
if (updateRequest != null) { UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey);
bulkRequest.add(updateRequest); if (updateRequest != null) {
bulkRequest.add(updateRequest);
}
} }
} this.executeBulkUpdateRequest(bulkRequest);
this.executeBulkUpdateRequest(bulkRequest); });
} }
/** /**