commit
a6ff38159f
@ -85,6 +85,7 @@ public abstract class BaseElasticsearchService {
|
|||||||
*/
|
*/
|
||||||
protected void createIndexRequest(String index) {
|
protected void createIndexRequest(String index) {
|
||||||
try {
|
try {
|
||||||
|
deleteIndexRequest(index);
|
||||||
CreateIndexRequest request = new CreateIndexRequest(index);
|
CreateIndexRequest request = new CreateIndexRequest(index);
|
||||||
//Settings for this index
|
//Settings for this index
|
||||||
request.settings(Settings.builder()
|
request.settings(Settings.builder()
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package cn.lili.modules.goods.entity.dto;
|
||||||
|
|
||||||
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author paulG
|
||||||
|
* @since 2022/6/13
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class GoodsSkuDTO extends GoodsSku {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 6600436187015048097L;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "商品参数json")
|
||||||
|
private String params;
|
||||||
|
|
||||||
|
}
|
@ -5,6 +5,7 @@ import cn.lili.modules.goods.entity.dos.GoodsSku;
|
|||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -16,6 +17,7 @@ import java.util.List;
|
|||||||
* @since 2020-02-26 23:24:13
|
* @since 2020-02-26 23:24:13
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class GoodsSkuVO extends GoodsSku {
|
public class GoodsSkuVO extends GoodsSku {
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package cn.lili.modules.goods.mapper;
|
package cn.lili.modules.goods.mapper;
|
||||||
|
|
||||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||||
|
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
import org.apache.ibatis.annotations.Insert;
|
import org.apache.ibatis.annotations.Insert;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
@ -99,4 +103,15 @@ public interface GoodsSkuMapper extends BaseMapper<GoodsSku> {
|
|||||||
")")
|
")")
|
||||||
int replaceGoodsSku(@Param("goodsSku") GoodsSku goodsSku);
|
int replaceGoodsSku(@Param("goodsSku") GoodsSku goodsSku);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询商品skuDTO
|
||||||
|
*
|
||||||
|
* @param page 分页
|
||||||
|
* @param queryWrapper 查询条件
|
||||||
|
* @return 售后VO分页
|
||||||
|
*/
|
||||||
|
@Select("SELECT *,g.params as params FROM li_goods_sku gs inner join li_goods g on gs.goods_id = g.id ${ew.customSqlSegment}")
|
||||||
|
IPage<GoodsSkuDTO> queryByParams(IPage<GoodsSkuDTO> page, @Param(Constants.WRAPPER) Wrapper<GoodsSkuDTO> queryWrapper);
|
||||||
|
|
||||||
}
|
}
|
@ -5,9 +5,12 @@ import cn.lili.modules.goods.entity.dos.Goods;
|
|||||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
||||||
|
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
|
||||||
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
|
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -136,6 +139,15 @@ public interface GoodsSkuService extends IService<GoodsSku> {
|
|||||||
*/
|
*/
|
||||||
IPage<GoodsSku> getGoodsSkuByPage(GoodsSearchParams searchParams);
|
IPage<GoodsSku> getGoodsSkuByPage(GoodsSearchParams searchParams);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询商品sku信息
|
||||||
|
*
|
||||||
|
* @param searchParams 查询参数
|
||||||
|
* @return 商品sku信息
|
||||||
|
*/
|
||||||
|
IPage<GoodsSkuDTO> getGoodsSkuDTOByPage(Page<GoodsSkuDTO> page, Wrapper<GoodsSkuDTO> queryWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列表查询商品sku信息
|
* 列表查询商品sku信息
|
||||||
*
|
*
|
||||||
|
@ -18,6 +18,7 @@ import cn.lili.modules.goods.entity.dos.GoodsGallery;
|
|||||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
||||||
|
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
||||||
@ -44,9 +45,11 @@ import cn.lili.modules.search.utils.EsIndexUtil;
|
|||||||
import cn.lili.mybatis.util.PageUtil;
|
import cn.lili.mybatis.util.PageUtil;
|
||||||
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
|
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
|
||||||
import cn.lili.rocketmq.tags.GoodsTagsEnum;
|
import cn.lili.rocketmq.tags.GoodsTagsEnum;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -448,6 +451,11 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
|||||||
return this.page(PageUtil.initPage(searchParams), searchParams.queryWrapper());
|
return this.page(PageUtil.initPage(searchParams), searchParams.queryWrapper());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IPage<GoodsSkuDTO> getGoodsSkuDTOByPage(Page<GoodsSkuDTO> page, Wrapper<GoodsSkuDTO> queryWrapper) {
|
||||||
|
return this.baseMapper.queryByParams(page, queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列表查询商品sku信息
|
* 列表查询商品sku信息
|
||||||
*
|
*
|
||||||
|
@ -29,7 +29,7 @@ public interface EsGoodsIndexService {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Map<String, Integer> getProgress();
|
Map<String, Long> getProgress();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,8 +106,9 @@ public interface EsGoodsIndexService {
|
|||||||
* 初始化商品索引
|
* 初始化商品索引
|
||||||
*
|
*
|
||||||
* @param goodsIndexList 商品索引列表
|
* @param goodsIndexList 商品索引列表
|
||||||
|
* @param regeneratorIndex 是否重新生成索引
|
||||||
*/
|
*/
|
||||||
void initIndex(List<EsGoodsIndex> goodsIndexList);
|
void initIndex(List<EsGoodsIndex> goodsIndexList, boolean regeneratorIndex);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新商品索引的促销信息
|
* 更新商品索引的促销信息
|
||||||
|
@ -21,6 +21,7 @@ import cn.lili.elasticsearch.EsSuffix;
|
|||||||
import cn.lili.elasticsearch.config.ElasticsearchProperties;
|
import cn.lili.elasticsearch.config.ElasticsearchProperties;
|
||||||
import cn.lili.modules.goods.entity.dos.*;
|
import cn.lili.modules.goods.entity.dos.*;
|
||||||
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
|
||||||
|
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum;
|
||||||
@ -38,6 +39,7 @@ import cn.lili.modules.search.service.EsGoodsSearchService;
|
|||||||
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
|
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
|
||||||
import cn.lili.rocketmq.tags.GoodsTagsEnum;
|
import cn.lili.rocketmq.tags.GoodsTagsEnum;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -83,6 +85,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
|
|
||||||
private final Map<String, Field> fieldMap = ReflectUtil.getFieldMap(EsGoodsIndex.class);
|
private final Map<String, Field> fieldMap = ReflectUtil.getFieldMap(EsGoodsIndex.class);
|
||||||
|
|
||||||
|
|
||||||
|
private final String KEY_SUCCESS = "success";
|
||||||
|
private final String KEY_FAIL = "fail";
|
||||||
|
private final String KEY_PROCESSED = "processed";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchProperties elasticsearchProperties;
|
private ElasticsearchProperties elasticsearchProperties;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -128,7 +135,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
||||||
//为空则默认写入没有任务
|
//为空则默认写入没有任务
|
||||||
if (flag == null) {
|
if (flag == null) {
|
||||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false, 10L, TimeUnit.MINUTES);
|
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
||||||
}
|
}
|
||||||
//有正在初始化的任务,则提示异常
|
//有正在初始化的任务,则提示异常
|
||||||
if (Boolean.TRUE.equals(flag)) {
|
if (Boolean.TRUE.equals(flag)) {
|
||||||
@ -137,48 +144,58 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
|
|
||||||
//初始化标识
|
//初始化标识
|
||||||
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
||||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true);
|
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true, 10L, TimeUnit.MINUTES);
|
||||||
|
|
||||||
|
|
||||||
ThreadUtil.execAsync(() -> {
|
ThreadUtil.execAsync(() -> {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
LambdaQueryWrapper<Goods> goodsQueryWrapper = new LambdaQueryWrapper<>();
|
QueryWrapper<GoodsSkuDTO> skuQueryWrapper = new QueryWrapper<>();
|
||||||
goodsQueryWrapper.eq(Goods::getAuthFlag, GoodsAuthEnum.PASS.name());
|
skuQueryWrapper.eq("gs.auth_flag", GoodsAuthEnum.PASS.name());
|
||||||
goodsQueryWrapper.eq(Goods::getMarketEnable, GoodsStatusEnum.UPPER.name());
|
skuQueryWrapper.eq("gs.market_enable", GoodsStatusEnum.UPPER.name());
|
||||||
goodsQueryWrapper.eq(Goods::getDeleteFlag, false);
|
skuQueryWrapper.eq("gs.delete_flag", false);
|
||||||
|
|
||||||
|
|
||||||
|
Map<String, Long> resultMap = (Map<String, Long>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
|
||||||
|
|
||||||
|
if (CollUtil.isEmpty(resultMap)) {
|
||||||
|
QueryWrapper<GoodsSku> skuCountQueryWrapper = new QueryWrapper<>();
|
||||||
|
skuCountQueryWrapper.eq("auth_flag", GoodsAuthEnum.PASS.name());
|
||||||
|
skuCountQueryWrapper.eq("market_enable", GoodsStatusEnum.UPPER.name());
|
||||||
|
skuCountQueryWrapper.eq("delete_flag", false);
|
||||||
|
resultMap = new HashMap<>();
|
||||||
|
resultMap.put(KEY_SUCCESS, 0L);
|
||||||
|
resultMap.put(KEY_FAIL, 0L);
|
||||||
|
resultMap.put(KEY_PROCESSED, 0L);
|
||||||
|
resultMap.put("total", this.goodsSkuService.count(skuCountQueryWrapper));
|
||||||
|
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), resultMap);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 1; ; i++) {
|
for (int i = 1; ; i++) {
|
||||||
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
|
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
|
||||||
IPage<Goods> page = new Page<>(i, 1000);
|
Page<GoodsSkuDTO> skuPage = new Page<>(i, 100);
|
||||||
IPage<Goods> goodsIPage = goodsService.page(page, goodsQueryWrapper);
|
IPage<GoodsSkuDTO> skuIPage = goodsSkuService.getGoodsSkuDTOByPage(skuPage, skuQueryWrapper);
|
||||||
if (goodsIPage == null || CollUtil.isEmpty(goodsIPage.getRecords())) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (Goods goods : goodsIPage.getRecords()) {
|
|
||||||
LambdaQueryWrapper<GoodsSku> skuQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
skuQueryWrapper.eq(GoodsSku::getGoodsId, goods.getId());
|
|
||||||
skuQueryWrapper.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name());
|
|
||||||
skuQueryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
|
|
||||||
skuQueryWrapper.eq(GoodsSku::getDeleteFlag, false);
|
|
||||||
for (int j = 1; ; j++) {
|
|
||||||
IPage<GoodsSku> skuPage = new Page<>(j, 100);
|
|
||||||
IPage<GoodsSku> skuIPage = goodsSkuService.page(skuPage, skuQueryWrapper);
|
|
||||||
if (skuIPage == null || CollUtil.isEmpty(skuIPage.getRecords())) {
|
if (skuIPage == null || CollUtil.isEmpty(skuIPage.getRecords())) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
for (GoodsSkuDTO goodsSku : skuIPage.getRecords()) {
|
||||||
int skuSource = 100;
|
int skuSource = 100;
|
||||||
for (GoodsSku goodsSku : skuIPage.getRecords()) {
|
EsGoodsIndex esGoodsIndex = wrapperEsGoodsIndex(goodsSku);
|
||||||
EsGoodsIndex esGoodsIndex = wrapperEsGoodsIndex(goodsSku, goods);
|
long count = esGoodsIndices.stream().filter(j -> j.getGoodsId().equals(esGoodsIndex.getGoodsId())).count();
|
||||||
esGoodsIndex.setSkuSource(skuSource--);
|
if (count >= 1) {
|
||||||
|
skuSource -= count;
|
||||||
|
}
|
||||||
|
esGoodsIndex.setSkuSource(skuSource);
|
||||||
esGoodsIndices.add(esGoodsIndex);
|
esGoodsIndices.add(esGoodsIndex);
|
||||||
//库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量
|
//库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量
|
||||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
//批量插入索引,如果为第一次则删除原索引并创建新索引
|
||||||
this.initIndex(esGoodsIndices);
|
this.initIndex(esGoodsIndices, i == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
||||||
|
|
||||||
//初始化商品索引
|
//初始化商品索引
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -188,16 +205,17 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Integer> getProgress() {
|
public Map<String, Long> getProgress() {
|
||||||
Map<String, Integer> map = (Map<String, Integer>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
|
Map<String, Long> map = (Map<String, Long>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
||||||
map.put("flag", Boolean.TRUE.equals(flag) ? 1 : 0);
|
map.put("flag", Boolean.TRUE.equals(flag) ? 1L : 0L);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,7 +409,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initIndex(List<EsGoodsIndex> goodsIndexList) {
|
public void initIndex(List<EsGoodsIndex> goodsIndexList, boolean regeneratorIndex) {
|
||||||
if (goodsIndexList == null || goodsIndexList.isEmpty()) {
|
if (goodsIndexList == null || goodsIndexList.isEmpty()) {
|
||||||
//初始化标识
|
//初始化标识
|
||||||
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
||||||
@ -405,27 +423,18 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
//但是如果索引已经自动生成过,这里就不会创建索引,设置mapping,所以这里决定在初始化索引的同时,将已有索引删除,重新创建
|
//但是如果索引已经自动生成过,这里就不会创建索引,设置mapping,所以这里决定在初始化索引的同时,将已有索引删除,重新创建
|
||||||
|
|
||||||
//如果索引存在,则删除,重新生成。 这里应该有更优解。
|
//如果索引存在,则删除,重新生成。 这里应该有更优解。
|
||||||
if (this.indexExist(indexName)) {
|
boolean indexExist = this.indexExist(indexName);
|
||||||
deleteIndexRequest(indexName);
|
if (regeneratorIndex || !indexExist) {
|
||||||
}
|
|
||||||
|
|
||||||
//如果索引不存在,则创建索引
|
//如果索引不存在,则创建索引
|
||||||
createIndexRequest(indexName);
|
createIndexRequest(indexName);
|
||||||
Map<String, Integer> resultMap = new HashMap<>(16);
|
}
|
||||||
final String KEY_SUCCESS = "success";
|
|
||||||
final String KEY_FAIL = "fail";
|
Map<String, Long> resultMap = (Map<String, Long>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
|
||||||
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);
|
|
||||||
if (!goodsIndexList.isEmpty()) {
|
if (!goodsIndexList.isEmpty()) {
|
||||||
goodsIndexRepository.deleteAll();
|
|
||||||
for (EsGoodsIndex goodsIndex : goodsIndexList) {
|
for (EsGoodsIndex goodsIndex : goodsIndexList) {
|
||||||
try {
|
try {
|
||||||
log.info("生成商品索引:{}", goodsIndex);
|
log.info("生成商品索引:{}", goodsIndex);
|
||||||
addIndex(goodsIndex);
|
this.addIndex(goodsIndex);
|
||||||
resultMap.put(KEY_SUCCESS, resultMap.get(KEY_SUCCESS) + 1);
|
resultMap.put(KEY_SUCCESS, resultMap.get(KEY_SUCCESS) + 1);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("商品{}生成索引错误!", goodsIndex);
|
log.error("商品{}生成索引错误!", goodsIndex);
|
||||||
@ -436,7 +445,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
@Override
|
||||||
@ -813,30 +821,30 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
return elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME;
|
return elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
private EsGoodsIndex wrapperEsGoodsIndex(GoodsSku goodsSku, Goods goods) {
|
private EsGoodsIndex wrapperEsGoodsIndex(GoodsSkuDTO goodsSku) {
|
||||||
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
|
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
|
||||||
|
|
||||||
//商品参数索引
|
//商品参数索引
|
||||||
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
|
if (CharSequenceUtil.isNotEmpty(goodsSku.getParams())) {
|
||||||
List<GoodsParamsDTO> goodsParamDTOS = JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class);
|
List<GoodsParamsDTO> goodsParamDTOS = JSONUtil.toList(goodsSku.getParams(), GoodsParamsDTO.class);
|
||||||
index = new EsGoodsIndex(goodsSku, goodsParamDTOS);
|
index = new EsGoodsIndex(goodsSku, goodsParamDTOS);
|
||||||
}
|
}
|
||||||
//商品分类索引
|
//商品分类索引
|
||||||
if (goods.getCategoryPath() != null) {
|
if (goodsSku.getCategoryPath() != null) {
|
||||||
List<Category> categories = categoryService.listByIdsOrderByLevel(Arrays.asList(goods.getCategoryPath().split(",")));
|
List<Category> categories = categoryService.listByIdsOrderByLevel(Arrays.asList(goodsSku.getCategoryPath().split(",")));
|
||||||
if (!categories.isEmpty()) {
|
if (!categories.isEmpty()) {
|
||||||
index.setCategoryNamePath(ArrayUtil.join(categories.stream().map(Category::getName).toArray(), ","));
|
index.setCategoryNamePath(ArrayUtil.join(categories.stream().map(Category::getName).toArray(), ","));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//商品品牌索引
|
//商品品牌索引
|
||||||
Brand brand = brandService.getById(goods.getBrandId());
|
Brand brand = brandService.getById(goodsSku.getBrandId());
|
||||||
if (brand != null) {
|
if (brand != null) {
|
||||||
index.setBrandName(brand.getName());
|
index.setBrandName(brand.getName());
|
||||||
index.setBrandUrl(brand.getLogo());
|
index.setBrandUrl(brand.getLogo());
|
||||||
}
|
}
|
||||||
//店铺分类索引
|
//店铺分类索引
|
||||||
if (goods.getStoreCategoryPath() != null && CharSequenceUtil.isNotEmpty(goods.getStoreCategoryPath())) {
|
if (goodsSku.getStoreCategoryPath() != null && CharSequenceUtil.isNotEmpty(goodsSku.getStoreCategoryPath())) {
|
||||||
List<StoreGoodsLabel> storeGoodsLabels = storeGoodsLabelService.listByStoreIds(Arrays.asList(goods.getStoreCategoryPath().split(",")));
|
List<StoreGoodsLabel> storeGoodsLabels = storeGoodsLabelService.listByStoreIds(Arrays.asList(goodsSku.getStoreCategoryPath().split(",")));
|
||||||
if (!storeGoodsLabels.isEmpty()) {
|
if (!storeGoodsLabels.isEmpty()) {
|
||||||
index.setStoreCategoryNamePath(ArrayUtil.join(storeGoodsLabels.stream().map(StoreGoodsLabel::getLabelName).toArray(), ","));
|
index.setStoreCategoryNamePath(ArrayUtil.join(storeGoodsLabels.stream().map(StoreGoodsLabel::getLabelName).toArray(), ","));
|
||||||
}
|
}
|
||||||
|
@ -365,9 +365,12 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
//关键字检索
|
//关键字检索
|
||||||
if (CharSequenceUtil.isEmpty(searchDTO.getKeyword())) {
|
if (CharSequenceUtil.isEmpty(searchDTO.getKeyword())) {
|
||||||
List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
|
List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
|
||||||
GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 100, 10).setWeight(10);
|
GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 50, 10, 50).setWeight(2);
|
||||||
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchAllQuery(), skuNoScore);
|
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchAllQuery(), skuNoScore);
|
||||||
filterFunctionBuilders.add(skuNoBuilder);
|
filterFunctionBuilders.add(skuNoBuilder);
|
||||||
|
FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3);
|
||||||
|
FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchAllQuery(), buyCountScore);
|
||||||
|
filterFunctionBuilders.add(buyCountBuilder);
|
||||||
FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
|
FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
|
||||||
filterFunctionBuilders.toArray(builders);
|
filterFunctionBuilders.toArray(builders);
|
||||||
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
|
||||||
@ -561,7 +564,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + keyword + "*"), ScoreMode.None),
|
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + keyword + "*"), ScoreMode.None),
|
||||||
ScoreFunctionBuilders.weightFactorFunction(8)));
|
ScoreFunctionBuilders.weightFactorFunction(8)));
|
||||||
|
|
||||||
GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 100, 10).setWeight(7);
|
GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 50, 10, 50).setWeight(7);
|
||||||
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(goodsNameQuery, skuNoScore);
|
FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(goodsNameQuery, skuNoScore);
|
||||||
filterFunctionBuilders.add(skuNoBuilder);
|
filterFunctionBuilders.add(skuNoBuilder);
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class ElasticsearchController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/progress")
|
@GetMapping("/progress")
|
||||||
public ResultMessage<Map<String, Integer>> getProgress() {
|
public ResultMessage<Map<String, Long>> getProgress() {
|
||||||
return ResultUtil.data(esGoodsIndexService.getProgress());
|
return ResultUtil.data(esGoodsIndexService.getProgress());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,11 @@ package cn.lili.test.elasticsearch;
|
|||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import cn.lili.cache.Cache;
|
import cn.lili.cache.Cache;
|
||||||
import cn.lili.common.vo.PageVO;
|
import cn.lili.common.vo.PageVO;
|
||||||
|
import cn.lili.modules.goods.entity.dos.Goods;
|
||||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||||
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
||||||
|
import cn.lili.modules.goods.service.GoodsService;
|
||||||
import cn.lili.modules.goods.service.GoodsSkuService;
|
import cn.lili.modules.goods.service.GoodsSkuService;
|
||||||
import cn.lili.modules.promotion.service.PromotionService;
|
import cn.lili.modules.promotion.service.PromotionService;
|
||||||
import cn.lili.modules.search.entity.dos.EsGoodsAttribute;
|
import cn.lili.modules.search.entity.dos.EsGoodsAttribute;
|
||||||
@ -47,6 +49,9 @@ class EsTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private GoodsSkuService goodsSkuService;
|
private GoodsSkuService goodsSkuService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GoodsService goodsService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Cache cache;
|
private Cache cache;
|
||||||
|
|
||||||
@ -63,6 +68,13 @@ class EsTest {
|
|||||||
// System.out.println(HtmlUtil.filter("+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-"));
|
// System.out.println(HtmlUtil.filter("+ADw-script+AD4-alert(document.cookie)+ADw-/script+AD4-"));
|
||||||
// Date dt1 = new Date(2021, 12, 10);
|
// Date dt1 = new Date(2021, 12, 10);
|
||||||
// Date dt2 = new Date(2021, 12, 14);
|
// Date dt2 = new Date(2021, 12, 14);
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
|
||||||
|
Goods goods = new Goods();
|
||||||
|
goods.setGoodsName("测试商品" + i);
|
||||||
|
// goods.setAuthFlag();
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -132,7 +144,7 @@ class EsTest {
|
|||||||
esGoodsIndices.add(index);
|
esGoodsIndices.add(index);
|
||||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||||
}
|
}
|
||||||
esGoodsIndexService.initIndex(esGoodsIndices);
|
esGoodsIndexService.initIndex(esGoodsIndices, true);
|
||||||
Assertions.assertTrue(true);
|
Assertions.assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user