20250410 创建ES
This commit is contained in:
parent
fd893c5540
commit
5b2537cedc
@ -67,6 +67,12 @@
|
||||
<artifactId>lettuce-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data Elasticsearch -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
|
@ -0,0 +1,80 @@
|
||||
package com.liang.sca.goods.controller;
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.lang.generator.SnowflakeGenerator;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.liang.sca.goods.mapper.ProductSkuMapper;
|
||||
import com.liang.sca.goods.mapper.ProductSpuMapper;
|
||||
import com.liang.sca.goods.mapper.ShopProductMapper;
|
||||
import com.liang.sca.goods.model.ProductSpu;
|
||||
import com.liang.sca.goods.model.ShopProduct;
|
||||
import com.liang.sca.goods.model.type.ShopProductInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/shop")
|
||||
public class ShopController {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchOperations elasticsearchOperations;
|
||||
|
||||
@Autowired
|
||||
private ShopProductMapper shopProductMapper;
|
||||
|
||||
@Autowired
|
||||
private ProductSpuMapper productSpuMapper;
|
||||
|
||||
@Autowired
|
||||
private ProductSkuMapper productSkuMapper;
|
||||
|
||||
@RequestMapping("save-es/batch")
|
||||
public String saveEsBatch() {
|
||||
Snowflake snowflake = IdUtil.getSnowflake();
|
||||
List<ShopProduct> shopProducts = shopProductMapper.selectList(new QueryWrapper<>());
|
||||
for (ShopProduct shopProduct : shopProducts) {
|
||||
ShopProductInfo shopProductInfo = new ShopProductInfo();
|
||||
shopProductInfo.setId(shopProduct.getShopId().toString() + shopProduct.getShopSpu().toString());
|
||||
shopProductInfo.setShopId(shopProduct.getShopId().toString());
|
||||
shopProductInfo.setShopName(shopProduct.getShopName());
|
||||
shopProductInfo.setSpuId(shopProduct.getShopSpu().toString());
|
||||
shopProductInfo.setProductName(shopProduct.getShopName());
|
||||
shopProductInfo.setStore(shopProduct.getShopStore());
|
||||
shopProductInfo.setSpuPrice(shopProduct.getShopPrice());
|
||||
ProductSpu productSpu = productSpuMapper.selectById(shopProduct.getShopSpu());
|
||||
shopProductInfo.setProductName(productSpu.getSpuName());
|
||||
List<Long> skus = shopProductMapper.skusByShopAndSpu(shopProduct.getShopId(), shopProduct.getShopSpu());
|
||||
String shopSku = JSONUtil.toJsonStr(skus);
|
||||
shopProductInfo.setSkus(shopSku);
|
||||
|
||||
// 获取索引操作对象
|
||||
IndexOperations indexOps = elasticsearchOperations.indexOps(ShopProductInfo.class);
|
||||
// 检查索引是否存在
|
||||
if (!indexOps.exists()) {
|
||||
// 如果索引不存在,则创建索引
|
||||
indexOps.create();
|
||||
}
|
||||
// 创建文档
|
||||
|
||||
// 创建索引查询
|
||||
IndexQuery indexQuery = new IndexQueryBuilder()
|
||||
.withId(shopProductInfo.getId().toString())
|
||||
.withObject(shopProductInfo)
|
||||
.build();
|
||||
// 执行索引操作
|
||||
elasticsearchOperations.index(indexQuery, IndexCoordinates.of("shop_product_info"));
|
||||
}
|
||||
return "saveEsBatch";
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.liang.sca.goods.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.liang.sca.goods.model.ShopProduct;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ShopProductMapper extends BaseMapper<ShopProduct> {
|
||||
|
||||
@Select("SELECT shop_sku FROM tb_shop_product WHERE shop_id = #{shopId} and shop_spu = #{spuId}")
|
||||
List<Long> skusByShopAndSpu(Long shopId, Long spuId);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.liang.sca.goods.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@TableName("tb_shop_product")
|
||||
@Data
|
||||
public class ShopProduct implements Serializable {
|
||||
|
||||
@TableId("shop_id")
|
||||
private Long shopId;
|
||||
|
||||
@TableField("shop_name")
|
||||
private String shopName;
|
||||
|
||||
@TableField("shop_spu")
|
||||
private Long shopSpu;
|
||||
|
||||
@TableField("shop_sku")
|
||||
private Long shopSku;
|
||||
|
||||
@TableField("shop_store")
|
||||
private Integer shopStore;
|
||||
|
||||
@TableField("shop_price")
|
||||
private BigDecimal shopPrice;
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.liang.sca.goods.model.type;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Document(indexName = "shop_product_info")
|
||||
@Data
|
||||
public class ShopProductInfo {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String shopProductId;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String shopId;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String shopName;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String spuId;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String productName;
|
||||
|
||||
@Field(type = FieldType.Text)
|
||||
private String skus;
|
||||
|
||||
@Field(type = FieldType.Integer)
|
||||
private Integer store;
|
||||
|
||||
@Field(type = FieldType.Double)
|
||||
private BigDecimal spuPrice;
|
||||
|
||||
@Field(type = FieldType.Integer)
|
||||
private Integer salesCount;
|
||||
|
||||
@Field(type = FieldType.Double)
|
||||
private String score;
|
||||
|
||||
}
|
@ -25,4 +25,9 @@ spring:
|
||||
max-active: 8
|
||||
max-wait: -1ms
|
||||
max-idle: 8
|
||||
min-idle: 0
|
||||
min-idle: 0
|
||||
elasticsearch:
|
||||
rest:
|
||||
uris: http://154.12.80.119:9200
|
||||
username: elastic
|
||||
password: 123456
|
Loading…
Reference in New Issue
Block a user