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. idea多级目录与单级目录切换

  2. TCP通信的文件上传案例

  3. hdoj3336(kmp算法next数组的应用)

    题目链接:https://vjudge.net/problem/HDU-3336 题意:给定长为n(<=2e5)的字符串s,求s的每个前缀在s中出现的次数之和. 思路: 用dp[i]表示以s[i ...

  4. Fiddler之手机抓包

    1.Fiddle设置端口,Tools->Options->Connections, 2.手机设置代理服务器: 注意:要保证手机和PC电脑IP在同一个网段(或者同一个网关) (1).安卓手机 ...

  5. java读写cookie中文乱码解决方法

    1.写入的时候: public boolean addCookie( HttpServletRequest req, HttpServletResponse resp){ //创建 Cookie co ...

  6. Java基础---Java变量

    变量:程序运行期间,内容可以发生改变的量. 创建一个变量并且使用的格式: 数据类型 变量名称;         // 创建了一个变量 变量名称 = 数据值;           // 赋值,将右边的数 ...

  7. Markdown 空格详细介绍

      它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...

  8. centos7无网络环境下创建基于scratch镜像的Linux镜像,并带有Java运行环境

    一.准备 将下载好的jdk以及scratch镜像放在同一文件夹下:这里放在linux:2.0 二.导入scratch镜像 #docker load -i scratch.tar 三.创建dockerf ...

  9. 自定义模块,time,datetime以及random

    自定义模块,time,datetime以及random 1.自定义模块 自定义一个模块 import #导入 (拿工具箱) 模块分类 1.内置模块(标准库) --python解释器自带的.py文件(模 ...

  10. 十二、使用PWM调整LCD背光亮度

    和手机一样,开发板中也带有调整背光亮度的功能. 调整背光亮度依赖于PWM,它通过调节脉冲宽度来控制背光亮度,此方式需要使用PWM驱动.本章将对其进行讲解. 一.用户空间调整背光亮度 一般应用程序可以通 ...