refactor(商城): es代码优化

master
waynaqua 3 years ago
parent 724a114178
commit 4d418fa1a1

@ -30,7 +30,7 @@ import java.util.stream.Collectors;
@RestController
@RequestMapping("elastic")
public class ElasticController {
public class ElasticTestController {
@Autowired
private ElasticDocument elasticDocument;

@ -76,32 +76,32 @@ public class GoodsController extends BaseController {
return R.error("正在同步,请稍等");
}
boolean flag = false;
redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 3, TimeUnit.MINUTES);
try {
redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 30, TimeUnit.MINUTES);
try (InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME)) {
elasticDocument.deleteIndex(SysConstants.ES_GOODS_INDEX);
InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME);
if (elasticDocument.createIndex(SysConstants.ES_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("sales", goods.getActualSales() + goods.getVirtualSales());
map.put("isHot", goods.getIsHot());
map.put("isNew", goods.getIsNew());
map.put("countPrice", goods.getCounterPrice());
map.put("retailPrice", goods.getRetailPrice());
map.put("keyword", goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale());
map.put("createTime", goods.getCreateTime());
elasticEntity.setData(map);
entities.add(elasticEntity);
}
flag = elasticDocument.insertBatch("goods", entities);
if (!elasticDocument.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) {
return R.error("创建索引失败!");
}
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("sales", goods.getActualSales() + goods.getVirtualSales());
map.put("isHot", goods.getIsHot());
map.put("isNew", goods.getIsNew());
map.put("countPrice", goods.getCounterPrice());
map.put("retailPrice", goods.getRetailPrice());
map.put("keyword", goods.getKeywords().split(","));
map.put("isOnSale", goods.getIsOnSale());
map.put("createTime", goods.getCreateTime());
elasticEntity.setData(map);
entities.add(elasticEntity);
}
flag = elasticDocument.insertBatch("goods", entities);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {

@ -1,5 +1,6 @@
package com.wayn.data.elastic.config;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
public class ElasticConfig {
@Value("${es.host}")
@ -17,6 +19,11 @@ public class ElasticConfig {
public int port;
@Value("${es.scheme}")
public String scheme;
@Value("${es.shards}")
public int shards;
@Value("${es.replicas}")
public int replicas;
@Bean
public RestClientBuilder restClientBuilder() {

@ -0,0 +1,32 @@
package com.wayn.data.elastic.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* elastic
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class ElasticException extends RuntimeException {
private static final long serialVersionUID = 9005063206372011860L;
private Integer code;
private String msg;
public ElasticException(String msg, Integer code) {
this.code = code;
this.msg = msg;
}
public ElasticException(String message, Throwable cause) {
super(message, cause);
}
public ElasticException(String message) {
super(message);
}
}

@ -1,7 +1,10 @@
package com.wayn.data.elastic.manager;
import com.alibaba.fastjson.JSON;
import com.wayn.data.elastic.config.ElasticConfig;
import com.wayn.data.elastic.exception.ElasticException;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
@ -30,6 +33,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -39,32 +43,27 @@ import java.util.List;
public class ElasticDocument {
@Autowired
RestHighLevelClient restHighLevelClient;
private ElasticConfig elasticConfig;
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
*
*
* @param idxName
* @param idxSQL
* @param idxSQL
*/
public boolean createIndex(String idxName, String idxSQL) {
try {
if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
return false;
}
CreateIndexRequest request = new CreateIndexRequest(idxName);
buildSetting(request);
request.mapping(idxSQL, XContentType.JSON);
// request.settings() 手工指定Setting
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
return false;
}
} catch (Exception e) {
log.error(e.getMessage(), e);;
public boolean createIndex(String idxName, String idxSQL) throws IOException {
if (indexExist(idxName)) {
throw new ElasticException(String.format("idxName=%s 已经存在,idxSql=%s", idxName, idxSQL));
}
return true;
CreateIndexRequest request = new CreateIndexRequest(idxName);
buildSetting(request);
request.mapping(idxSQL, XContentType.JSON);
// request.settings() 手工指定Setting
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
return res.isAcknowledged();
}
/**
@ -72,9 +71,9 @@ public class ElasticDocument {
*
* @param idxName
* @return boolean
* @throws Exception
* @throws IOException
*/
public boolean indexExist(String idxName) throws Exception {
public boolean indexExist(String idxName) throws IOException {
GetIndexRequest request = new GetIndexRequest(idxName);
request.local(false);
request.humanReadable(true);
@ -84,23 +83,13 @@ public class ElasticDocument {
}
/**
* index
*
* @param idxName index
* @return boolean
*/
public boolean isExistsIndex(String idxName) throws Exception {
return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT);
}
/**
*
*
*
* @param request
* @param request
*/
public void buildSetting(CreateIndexRequest request) {
request.settings(Settings.builder().put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2));
request.settings(Settings.builder().put("index.number_of_shards", elasticConfig.getShards())
.put("index.number_of_replicas", elasticConfig.getReplicas()));
}
/**
@ -112,7 +101,6 @@ public class ElasticDocument {
log.info("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData()));
request.id(entity.getId());
request.source(entity.getData(), XContentType.JSON);
// request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
try {
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();

Loading…
Cancel
Save