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
}
												

es批量导入进一对多的数据的更多相关文章

  1. HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)

    HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...

  2. es 批量导入文件

    首先是json格式的文件: curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json 1 ...

  3. Excel 批量导入Mysql(创建表-追加数据)

    之前弄数据库的时候, 测试excel导mysql, 中间用pandas 处理后再入库.  直接上代码, 此种有真意, 尽在不言中. #!/usr/bin/env python # coding: ut ...

  4. 使用BCP批量导入数据

    本文原创,转载请标明出处 BCP 工具的使用 The bulk copy program utility (bcp) bulk copies data between an instance of M ...

  5. ELK数据批量导入

                                                                            数据批量导入 • 使用 _bulk 批量导入数据 – 批 ...

  6. Elasticsearch —— bulk批量导入数据

    在使用Elasticsearch的时候,一定会遇到这种场景--希望批量的导入数据,而不是一条一条的手动导入.那么此时,就一定会需要bulk命令! 更多内容参考我整理的Elk教程 bulk批量导入 批量 ...

  7. Shp数据批量导入Postgresql工具的原理和设计

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 在制作整体的开源工具箱产品中,数据入库是一个重要的环节.虽然 ...

  8. [Django]网页中利用ajax实现批量导入数据功能

    url.py代码: url(r'^workimport/$', 'keywork.views.import_keywork', name='import_keywork') view.py代码: fr ...

  9. [diango]批量导入不重复数据

    去年研究导入数据的时候写了一个批量导入数据的脚本,但有个问题,如果导入这批数据在数据库中已经存在,那么我们导入的数据不就重复了么,本文就讨论如何解决这个问题? 程序如下: #coding:utf-8 ...

随机推荐

  1. 系统获取 IP 工具类

    系统获取 IP 工具类 import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterf ...

  2. Hadoop的eclipse的插件是怎么安装的?

    [学习笔记] 1)网上下载hadoop-eclipse-plugin-2.7.4.jar,将该jar包拷贝到Eclipse安装目录下的dropins文件夹下,我的目录是C:\Users\test\ec ...

  3. iOS开发支付篇——内购(IAP)详解

    1 <em>内购所需要的资料整理总结,史上最完整的,哈哈哈哈哈哈</em> 思维导图 重点总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 1.获取内购列表( ...

  4. PAT甲级 图的遍历 相关题_C++题解

    图的遍历 PAT (Advanced Level) Practice 图的遍历 相关题 目录 <算法笔记>重点摘要 1021 Deepest Root (25) 1076 Forwards ...

  5. Python之正则表达式笔记

    概述 概念 Regular Expression 一种文本模式,描述在搜索文本时要匹配的一个或多个字符串 典型场景 数据验证.文本扫描.文本提取.文本替换.文本分割 语法 字面值 普通字符 需转义:\ ...

  6. Http中的同步请求和异步请求

    最近在上springmvc的JSON数据交换的时候,老师下课提了一个课后问题:什么是异步请求?什么是同步请求?我想大部分同学听到这个问题的时候应该和我一样不知所云.现在,给大家分享一篇关于同步请求和异 ...

  7. Qt里的原子操作QAtomicInteger,有挑战性,使用Q_ATOMIC_INT{nn}_IS_SUPPORTED测试系统是否支持

    所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...

  8. (三)CXF之处理输入参数与输出类型为复杂类型的webService服务

    一.需求 调用webService服务,把用户名和密码封装为用户对象作为参数,返回该用户所用友的角色列表. 二.发布服务 2.1 编写服务接口 @WebService public interface ...

  9. LeetCode 754. Reach a Number

    754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...

  10. 第三代PacBio测序技术的测序原理和读长

    针对PacBio单分子测序——第三代测序技术的测序原理和读长     DNA基因测序技术从上世纪70年代起,历经三代技术后,目前已发展成为一项相对成熟的生物产业.测序技术的应用也扩展到了生物.医学.制 ...