Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十四(Spring Data Elasticsearch,将数据添加到索引库)
Posted 蓝盒子bluebox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十四(Spring Data Elasticsearch,将数据添加到索引库)相关的知识,希望对你有一定的参考价值。
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)十四(Spring Data Elasticsearch,将数据添加到索引库)
一、创建Elasticsearch相关内容
1、创建GoodsRepository
package com.leyou.search.repository;
import com.leyou.search.pojo.Goods;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface GoodsRepository extends ElasticsearchRepository<Goods,Long> {
}
(1)创建GoodsRepository对应的测试类
package com.leyou.search.repostory;
import com.leyou.search.pojo.Goods;
import com.leyou.search.repository.GoodsRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GoodsRepositoryTest {
@Autowired
private GoodsRepository goodsRepository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void testCreateIndex(){
template.createIndex(Goods.class);
template.putMapping(Goods.class);
}
}
运行测试
二、导入数据
1、创建SearchService,构建Goods对象
将数据库当中的SPU和SKU的信息封装为Goods对象,并导入Elasticsearch
package com.leyou.search.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.pojo.*;
import com.leyou.search.client.BrandClient;
import com.leyou.search.client.CategoryClient;
import com.leyou.search.client.GoodsClient;
import com.leyou.search.client.SpecificationClient;
import com.leyou.search.pojo.Goods;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.lucene.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class SearchService {
@Autowired
private CategoryClient categoryClient;
@Autowired
private BrandClient brandClient;
@Autowired
private GoodsClient goodsClient;
@Autowired
private SpecificationClient specClient;
public Goods buildGoods(Spu spu){
//查询分类
List<Category> categories = categoryClient.queryCategoryByIds(
Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
if(CollectionUtils.isEmpty(categories)){
throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND);
}
//将categories集合当中所有的name取出来封装为一个字符串集合
List<String> names = categories.stream().map(Category::getName).collect(Collectors.toList());
//查询品牌
Brand brand = brandClient.queryBrandById(spu.getBrandId());
if(brand == null){
throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
}
//搜索字段 将字符串集合变成一个字符串以空格为分隔拼接到后面
String all = spu.getTitle() + StringUtils.join(names," ") + brand.getName();
//查询sku
List<Sku> skuList = goodsClient.querySkuBySpuId(spu.getId());
if(CollectionUtils.isEmpty(skuList)){
throw new LyException(ExceptionEnum.GOODS_SKU_NOT_FOND);
}
//对Sku进行处理
List<Map<String,Object>> skus = new ArrayList<>();
//价格集合
ArrayList<Long> priceList = new ArrayList<Long>();
for (Sku sku : skuList) {
Map<String,Object> map = new HashMap<>();
map.put("id",sku.getId());
map.put("title",sku.getTitle());
map.put("price",sku.getPrice());
//截取sku当中图片逗号之前的第一个
map.put("images",StringUtils.substringBefore(sku.getImages(),","));
skus.add(map);
//处理价格
priceList.add(sku.getPrice());
}
//查询规格参数
List<SpecParam> params = specClient.queryParamList(null, spu.getCid3(), true);
if(CollectionUtils.isEmpty(params)){
throw new LyException(ExceptionEnum.SPEC_GROUP_NOT_FOND);
}
//查询商品详情
SpuDetail spuDetail = goodsClient.queryDetailById(spu.getId());
//获取通用规格参数,获取到通用规格参数的JSON字符串,将其转换为Map集合
Map<Long, String> genericSpec = JsonUtils.toMap(spuDetail.getGenericSpec(), Long.class, String.class);
//获取特有规格参数,获取到特有规格参数的JSON字符串,将其转换为Map集合,而Map集合当中的值是String,键为List集合
Map<Long, List<String>> specailSpec =
JsonUtils.nativeRead(spuDetail.getSpecialSpec(),
new TypeReference<Map<Long, List<String>>>(){});
//处理规格参数,key是规格参数的名称,值是规格参数的值
Map<String,Object> specs = new HashMap<>();
for (SpecParam param : params) {
//规格名称
String key = param.getName();
Object value = "";
//判断是否是通过规格参数
if(param.getGeneric()){
value = genericSpec.get(param.getId());
//判断是否是数值类型
if(param.getNumeric()){
//处理成段
value = chooseSegment(value.toString(),param);
}
}else {
value = specailSpec.get(param.getId());
}
//存入map
specs.put(key,value);
}
//构建good对象
Goods goods = new Goods();
goods.setBrandId(spu.getBrandId());
goods.setCid1(spu.getCid1());
goods.setCid2(spu.getCid2());
goods.setCid3(spu.getCid3());
goods.setCreateTime(spu.getCreateTime());
goods.setId(spu.getId());
goods.setAll(all);//搜索字段,包含标题,分类,品牌,规格等信息
goods.setPrice(priceList);// 所有sku价格的集合
goods.setSkus(JsonUtils.toString(skus));// 所有sku的集合的JSON格式
goods.setSpecs(specs);// 所有可以搜索的规格参数
goods.setSubTitle(spu.getSubTitle());
return goods;
}
private String chooseSegment(String value, SpecParam p) {
double val = NumberUtils.toDouble(value);
String result = "其它";
// 保存数值段
for (String segment : p.getSegments().split(",")) {
String[] segs = segment.split("-");
// 获取数值范围
double begin = NumberUtils.toDouble(segs[0]);
double end = Double.MAX_VALUE;
if(segs.length == 2){
end = NumberUtils.toDouble(segs[1]);
}
// 判断是否在范围内
if(val >= begin && val < end){
if(segs.length == 1){
result = segs[0] + p.getUnit() + "以上";
}else if(begin == 0){
result = segs[1] + p.getUnit() + "以下";
}else{
result = segment + p.getUnit();
}
break;
}
}
return result;
}
}
2、然后编写一个测试类,循环查询Spu,然后调用IndexService中的方法,把SPU变为Goods,然后写入索引库:
package com.leyou.search.repostory;
import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Spu;
import com.leyou.search.client.GoodsClient;
import com.leyou.search.pojo.Goods;
import com.leyou.search.repository.GoodsRepository;
import com.leyou.search.service.SearchService;
import org.apache.lucene.util.CollectionUtil;
import org.aspectj.weaver.ast.Var;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GoodsRepositoryTest {
@Autowired
private GoodsRepository goodsRepository;
@Autowired
private ElasticsearchTemplate template;
@Autowired
private GoodsClient goodsClient;
@Autowired
private SearchService searchService;
@Test
public void testCreateIndex(){
template.createIndex(Goods.class);
template.putMapping(Goods.class);
}
@Test
public void loadData(){
int page = 1;
int rows = 100;
int size = 0;
do {
//查询spu的信息
PageResult<Spu> result = goodsClient.querySpuByPage(page, rows, true, null);
List<Spu> spuList = result.getItems();//得到当前页
if(CollectionUtils.isEmpty(spuList)){
break;
}
//构建成Goods
List<Goods> goodsList = spuList.stream().map(searchService::buildGoods).collect(Collectors.toList());
//存入索引库
goodsRepository.saveAll(goodsList);
//翻页
page++IDEA SpringBoot 项目打包成jar包
(超详解)SpringBoot高级部分-自动配置+监听机制+监控+项目部署