优化生成索引时,自动将分词信息添加到分词表

This commit is contained in:
paulGao 2022-06-29 09:33:02 +08:00
parent 170aa01946
commit 60a210a45f
3 changed files with 59 additions and 39 deletions

View File

@ -3,6 +3,7 @@ package cn.lili.modules.search.entity.vo;
import cn.lili.modules.search.entity.dos.CustomWords;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* @author paulG
@ -10,8 +11,13 @@ import lombok.EqualsAndHashCode;
**/
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class CustomWordsVO extends CustomWords {
private static final long serialVersionUID = 143299060233417009L;
public CustomWordsVO(String name) {
this.setName(name);
this.setDisabled(1);
}
}

View File

@ -68,7 +68,7 @@ public class CustomWordsServiceImpl extends ServiceImpl<CustomWordsMapper, Custo
LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<CustomWords>().eq(CustomWords::getName, customWordsVO.getName());
CustomWords one = this.getOne(queryWrapper, false);
if (one != null && one.getDisabled().equals(1)) {
throw new ServiceException(ResultCode.CUSTOM_WORDS_EXIST_ERROR);
return false;
} else if (one != null && !one.getDisabled().equals(1)) {
this.remove(queryWrapper);
}

View File

@ -6,7 +6,6 @@ import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
@ -19,26 +18,33 @@ import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.elasticsearch.BaseElasticsearchService;
import cn.lili.elasticsearch.EsSuffix;
import cn.lili.elasticsearch.config.ElasticsearchProperties;
import cn.lili.modules.goods.entity.dos.*;
import cn.lili.modules.goods.entity.dos.Brand;
import cn.lili.modules.goods.entity.dos.Category;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dos.StoreGoodsLabel;
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.GoodsStatusEnum;
import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum;
import cn.lili.modules.goods.service.*;
import cn.lili.modules.goods.service.BrandService;
import cn.lili.modules.goods.service.CategoryService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.goods.service.StoreGoodsLabelService;
import cn.lili.modules.promotion.entity.dos.BasePromotions;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
import cn.lili.modules.promotion.service.PromotionService;
import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.modules.search.entity.dos.EsGoodsAttribute;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import cn.lili.modules.search.entity.vo.CustomWordsVO;
import cn.lili.modules.search.repository.EsGoodsIndexRepository;
import cn.lili.modules.search.service.CustomWordsService;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.service.EsGoodsSearchService;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum;
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.extension.plugins.pagination.Page;
@ -49,6 +55,8 @@ import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
@ -82,14 +90,10 @@ import java.util.stream.Collectors;
public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements EsGoodsIndexService {
private static final String IGNORE_FIELD = "serialVersionUID,promotionMap,id,goodsId";
private final Map<String, Field> fieldMap = ReflectUtil.getFieldMap(EsGoodsIndex.class);
private static final String KEY_SUCCESS = "success";
private static final String KEY_FAIL = "fail";
private static final String KEY_PROCESSED = "processed";
private final Map<String, Field> fieldMap = ReflectUtil.getFieldMap(EsGoodsIndex.class);
@Autowired
private ElasticsearchProperties elasticsearchProperties;
@Autowired
@ -97,10 +101,10 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Autowired
private EsGoodsSearchService goodsSearchService;
@Autowired
private GoodsWordsService goodsWordsService;
@Autowired
private PromotionService promotionService;
@Autowired
private CustomWordsService customWordsService;
@Autowired
private GoodsSkuService goodsSkuService;
@ -240,21 +244,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
public void addIndex(EsGoodsIndex goods) {
try {
//分词器分词
// AnalyzeRequest analyzeRequest = AnalyzeRequest.withIndexAnalyzer(getIndexName(), "ik_max_word", goods.getGoodsName());
// AnalyzeResponse analyze = client.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
// List<AnalyzeResponse.AnalyzeToken> tokens = analyze.getTokens();
// if (goods.getAttrList() != null && !goods.getAttrList().isEmpty()) {
// //保存分词
// for (EsGoodsAttribute esGoodsAttribute : goods.getAttrList()) {
// wordsToDb(esGoodsAttribute.getValue());
// }
// }
// //分析词条
// for (AnalyzeResponse.AnalyzeToken token : tokens) {
// //保存词条进入数据库
// wordsToDb(token.getTerm());
// }
this.analyzeAndSaveWords(goods);
//生成索引
goodsIndexRepository.save(goods);
} catch (Exception e) {
@ -270,6 +260,9 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Override
public void addIndex(List<EsGoodsIndex> goods) {
try {
for (EsGoodsIndex esGoodsIndex : goods) {
this.analyzeAndSaveWords(esGoodsIndex);
}
goodsIndexRepository.saveAll(goods);
} catch (Exception e) {
log.error("批量为商品生成索引异常", e);
@ -278,9 +271,39 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Override
public void updateIndex(EsGoodsIndex goods) {
this.analyzeAndSaveWords(goods);
goodsIndexRepository.save(goods);
}
/**
* 商品分词
*
* @param goods 商品信息
*/
private void analyzeAndSaveWords(EsGoodsIndex goods) {
try {
//分词器分词
AnalyzeRequest analyzeRequest = AnalyzeRequest.withIndexAnalyzer(getIndexName(), "ik_max_word", goods.getGoodsName());
AnalyzeResponse analyze = client.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
List<AnalyzeResponse.AnalyzeToken> tokens = analyze.getTokens();
if (goods.getAttrList() != null && !goods.getAttrList().isEmpty()) {
//保存分词
for (EsGoodsAttribute esGoodsAttribute : goods.getAttrList()) {
wordsToDb(esGoodsAttribute.getValue());
}
}
//分析词条
for (AnalyzeResponse.AnalyzeToken token : tokens) {
//保存词条进入数据库
wordsToDb(token.getTerm());
}
} catch (IOException e) {
log.info(goods + "分词错误", e);
}
}
/**
* 更新商品索引的的部分属性只填写更新的字段不需要更新的字段不要填写
*
@ -800,18 +823,9 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
}
try {
//是否有重复
GoodsWords entity = goodsWordsService.getOne(new LambdaQueryWrapper<GoodsWords>().eq(GoodsWords::getWords, words));
if (entity == null) {
GoodsWords goodsWords = new GoodsWords();
goodsWords.setWords(words);
goodsWords.setWholeSpell(PinyinUtil.getPinyin(words, ""));
goodsWords.setAbbreviate(PinyinUtil.getFirstLetter(words, ""));
goodsWords.setType(GoodsWordsTypeEnum.SYSTEM.name());
goodsWords.setSort(0);
goodsWordsService.save(goodsWords);
}
customWordsService.addCustomWords(new CustomWordsVO(words));
} catch (MyBatisSystemException me) {
log.error(words + "关键字已存在!");
log.error(words + "关键字已存在!", me);
} catch (Exception e) {
log.error("关键字入库异常!", e);
}