feat(商城): 添加elasticsearch

master
hequan_waynaqua 4 years ago
parent 3fb99a9e04
commit f8a8e2fc68

@ -70,14 +70,14 @@ public class GoodsController extends BaseController {
@PostMapping("syncEs") @PostMapping("syncEs")
public R syncEs() { public R syncEs() {
if (redisCache.getCacheObject(SysConstants.REDIS_GOODS_INDEX) != null) { if (redisCache.getCacheObject(SysConstants.REDIS_ES_GOODS_INDEX) != null) {
return R.error("正在同步,请稍等"); return R.error("正在同步,请稍等");
} }
boolean flag = false; boolean flag = false;
redisCache.setCacheObject(SysConstants.REDIS_GOODS_INDEX, true, 3, TimeUnit.MINUTES); redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 3, TimeUnit.MINUTES);
baseElasticService.deleteIndex(SysConstants.GOODS_INDEX); baseElasticService.deleteIndex(SysConstants.ES_GOODS_INDEX);
InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME); InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME);
if (baseElasticService.createIndex(SysConstants.GOODS_INDEX, FileUtils.getContent(inputStream))) { if (baseElasticService.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) {
List<Goods> list = iGoodsService.list(); List<Goods> list = iGoodsService.list();
List<ElasticEntity> entities = new ArrayList<>(); List<ElasticEntity> entities = new ArrayList<>();
for (Goods goods : list) { for (Goods goods : list) {
@ -94,7 +94,7 @@ public class GoodsController extends BaseController {
entities.add(elasticEntity); entities.add(elasticEntity);
} }
flag = baseElasticService.insertBatch("goods", entities); flag = baseElasticService.insertBatch("goods", entities);
redisCache.deleteObject(SysConstants.REDIS_GOODS_INDEX); redisCache.deleteObject(SysConstants.REDIS_ES_GOODS_INDEX);
} }
return R.result(flag); return R.result(flag);
} }

@ -78,7 +78,7 @@ public class SysConstants {
*/ */
public static String ES_INDEX_GOODS_FILENAME = "/es/index/goods"; public static String ES_INDEX_GOODS_FILENAME = "/es/index/goods";
public static final String GOODS_INDEX = "goods"; public static final String ES_GOODS_INDEX = "goods";
public static final String REDIS_GOODS_INDEX = "es_goods"; public static final String REDIS_ES_GOODS_INDEX = "es_goods";
} }

@ -142,7 +142,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
map.put("keyword", goods.getKeywords().split(",")); map.put("keyword", goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale()); map.put("isOnSale", goods.getIsOnSale());
elasticEntity.setData(map); elasticEntity.setData(map);
boolean one = baseElasticService.insertOrUpdateOne(SysConstants.GOODS_INDEX, elasticEntity); boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity);
if (!one) { if (!one) {
throw new BusinessException("创建商品同步es失败"); throw new BusinessException("创建商品同步es失败");
} }
@ -167,7 +167,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
iGoodsAttributeService.remove(new QueryWrapper<GoodsAttribute>().eq("goods_id", goodsId)); iGoodsAttributeService.remove(new QueryWrapper<GoodsAttribute>().eq("goods_id", goodsId));
iGoodsProductService.remove(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId)); iGoodsProductService.remove(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId));
// 同步es // 同步es
boolean one = baseElasticService.delete(SysConstants.GOODS_INDEX, goodsId.toString()); boolean one = baseElasticService.delete(SysConstants.ES_GOODS_INDEX, goodsId.toString());
if (!one) { if (!one) {
throw new BusinessException("删除商品同步es失败"); throw new BusinessException("删除商品同步es失败");
} }
@ -238,7 +238,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
map.put("keyword", goods.getKeywords().split(",")); map.put("keyword", goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale()); map.put("isOnSale", goods.getIsOnSale());
elasticEntity.setData(map); elasticEntity.setData(map);
boolean one = baseElasticService.insertOrUpdateOne(SysConstants.GOODS_INDEX, elasticEntity); boolean one = baseElasticService.insertOrUpdateOne(SysConstants.ES_GOODS_INDEX, elasticEntity);
if (!one) { if (!one) {
throw new BusinessException("创建商品同步es失败"); throw new BusinessException("创建商品同步es失败");
} }

Loading…
Cancel
Save