Skip to content

Commit

Permalink
!20 add elasticsearch progress
Browse files Browse the repository at this point in the history
Merge pull request !20 from OceansDeep/feature/pg
  • Loading branch information
OceansDeep authored and gitee-org committed Aug 11, 2021
2 parents 8532400 + 48146ed commit dbc5305
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 24 deletions.
10 changes: 10 additions & 0 deletions framework/src/main/java/cn/lili/cache/CachePrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ public enum CachePrefix {
*/
ARTICLE_CACHE,

/**
* 初始化索引
*/
INIT_INDEX_PROCESS,

/**
* 初始化索引标示
*/
INIT_INDEX_FLAG,

/**
* 店铺分类
*/
Expand Down
4 changes: 2 additions & 2 deletions framework/src/main/java/cn/lili/common/enums/ResultCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ public enum ResultCode {
CUSTOM_WORDS_EXIST_ERROR(90000, "当前自定义分词已存在!"),
CUSTOM_WORDS_NOT_EXIST_ERROR(90001, "当前自定义分词不存在!"),
CUSTOM_WORDS_SECRET_KEY_ERROR(90002, "秘钥验证失败!"),
CONNECT_NOT_EXIST(90000, "登录方式不存在"),

CONNECT_NOT_EXIST(90000, "登录方式不存在"),
ELASTICSEARCH_INDEX_INIT_ERROR(90003, "索引初始化失败!"),
;

private final Integer code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public void createMapping(String index) throws Exception {
" }\n" +
" }\n" +
" },\n" +
" \"recommend\": {\n" +
" \"type\": \"boolean\"\n" +
" },\n" +
" \"selfOperated\": {\n" +
" \"type\": \"boolean\"\n" +
" },\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class EsGoodsSearchDTO {
@ApiModelProperty(value = "品牌,可以多选 品牌Id@品牌Id@品牌Id")
private String brandId;

@ApiModelProperty("是否为推荐商品")
private Boolean recommend;

@ApiModelProperty(value = "价格", example = "10_30")
private String price;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.elasticsearch.BaseElasticsearchService;
import cn.lili.elasticsearch.EsSuffix;
import cn.lili.elasticsearch.config.ElasticsearchProperties;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dos.GoodsWords;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum;
import cn.lili.modules.goods.service.GoodsWordsService;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.BasePromotion;
import cn.lili.modules.promotion.entity.enums.PromotionStatusEnum;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.modules.promotion.service.PromotionService;
import cn.lili.modules.search.entity.dos.EsGoodsAttribute;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
Expand All @@ -36,7 +38,6 @@
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

Expand Down Expand Up @@ -65,7 +66,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Autowired
private PromotionService promotionService;
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
private Cache<Object> cache;

@Override
public void addIndex(EsGoodsIndex goods) {
Expand Down Expand Up @@ -152,6 +153,9 @@ public void deleteIndexById(String id) {

@Override
public void initIndex(List<EsGoodsIndex> goodsIndexList) {
if (goodsIndexList == null || goodsIndexList.isEmpty()) {
return;
}
//索引名称拼接
String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME;

Expand All @@ -165,12 +169,32 @@ public void initIndex(List<EsGoodsIndex> goodsIndexList) {

//如果索引不存在,则创建索引
createIndexRequest(indexName);
if (goodsIndexList != null && !goodsIndexList.isEmpty()) {
Map<String, Integer> resultMap = new HashMap<>();
final String KEY_SUCCESS = "success";
final String KEY_FAIL = "fail";
final String KEY_PROCESSED = "processed";
resultMap.put("total", goodsIndexList.size());
resultMap.put(KEY_SUCCESS, 0);
resultMap.put(KEY_FAIL, 0);
resultMap.put(KEY_PROCESSED, 0);
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix() + "", resultMap);
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true);
if (!goodsIndexList.isEmpty()) {
goodsIndexRepository.deleteAll();
for (EsGoodsIndex goodsIndex : goodsIndexList) {
addIndex(goodsIndex);
try {
addIndex(goodsIndex);
resultMap.put(KEY_SUCCESS, resultMap.get(KEY_SUCCESS) + 1);
} catch (Exception e) {
log.error("商品{}生成索引错误!", goodsIndex);
resultMap.put(KEY_FAIL, resultMap.get(KEY_FAIL) + 1);
}
resultMap.put(KEY_PROCESSED, resultMap.get(KEY_PROCESSED) + 1);
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), resultMap);
}
}
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), resultMap);
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
}

@Override
Expand Down Expand Up @@ -440,7 +464,7 @@ private void removePromotionKey(String currentKey, Map<String, Object> promotion
}
}
//移除促销信息
promotionMap.keySet().removeAll(removeKeys);
removeKeys.forEach(promotionMap.keySet()::remove);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ private void commonSearch(BoolQueryBuilder filterBuilder, BoolQueryBuilder query
String[] brands = searchDTO.getBrandId().split("@");
filterBuilder.must(QueryBuilders.termsQuery("brandId", brands));
}
if (searchDTO.getRecommend() != null) {
filterBuilder.filter(QueryBuilders.termQuery("storeId", searchDTO.getRecommend()));
}
//规格项判定
if (searchDTO.getNameIds() != null && !searchDTO.getNameIds().isEmpty()) {
filterBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termsQuery("attrList.nameId", searchDTO.getNameIds()), ScoreMode.None));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package cn.lili.controller.other;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
Expand All @@ -15,6 +21,7 @@
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -50,20 +57,28 @@ public class ElasticsearchController {
@Autowired
private PromotionService promotionService;

@Autowired
private Cache cache;

@GetMapping
public void init() {
//查询商品信息
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name());
queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
public ResultMessage<String> init() {

Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
if (Boolean.TRUE.equals(flag)) {
return ResultUtil.error(100000, "当前有任务在执行");
}
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
ThreadUtil.execAsync(() -> {
//查询商品信息
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name());
queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());

List<GoodsSku> list = goodsSkuService.list(queryWrapper);
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
String goodsId = null;
//库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量
for (GoodsSku goodsSku : list) {
goodsId = goodsSku.getGoodsId();
Goods goods = goodsService.getById(goodsId);
List<GoodsSku> list = goodsSkuService.list(queryWrapper);
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
//库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量
for (GoodsSku goodsSku : list) {
Goods goods = goodsService.getById(goodsSku.getGoodsId());
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
List<GoodsParamsDTO> goodsParamDTOS = JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class);
Expand All @@ -73,9 +88,18 @@ public void init() {
index.setPromotionMap(goodsCurrentPromotionMap);
esGoodsIndices.add(index);
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
}
//初始化商品索引
esGoodsIndexService.initIndex(esGoodsIndices);
});
return ResultUtil.success();
}

}
//初始化商品索引
esGoodsIndexService.initIndex(esGoodsIndices);
@GetMapping("/progress")
public ResultMessage<Map<String, Integer>> getProgress() {
Map<String, Integer> map = (Map<String, Integer>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
map.put("flag", Boolean.TRUE.equals(flag) ? 1 : 0);
return ResultUtil.data(map);
}
}

0 comments on commit dbc5305

Please sign in to comment.