From 08a7ed379ef0b86492f3fb10cda2e5d5b89e54c9 Mon Sep 17 00:00:00 2001
From: wayn <1669738430@qq.com>
Date: Sat, 24 Oct 2020 11:12:19 +0800
Subject: [PATCH] =?UTF-8?q?feat(=E5=95=86=E5=9F=8E):=20=E6=B7=BB=E5=8A=A0e?=
=?UTF-8?q?lasticsearch=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../api/controller/shop/GoodsController.java | 51 ++++++++++++-------
.../elastic/service/BaseElasticService.java | 28 +++++-----
2 files changed, 49 insertions(+), 30 deletions(-)
diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
index 47ffc19..9231bb6 100644
--- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
+++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
@@ -3,6 +3,7 @@ package com.wayn.admin.api.controller.shop;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.admin.framework.manager.elastic.service.BaseElasticService;
+import com.wayn.admin.framework.redis.RedisCache;
import com.wayn.common.base.BaseController;
import com.wayn.common.base.ElasticEntity;
import com.wayn.common.constant.SysConstants;
@@ -15,10 +16,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
/**
*
@@ -32,11 +35,14 @@ import java.util.Map;
@RequestMapping("/shop/goods")
public class GoodsController extends BaseController {
+ private static final String GOODS_INDEX = "goods";
+ private static final String REDIS_GOODS_INDEX = "es_goods";
@Autowired
private IGoodsService iGoodsService;
-
@Autowired
private BaseElasticService baseElasticService;
+ @Autowired
+ private RedisCache redisCache;
@GetMapping("/list")
public R list(Goods goods) {
@@ -66,23 +72,32 @@ public class GoodsController extends BaseController {
@PostMapping("syncEs")
public R syncEs() {
- baseElasticService.deleteIndex("goods");
- baseElasticService.createIndex("goos", FileUtils.getContent(this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME)));
- List list = iGoodsService.list();
- List entities = new ArrayList<>();
- for (Goods goods : list) {
- ElasticEntity elasticEntity = new ElasticEntity();
- Map map = new HashMap<>();
- elasticEntity.setId(goods.getId().toString());
- map.put("id", goods.getId());
- map.put("name", goods.getName());
- map.put("countPrice", goods.getCounterPrice());
- map.put("retailPrice", goods.getRetailPrice());
- map.put("keyword", goods.getKeywords());
- map.put("isOnSale", goods.getIsOnSale());
- elasticEntity.setData(map);
- entities.add(elasticEntity);
+ if (redisCache.getCacheObject(REDIS_GOODS_INDEX) != null) {
+ return R.error("正在同步,请稍等");
+ }
+ boolean flag = false;
+ redisCache.setCacheObject(REDIS_GOODS_INDEX, true, 3, TimeUnit.MINUTES);
+ baseElasticService.deleteIndex(GOODS_INDEX);
+ InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME);
+ if (baseElasticService.createIndex(GOODS_INDEX, FileUtils.getContent(inputStream))) {
+ List list = iGoodsService.list();
+ List entities = new ArrayList<>();
+ for (Goods goods : list) {
+ ElasticEntity elasticEntity = new ElasticEntity();
+ Map map = new HashMap<>();
+ elasticEntity.setId(goods.getId().toString());
+ map.put("id", goods.getId());
+ map.put("name", goods.getName());
+ map.put("countPrice", goods.getCounterPrice());
+ map.put("retailPrice", goods.getRetailPrice());
+ map.put("keyword", goods.getKeywords());
+ map.put("isOnSale", goods.getIsOnSale());
+ elasticEntity.setData(map);
+ entities.add(elasticEntity);
+ }
+ flag = baseElasticService.insertBatch("goods", entities);
+ redisCache.deleteObject(REDIS_GOODS_INDEX);
}
- return R.result(baseElasticService.insertBatch("goods", entities));
+ return R.result(flag);
}
}
diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/elastic/service/BaseElasticService.java b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/elastic/service/BaseElasticService.java
index c4594a0..13f6964 100644
--- a/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/elastic/service/BaseElasticService.java
+++ b/waynboot-admin-api/src/main/java/com/wayn/admin/framework/manager/elastic/service/BaseElasticService.java
@@ -14,7 +14,7 @@ import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.support.IndicesOptions;
+import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
@@ -48,11 +48,11 @@ public class BaseElasticService {
* @param idxName 缩影名称
* @param idxSQL 缩影定义
*/
- public void createIndex(String idxName, String idxSQL) {
+ public boolean createIndex(String idxName, String idxSQL) {
try {
if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
- return;
+ return false;
}
CreateIndexRequest request = new CreateIndexRequest(idxName);
buildSetting(request);
@@ -60,12 +60,12 @@ public class BaseElasticService {
// request.settings() 手工指定Setting
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
- throw new RuntimeException("初始化失败");
+ return false;
}
} catch (Exception e) {
e.printStackTrace();
- System.exit(0);
}
+ return true;
}
/**
@@ -80,8 +80,8 @@ public class BaseElasticService {
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
- request.indicesOptions(IndicesOptions.lenientExpandOpen());
- return !restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
+// request.indicesOptions(IndicesOptions.lenientExpandOpen());
+ return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
}
/**
@@ -218,16 +218,20 @@ public class BaseElasticService {
*
* @param idxName 索引名称
*/
- public void deleteIndex(String idxName) {
+ public boolean deleteIndex(String idxName) {
try {
- if (this.indexExist(idxName)) {
- log.error(" idxName={} 已经存在", idxName);
- return;
+ if (!this.indexExist(idxName)) {
+ log.error(" idxName={} 不存在,idxSql={}", idxName);
+ return false;
+ }
+ AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
+ if (!acknowledgedResponse.isAcknowledged()) {
+ return false;
}
- restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
+ return true;
}