feat(商城): 添加elasticsearch测试代码

master
wayn 4 years ago
parent ea91d13455
commit 08a7ed379e

@ -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;
/**
* <p>
@ -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<Goods> list = iGoodsService.list();
List<ElasticEntity> entities = new ArrayList<>();
for (Goods goods : list) {
ElasticEntity elasticEntity = new ElasticEntity();
Map<String, Object> 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<Goods> list = iGoodsService.list();
List<ElasticEntity> entities = new ArrayList<>();
for (Goods goods : list) {
ElasticEntity elasticEntity = new ElasticEntity();
Map<String, Object> 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);
}
}

@ -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;
}

Loading…
Cancel
Save