!150 优化生成索引mapping日志

Merge pull request !150 from OceansDeep/feature/pg
This commit is contained in:
OceansDeep 2022-04-25 09:23:34 +00:00 committed by Gitee
commit 2b21c3cd09
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 27 additions and 9 deletions

View File

@ -376,20 +376,18 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
*/ */
private void generatorGoodsIndex(Goods goods, List<GoodsSku> goodsSkuList) { private void generatorGoodsIndex(Goods goods, List<GoodsSku> goodsSkuList) {
int skuSource = 100; int skuSource = 100;
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
for (GoodsSku goodsSku : goodsSkuList) { for (GoodsSku goodsSku : goodsSkuList) {
EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId());
EsGoodsIndex goodsIndex = this.settingUpGoodsIndexData(goods, goodsSku); EsGoodsIndex goodsIndex = this.settingUpGoodsIndexData(goods, goodsSku);
goodsIndex.setSkuSource(skuSource--); goodsIndex.setSkuSource(skuSource--);
log.info("goodsSku{}", goodsSku); log.info("goodsSku{}", goodsSku);
log.info("esGoodsOld{}", esGoodsOld);
//如果商品库存不为0并且es中有数据 //如果商品库存不为0并且es中有数据
if (goodsSku.getQuantity() > 0 && esGoodsOld == null) { if (goodsSku.getQuantity() > 0) {
log.info("生成商品索引 {}", goodsIndex); log.info("生成商品索引 {}", goodsIndex);
this.goodsIndexService.addIndex(goodsIndex); esGoodsIndices.add(goodsIndex);
} else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) {
goodsIndexService.updateIndex(goodsIndex);
} }
} }
this.goodsIndexService.addIndex(esGoodsIndices);
} }
private EsGoodsIndex settingUpGoodsIndexData(Goods goods, GoodsSku goodsSku) { private EsGoodsIndex settingUpGoodsIndexData(Goods goods, GoodsSku goodsSku) {

View File

@ -29,7 +29,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author paulG * @author paulG
@ -343,20 +342,20 @@ public abstract class BaseElasticsearchService {
PutMappingRequest request = new PutMappingRequest(index) PutMappingRequest request = new PutMappingRequest(index)
.source(source, XContentType.JSON); .source(source, XContentType.JSON);
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AcknowledgedResponse> response = new AtomicReference<>();
client.indices().putMappingAsync( client.indices().putMappingAsync(
request, request,
RequestOptions.DEFAULT, RequestOptions.DEFAULT,
new ActionListener<AcknowledgedResponse>() { new ActionListener<AcknowledgedResponse>() {
@Override @Override
public void onResponse(AcknowledgedResponse r) { public void onResponse(AcknowledgedResponse r) {
response.set(r);
latch.countDown(); latch.countDown();
log.info("创建索引mapping成功{}", r);
} }
@Override @Override
public void onFailure(Exception e) { public void onFailure(Exception e) {
latch.countDown(); latch.countDown();
log.error("创建索引mapping失败", e);
} }
}); });
latch.await(10, TimeUnit.SECONDS); latch.await(10, TimeUnit.SECONDS);

View File

@ -38,6 +38,13 @@ public interface EsGoodsIndexService {
*/ */
void addIndex(EsGoodsIndex goods); void addIndex(EsGoodsIndex goods);
/**
* 添加商品索引
*
* @param goods 商品索引信息
*/
void addIndex(List<EsGoodsIndex> goods);
/** /**
* 更新商品索引 * 更新商品索引
* *

View File

@ -210,6 +210,20 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
} }
} }
/**
* 添加商品索引
*
* @param goods 商品索引信息
*/
@Override
public void addIndex(List<EsGoodsIndex> goods) {
try {
goodsIndexRepository.saveAll(goods);
} catch (Exception e) {
log.error("批量为商品生成索引异常", e);
}
}
@Override @Override
public void updateIndex(EsGoodsIndex goods) { public void updateIndex(EsGoodsIndex goods) {
goodsIndexRepository.save(goods); goodsIndexRepository.save(goods);