feat(后台管理): 商品管理

添加重新同步商品索引接口
master
hequan_waynaqua 4 years ago
parent e88fe8e0ff
commit ea91d13455

@ -7,6 +7,7 @@ import com.wayn.common.base.ElasticEntity;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.common.util.file.FileUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
@ -17,6 +18,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -35,32 +38,12 @@ public class ElasticController {
@GetMapping("index")
public R index() {
String indexSql = "{\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"countPrice\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"retailPrice\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"isOnSale\": {\n" +
" \"type\": \"boolean\"\n" +
" }\n" +
" }\n" +
"}";
baseElasticService.createIndex("goods", indexSql);
public R index() throws IOException {
InputStream is = this.getClass().getResourceAsStream("/es/index/goods");
System.out.println(FileUtils.getContent(is));
// baseElasticService.createIndex("goods", indexSql);
return R.success();
}
@GetMapping("insert")

@ -2,15 +2,24 @@ 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.common.base.BaseController;
import com.wayn.common.base.ElasticEntity;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.vo.GoodsSaveRelatedVO;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.util.R;
import com.wayn.common.util.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
*
@ -26,6 +35,9 @@ public class GoodsController extends BaseController {
@Autowired
private IGoodsService iGoodsService;
@Autowired
private BaseElasticService baseElasticService;
@GetMapping("/list")
public R list(Goods goods) {
Page<Goods> page = getPage();
@ -51,4 +63,26 @@ public class GoodsController extends BaseController {
public R deleteGoods(@PathVariable Long goodsId) {
return R.result(iGoodsService.deleteGoodsRelatedByGoodsId(goodsId));
}
@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);
}
return R.result(baseElasticService.insertBatch("goods", entities));
}
}

@ -4,15 +4,18 @@ import com.alibaba.fastjson.JSON;
import com.wayn.common.base.ElasticEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
@ -105,17 +108,27 @@ public class BaseElasticService {
* @param idxName index
* @param entity
*/
public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
public String insertOrUpdateOne(String idxName, ElasticEntity entity) {
IndexRequest request = new IndexRequest(idxName);
log.error("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);
String result = null;
try {
restHighLevelClient.index(request, RequestOptions.DEFAULT);
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure :
shardInfo.getFailures()) {
return failure.reason();
}
}
result = indexResponse.getId();
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
@ -125,15 +138,21 @@ public class BaseElasticService {
* @param idxName index
* @param list
*/
public void insertBatch(String idxName, List<ElasticEntity> list) {
public boolean insertBatch(String idxName, List<ElasticEntity> list) {
BulkRequest request = new BulkRequest();
list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
.source(item.getData(), XContentType.JSON)));
try {
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
BulkResponse bulkResponse = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
if (bulkResponse.hasFailures()) {
return false;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
}
/**

@ -0,0 +1,23 @@
{
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"countPrice": {
"type": "float"
},
"retailPrice": {
"type": "float"
},
"keyword": {
"type": "keyword"
},
"isOnSale": {
"type": "boolean"
}
}
}

@ -73,4 +73,9 @@ public class SysConstants {
return msg;
}
/**
* es
*/
public static String ES_INDEX_GOODS_FILENAME = "/es/index/goods";
}

@ -21,7 +21,6 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
*
* @param filePath
* @param os
* @return
*/
public static void writeBytes(String filePath, OutputStream os) throws IOException {
FileInputStream fis = null;
@ -36,8 +35,6 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
while ((length = fis.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (IOException e) {
throw e;
} finally {
if (os != null) {
try {
@ -111,10 +108,37 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
return filename;
}
/**
*
*
* @param fileName
* @return
*/
public static String encodingFilename(String fileName) {
fileName = fileName.replace("_", " ");
fileName = Md5Utils.hash(fileName + System.nanoTime() + counter++);
return fileName;
}
/**
* resources
*
* @param inputStream is
* @return
*/
public static String getContent(InputStream inputStream) {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder content = new StringBuilder();
String temp = null;
while (true) {
try {
if (!((temp = br.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
content.append(temp);
}
return content.toString();
}
}

Loading…
Cancel
Save