es批量导入进一对多的数据

es批量导入进一对多的数据

我有一个产品表

一个产品对应多个属性名

一个属性名对应多个属性值

一个产品还对应一个分类名称

 

   控制层

@ApiOperation(value = "导入所有产品信息数据库中商品到ES")
@RequestMapping(value = "/importAll", method = RequestMethod.POST)
@ResponseBody
public CommonResult<Integer> importAllList() {
int count = esProductService.importAll();
return CommonResult.success(count);
}

业务实现类

@Autowired
private EsProductDao productDao;
@Autowired
private EsProductRepository productRepository;
@Override
public int importAll() {
   //从库中查询出数据
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
  //将数据导入到es中
Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);
Iterator<EsProduct> iterator = esProductIterable.iterator();
int result = 0;
while (iterator.hasNext()) {
result++;
iterator.next();
}
return result;
}

mybatis中getAllEsProductList的mapper.xml

<mapper namespace="com.macro.mall.search.dao.EsProductDao">
<resultMap id="esProductListMap" type="com.macro.mall.search.domain.EsProduct">
<id column="productId" jdbcType="BIGINT" property="id" />
<result column="productSn" jdbcType="VARCHAR" property="productSn"/>
<result column="brandId" jdbcType="BIGINT" property="brandId"/>
<result column="brandName" jdbcType="VARCHAR" property="brandName"/>
<result column="productCategoryId" jdbcType="BIGINT" property="productCategoryId"/>
<result column="productName" jdbcType="VARCHAR" property="productName"/>
<result column="subTitle" jdbcType="VARCHAR" property="subTitle"/>
<result column="price" jdbcType="DECIMAL" property="price"/>
<result column="keywords" jdbcType="VARCHAR" property="keywords"/>
<association property="productCategorie" columnPrefix="pc" javaType="com.macro.mall.search.domain.EsProductCategory">
<id column="productCategoryId" property="id" jdbcType="BIGINT"/>
<result column="productCategoryName" property="productCategoryName" jdbcType="VARCHAR"/>
</association>
<collection property="attributeList" ofType="com.macro.mall.search.domain.EsProductAttribute" javaType="java.util.ArrayList">
<id column="paProductAttributeId" property="id" jdbcType="BIGINT"/>
<result column="paProductAttributeName" property="paProductAttributeName" jdbcType="VARCHAR"/>
<collection property="attributeValues" ofType="com.macro.mall.search.domain.EsProductAttributeValue" javaType="java.util.ArrayList">
<id column="pavProductAttributeValueId" property="id" jdbcType="BIGINT"/>
<result column="pavProductAttributeValue" property="value" jdbcType="VARCHAR"/>
</collection>
</collection>
</resultMap>
<select id="getAllEsProductList" resultMap="esProductListMap">
SELECT
p.id productId,
p.product_sn productSn,
p.brand_id brandId,
p.brand_name brandName,
p.product_category_id productCategoryId,
p.name productName,
p.sub_title subTitle,
p.price price,
p.keywords keywords,
pav.id pavProductAttributeValueId,
pav.`value` pavProductAttributeValue,
pa.id paProductAttributeId,
pa.`name` paProductAttributeName,
pc.id pcProductCategoryId,
pc.`name` pcProductCategoryName
FROM pms_product p
LEFT JOIN pms_product_attribute_value pav ON p.id = pav.product_id
LEFT JOIN pms_product_attribute pa ON pav.product_attribute_id= pa.id
LEFT JOIN pms_product_category pc ON p.`product_category_id` = pc.`id`
WHERE delete_status = 0 AND publish_status = 1
<if test="id!=null">
and p.id=#{id}
</if>
</select>
</mapper>
 
 
自定义的接口productRepository
 
 
public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {

}
 

这个是我的产品实体类

@Document(indexName = "product", type = "productInfo",shards = 1,replicas = 0)
public class EsProduct implements Serializable {

private static final long serialVersionUID = 2372551074091780419L;
@Id
private Long id;
@Field(type = FieldType.Keyword)
private String productSn;
private Long brandId;
@Field(type = FieldType.Keyword)
private String brandName;
private Long productCategoryId;
@Field(type = FieldType.Keyword)
private String productName;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String subTitle;
private BigDecimal price;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String keywords;

@Field(type =FieldType.Nested)
private List<EsProductAttribute> attributeList;

@Field(type =FieldType.Nested)
private EsProductCategory productCategorie;

这个是我的产品属性实体类
public class EsProductAttribute implements Serializable {

private static final long serialVersionUID = 4965902919813623705L;
@Id
private Long id;

@Field(type = FieldType.Keyword)
private String paProductAttributeName;//属性名称

@Field(type =FieldType.Nested)
private List<EsProductAttributeValue> attributeValues;
这个是我的产品属性值实体类
public class EsProductAttributeValue implements Serializable {
private static final long serialVersionUID = 6713756365860464751L;

private Long id;
private Long productAttributeId;
//属性值
@Field(type = FieldType.Keyword)
private String value;
上面三个实体类都提供get和set方法

操作之后,添加成功
{
  "code": 200,
  "message": "操作成功",
  "data": 17 }


转载于:https://www.cnblogs.com/javawxid/p/10967923.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值