From aa185c8bd2884f72cc60d5a5a40b08df8be84762 Mon Sep 17 00:00:00 2001 From: paulGao Date: Mon, 25 Apr 2022 10:26:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96consumer=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=95=86=E5=93=81=E7=B4=A2=E5=BC=95=E3=80=82=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=89=B9=E9=87=8F=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lili/listener/GoodsMessageListener.java | 10 ++++------ .../search/service/EsGoodsIndexService.java | 7 +++++++ .../serviceimpl/EsGoodsIndexServiceImpl.java | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index 45c1ea98..19a1bab4 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -376,20 +376,18 @@ public class GoodsMessageListener implements RocketMQListener { */ private void generatorGoodsIndex(Goods goods, List goodsSkuList) { int skuSource = 100; + List esGoodsIndices = new ArrayList<>(); for (GoodsSku goodsSku : goodsSkuList) { - EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); EsGoodsIndex goodsIndex = this.settingUpGoodsIndexData(goods, goodsSku); goodsIndex.setSkuSource(skuSource--); log.info("goodsSku:{}", goodsSku); - log.info("esGoodsOld:{}", esGoodsOld); //如果商品库存不为0,并且es中有数据 - if (goodsSku.getQuantity() > 0 && esGoodsOld == null) { + if (goodsSku.getQuantity() > 0) { log.info("生成商品索引 {}", goodsIndex); - this.goodsIndexService.addIndex(goodsIndex); - } else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) { - goodsIndexService.updateIndex(goodsIndex); + esGoodsIndices.add(goodsIndex); } } + this.goodsIndexService.addIndex(esGoodsIndices); } private EsGoodsIndex settingUpGoodsIndexData(Goods goods, GoodsSku goodsSku) { 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 e5a93839..aad7893e 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 @@ -38,6 +38,13 @@ public interface EsGoodsIndexService { */ void addIndex(EsGoodsIndex goods); + /** + * 添加商品索引 + * + * @param goods 商品索引信息 + */ + void addIndex(List goods); + /** * 更新商品索引 * 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 c78a96af..0a02ea2a 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 @@ -210,6 +210,20 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements } } + /** + * 添加商品索引 + * + * @param goods 商品索引信息 + */ + @Override + public void addIndex(List goods) { + try { + goodsIndexRepository.saveAll(goods); + } catch (Exception e) { + log.error("批量为商品生成索引异常", e); + } + } + @Override public void updateIndex(EsGoodsIndex goods) { goodsIndexRepository.save(goods); From b1bdd9b8a5df0b51343a5823defcc8b25571da67 Mon Sep 17 00:00:00 2001 From: paulGao Date: Mon, 25 Apr 2022 17:22:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=B4=A2=E5=BC=95mapping=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/lili/elasticsearch/BaseElasticsearchService.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java index 11a20fab..8421156a 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java @@ -29,7 +29,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; /** * @author paulG @@ -343,20 +342,20 @@ public abstract class BaseElasticsearchService { PutMappingRequest request = new PutMappingRequest(index) .source(source, XContentType.JSON); CountDownLatch latch = new CountDownLatch(1); - AtomicReference response = new AtomicReference<>(); client.indices().putMappingAsync( request, RequestOptions.DEFAULT, new ActionListener() { @Override public void onResponse(AcknowledgedResponse r) { - response.set(r); latch.countDown(); + log.info("创建索引mapping成功:{}", r); } @Override public void onFailure(Exception e) { latch.countDown(); + log.error("创建索引mapping失败", e); } }); latch.await(10, TimeUnit.SECONDS);