Skip to content

Commit

Permalink
!188 优化初始化商品索引,增加编辑商品时,保留原商品的销量、好评率和评价量
Browse files Browse the repository at this point in the history
Merge pull request !188 from OceansDeep/feature/pg
  • Loading branch information
OceansDeep authored and gitee-org committed Jun 9, 2022
2 parents 79e1f34 + 4316fa8 commit 7129410
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 28 deletions.
30 changes: 30 additions & 0 deletions buyer-api/src/main/java/cn/lili/init/EsGoodsIndexInitRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.lili.init;

import cn.lili.modules.search.service.EsGoodsIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
* @author paulG
* @since 2022/6/9
**/
@Component
@Slf4j
public class EsGoodsIndexInitRunner implements ApplicationRunner {

@Autowired
private EsGoodsIndexService esGoodsIndexService;


@Override
public void run(ApplicationArguments args) {
try {
esGoodsIndexService.initIndex();
} catch (Exception e) {
log.error("检测ES商品索引失败", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public RedisTemplate<Object, Object> redisTemplate(LettuceConnectionFactory lett
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() {
Config config = new Config();

if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
// 哨兵模式
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
Expand Down Expand Up @@ -145,6 +144,7 @@ public RedissonClient redisson() {
if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) {
singleServerConfig.setPassword(redisProperties.getPassword());
}
singleServerConfig.setPingConnectionInterval(1000);
}

return Redisson.create(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void createMapping(String index) throws Exception {
" \"type\": \"keyword\"\n" +
" },\n" +
" \"type\": {\n" +
" \"type\": \"long\"\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"value\": {\n" +
" \"type\": \"keyword\"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsGallery;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
Expand Down Expand Up @@ -166,8 +167,7 @@ public void update(Goods goods, GoodsOperationDTO goodsOperationDTO) {
} else {
skuList = new ArrayList<>();
for (Map<String, Object> map : goodsOperationDTO.getSkuList()) {
GoodsSku sku = null;
sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO);
GoodsSku sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO);
renderGoodsSku(sku, goodsOperationDTO);
skuList.add(sku);
//如果商品状态值不对,则es索引移除
Expand Down Expand Up @@ -636,6 +636,7 @@ void renderGoodsSkuList(List<GoodsSku> goodsSkuList, GoodsOperationDTO goodsOper
// 商品销售模式渲染器
salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderBatch(goodsSkuList, goodsOperationDTO));
for (GoodsSku goodsSku : goodsSkuList) {
extendOldSkuValue(goodsSku);
this.renderImages(goodsSku);
}
}
Expand All @@ -647,22 +648,43 @@ void renderGoodsSkuList(List<GoodsSku> goodsSkuList, GoodsOperationDTO goodsOper
* @param goodsOperationDTO 商品操作DTO
*/
void renderGoodsSku(GoodsSku goodsSku, GoodsOperationDTO goodsOperationDTO) {
extendOldSkuValue(goodsSku);
// 商品销售模式渲染器
salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderSingle(goodsSku, goodsOperationDTO));
this.renderImages(goodsSku);
}

/**
* 将原sku的一些不会直接传递的值放到新的sku中
*
* @param goodsSku 商品sku
*/
private void extendOldSkuValue(GoodsSku goodsSku) {
if (CharSequenceUtil.isNotEmpty(goodsSku.getGoodsId())) {
GoodsSku oldSku = this.getGoodsSkuByIdFromCache(goodsSku.getId());
if (oldSku != null) {
goodsSku.setCommentNum(oldSku.getCommentNum());
goodsSku.setViewCount(oldSku.getViewCount());
goodsSku.setBuyCount(oldSku.getBuyCount());
goodsSku.setGrade(oldSku.getGrade());
}
}
}

/**
* 渲染sku图片
*
* @param goodsSku
* @param goodsSku sku
*/
void renderImages(GoodsSku goodsSku) {
JSONObject jsonObject = JSONUtil.parseObj(goodsSku.getSpecs());
List<Map<String, String>> images = jsonObject.get("images", List.class);
if (images != null && !images.isEmpty()) {
goodsSku.setThumbnail(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getThumbnail());
goodsSku.setSmall(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getSmall());
GoodsGallery goodsGallery = goodsGalleryService.getGoodsGallery(images.get(0).get("url"));
goodsSku.setBig(goodsGallery.getOriginal());
goodsSku.setOriginal(goodsGallery.getOriginal());
goodsSku.setThumbnail(goodsGallery.getThumbnail());
goodsSku.setSmall(goodsGallery.getSmall());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author paulG
**/
@Data
@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME)
@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME, createIndex = false)
@ToString
@NoArgsConstructor
@Accessors(chain = true)
Expand Down Expand Up @@ -215,6 +215,7 @@ public class EsGoodsIndex implements Serializable {

/**
* 销售模式
*
* @see cn.lili.modules.goods.entity.enums.GoodsSalesModeEnum
*/
@Field(type = FieldType.Text)
Expand Down Expand Up @@ -277,7 +278,7 @@ public class EsGoodsIndex implements Serializable {
* @see PromotionTypeEnum
* value 为 促销活动实体信息
*/
@Field(type = FieldType.Nested)
@Field(type = FieldType.Text)
@ApiModelProperty("商品促销活动集合JSON,key 为 促销活动类型,value 为 促销活动实体信息 ")
private String promotionMapJson;

Expand Down Expand Up @@ -316,7 +317,7 @@ public EsGoodsIndex(GoodsSku sku) {
/**
* 参数索引增加
*
* @param sku 商品sku信息
* @param sku 商品sku信息
* @param goodsParamDTOS 商品参数信息
*/
public EsGoodsIndex(GoodsSku sku, List<GoodsParamsDTO> goodsParamDTOS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public interface EsGoodsIndexService {

/**
* 全局索引初始化
* 全局索引数据初始化
*/
void init();

Expand All @@ -31,6 +31,12 @@ public interface EsGoodsIndexService {
*/
Map<String, Integer> getProgress();


/**
* 全局索引初始化
*/
void initIndex();

/**
* 添加商品索引
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -127,7 +128,7 @@ public void init() {
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
//为空则默认写入没有任务
if (flag == null) {
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false, 10L, TimeUnit.MINUTES);
}
//有正在初始化的任务,则提示异常
if (Boolean.TRUE.equals(flag)) {
Expand Down Expand Up @@ -200,6 +201,25 @@ public Map<String, Integer> getProgress() {
return map;
}

@Override
public void initIndex() {
//索引名称拼接
String indexName = this.getIndexName();

//索引初始化,因为mapping结构问题:
//但是如果索引已经自动生成过,这里就不会创建索引,设置mapping,所以这里决定在初始化索引的同时,将已有索引删除,重新创建

boolean indexExist = this.indexExist(indexName);
log.info("检测 {} 索引结构是否存在:{}", indexName, indexExist);
if (!indexExist) {

log.info("初始化索引结构 {}", indexName);
//如果索引不存在,则创建索引
createIndexRequest(indexName);
}

}

@Override
public void addIndex(EsGoodsIndex goods) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import cn.lili.modules.search.entity.dto.HotWordsDTO;
import cn.lili.modules.search.entity.dto.ParamOptions;
import cn.lili.modules.search.entity.dto.SelectorOptions;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.service.EsGoodsSearchService;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -44,7 +42,6 @@
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;
Expand Down Expand Up @@ -73,9 +70,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
*/
@Autowired
private ElasticsearchOperations restTemplate;

@Autowired
private EsGoodsIndexService esGoodsIndexService;
/**
* 缓存
*/
Expand All @@ -84,10 +78,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {

@Override
public SearchPage<EsGoodsIndex> searchGoods(EsGoodsSearchDTO searchDTO, PageVO pageVo) {
boolean exists = restTemplate.indexOps(EsGoodsIndex.class).exists();
if (!exists) {
esGoodsIndexService.init();
}
if (CharSequenceUtil.isNotBlank(searchDTO.getKeyword())) {
cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), searchDTO.getKeyword());
}
Expand Down Expand Up @@ -364,7 +354,7 @@ private NativeSearchQueryBuilder createSearchQueryBuilder(EsGoodsSearchDTO searc
this.commonSearch(filterBuilder, searchDTO);

//智能推荐
this.recommended(filterBuilder,searchDTO);
this.recommended(filterBuilder, searchDTO);

//未上架的商品不显示
filterBuilder.must(QueryBuilders.matchQuery("marketEnable", GoodsStatusEnum.UPPER.name()));
Expand Down Expand Up @@ -406,30 +396,31 @@ private NativeSearchQueryBuilder createSearchQueryBuilder(EsGoodsSearchDTO searc

/**
* 商品推荐
*
* @param filterBuilder
* @param searchDTO
*/
private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) {

String currentGoodsId = searchDTO.getCurrentGoodsId();
if(CharSequenceUtil.isEmpty(currentGoodsId)) {
if (CharSequenceUtil.isEmpty(currentGoodsId)) {
return;
}

//排除当前商品
filterBuilder.mustNot(QueryBuilders.matchQuery("id",currentGoodsId));
filterBuilder.mustNot(QueryBuilders.matchQuery("id", currentGoodsId));

//查询当前浏览商品的索引信息
EsGoodsIndex esGoodsIndex = restTemplate.get(currentGoodsId, EsGoodsIndex.class);
if(esGoodsIndex==null) {
if (esGoodsIndex == null) {
return;
}
//推荐与当前浏览商品相同一个二级分类下的商品
String categoryPath = esGoodsIndex.getCategoryPath();
if(CharSequenceUtil.isNotEmpty(categoryPath)){
if (CharSequenceUtil.isNotEmpty(categoryPath)) {
//匹配二级分类
String substring = categoryPath.substring(0, categoryPath.lastIndexOf(","));
filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath",substring+"*"));
filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath", substring + "*"));
}

}
Expand Down

0 comments on commit 7129410

Please sign in to comment.