优化代码,增加buyer启动时检测es索引存在,如不存在则自动创建
This commit is contained in:
parent
79e1f34890
commit
d14763d52c
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -115,7 +115,6 @@ public class RedisConfig extends CachingConfigurerSupport {
|
|||||||
@Bean(destroyMethod = "shutdown")
|
@Bean(destroyMethod = "shutdown")
|
||||||
public RedissonClient redisson() {
|
public RedissonClient redisson() {
|
||||||
Config config = new Config();
|
Config config = new Config();
|
||||||
|
|
||||||
if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
|
if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
|
||||||
// 哨兵模式
|
// 哨兵模式
|
||||||
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
|
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
|
||||||
@ -145,6 +144,7 @@ public class RedisConfig extends CachingConfigurerSupport {
|
|||||||
if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) {
|
if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) {
|
||||||
singleServerConfig.setPassword(redisProperties.getPassword());
|
singleServerConfig.setPassword(redisProperties.getPassword());
|
||||||
}
|
}
|
||||||
|
singleServerConfig.setPingConnectionInterval(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Redisson.create(config);
|
return Redisson.create(config);
|
||||||
|
@ -123,7 +123,7 @@ public abstract class BaseElasticsearchService {
|
|||||||
" \"type\": \"keyword\"\n" +
|
" \"type\": \"keyword\"\n" +
|
||||||
" },\n" +
|
" },\n" +
|
||||||
" \"type\": {\n" +
|
" \"type\": {\n" +
|
||||||
" \"type\": \"long\"\n" +
|
" \"type\": \"integer\"\n" +
|
||||||
" },\n" +
|
" },\n" +
|
||||||
" \"value\": {\n" +
|
" \"value\": {\n" +
|
||||||
" \"type\": \"keyword\"\n" +
|
" \"type\": \"keyword\"\n" +
|
||||||
|
@ -277,7 +277,7 @@ public class EsGoodsIndex implements Serializable {
|
|||||||
* @see PromotionTypeEnum
|
* @see PromotionTypeEnum
|
||||||
* value 为 促销活动实体信息
|
* value 为 促销活动实体信息
|
||||||
*/
|
*/
|
||||||
@Field(type = FieldType.Nested)
|
@Field(type = FieldType.Text)
|
||||||
@ApiModelProperty("商品促销活动集合JSON,key 为 促销活动类型,value 为 促销活动实体信息 ")
|
@ApiModelProperty("商品促销活动集合JSON,key 为 促销活动类型,value 为 促销活动实体信息 ")
|
||||||
private String promotionMapJson;
|
private String promotionMapJson;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ import java.util.Map;
|
|||||||
public interface EsGoodsIndexService {
|
public interface EsGoodsIndexService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局索引初始化
|
* 全局索引数据初始化
|
||||||
*/
|
*/
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
@ -31,6 +31,12 @@ public interface EsGoodsIndexService {
|
|||||||
*/
|
*/
|
||||||
Map<String, Integer> getProgress();
|
Map<String, Integer> getProgress();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局索引初始化
|
||||||
|
*/
|
||||||
|
void initIndex();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加商品索引
|
* 添加商品索引
|
||||||
*
|
*
|
||||||
|
@ -200,6 +200,25 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
|||||||
return map;
|
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
|
@Override
|
||||||
public void addIndex(EsGoodsIndex goods) {
|
public void addIndex(EsGoodsIndex goods) {
|
||||||
try {
|
try {
|
||||||
|
@ -11,10 +11,8 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
|||||||
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
|
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
|
||||||
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
|
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
|
||||||
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
|
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.ParamOptions;
|
||||||
import cn.lili.modules.search.entity.dto.SelectorOptions;
|
import cn.lili.modules.search.entity.dto.SelectorOptions;
|
||||||
import cn.lili.modules.search.service.EsGoodsIndexService;
|
|
||||||
import cn.lili.modules.search.service.EsGoodsSearchService;
|
import cn.lili.modules.search.service.EsGoodsSearchService;
|
||||||
import com.alibaba.druid.util.StringUtils;
|
import com.alibaba.druid.util.StringUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -44,7 +42,6 @@ import org.springframework.data.elasticsearch.core.SearchHits;
|
|||||||
import org.springframework.data.elasticsearch.core.SearchPage;
|
import org.springframework.data.elasticsearch.core.SearchPage;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.redis.core.ZSetOperations;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -73,9 +70,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
*/
|
*/
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchOperations restTemplate;
|
private ElasticsearchOperations restTemplate;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private EsGoodsIndexService esGoodsIndexService;
|
|
||||||
/**
|
/**
|
||||||
* 缓存
|
* 缓存
|
||||||
*/
|
*/
|
||||||
@ -84,10 +78,6 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SearchPage<EsGoodsIndex> searchGoods(EsGoodsSearchDTO searchDTO, PageVO pageVo) {
|
public SearchPage<EsGoodsIndex> searchGoods(EsGoodsSearchDTO searchDTO, PageVO pageVo) {
|
||||||
boolean exists = restTemplate.indexOps(EsGoodsIndex.class).exists();
|
|
||||||
if (!exists) {
|
|
||||||
esGoodsIndexService.init();
|
|
||||||
}
|
|
||||||
if (CharSequenceUtil.isNotBlank(searchDTO.getKeyword())) {
|
if (CharSequenceUtil.isNotBlank(searchDTO.getKeyword())) {
|
||||||
cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), searchDTO.getKeyword());
|
cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), searchDTO.getKeyword());
|
||||||
}
|
}
|
||||||
@ -364,7 +354,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
this.commonSearch(filterBuilder, searchDTO);
|
this.commonSearch(filterBuilder, searchDTO);
|
||||||
|
|
||||||
//智能推荐
|
//智能推荐
|
||||||
this.recommended(filterBuilder,searchDTO);
|
this.recommended(filterBuilder, searchDTO);
|
||||||
|
|
||||||
//未上架的商品不显示
|
//未上架的商品不显示
|
||||||
filterBuilder.must(QueryBuilders.matchQuery("marketEnable", GoodsStatusEnum.UPPER.name()));
|
filterBuilder.must(QueryBuilders.matchQuery("marketEnable", GoodsStatusEnum.UPPER.name()));
|
||||||
@ -406,30 +396,31 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品推荐
|
* 商品推荐
|
||||||
|
*
|
||||||
* @param filterBuilder
|
* @param filterBuilder
|
||||||
* @param searchDTO
|
* @param searchDTO
|
||||||
*/
|
*/
|
||||||
private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) {
|
private void recommended(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchDTO) {
|
||||||
|
|
||||||
String currentGoodsId = searchDTO.getCurrentGoodsId();
|
String currentGoodsId = searchDTO.getCurrentGoodsId();
|
||||||
if(CharSequenceUtil.isEmpty(currentGoodsId)) {
|
if (CharSequenceUtil.isEmpty(currentGoodsId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//排除当前商品
|
//排除当前商品
|
||||||
filterBuilder.mustNot(QueryBuilders.matchQuery("id",currentGoodsId));
|
filterBuilder.mustNot(QueryBuilders.matchQuery("id", currentGoodsId));
|
||||||
|
|
||||||
//查询当前浏览商品的索引信息
|
//查询当前浏览商品的索引信息
|
||||||
EsGoodsIndex esGoodsIndex = restTemplate.get(currentGoodsId, EsGoodsIndex.class);
|
EsGoodsIndex esGoodsIndex = restTemplate.get(currentGoodsId, EsGoodsIndex.class);
|
||||||
if(esGoodsIndex==null) {
|
if (esGoodsIndex == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//推荐与当前浏览商品相同一个二级分类下的商品
|
//推荐与当前浏览商品相同一个二级分类下的商品
|
||||||
String categoryPath = esGoodsIndex.getCategoryPath();
|
String categoryPath = esGoodsIndex.getCategoryPath();
|
||||||
if(CharSequenceUtil.isNotEmpty(categoryPath)){
|
if (CharSequenceUtil.isNotEmpty(categoryPath)) {
|
||||||
//匹配二级分类
|
//匹配二级分类
|
||||||
String substring = categoryPath.substring(0, categoryPath.lastIndexOf(","));
|
String substring = categoryPath.substring(0, categoryPath.lastIndexOf(","));
|
||||||
filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath",substring+"*"));
|
filterBuilder.must(QueryBuilders.wildcardQuery("categoryPath", substring + "*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user