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
}
es批量导入进一对多的数据的更多相关文章
- HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)
HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...
- es 批量导入文件
首先是json格式的文件: curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json 1 ...
- Excel 批量导入Mysql(创建表-追加数据)
之前弄数据库的时候, 测试excel导mysql, 中间用pandas 处理后再入库. 直接上代码, 此种有真意, 尽在不言中. #!/usr/bin/env python # coding: ut ...
- 使用BCP批量导入数据
本文原创,转载请标明出处 BCP 工具的使用 The bulk copy program utility (bcp) bulk copies data between an instance of M ...
- ELK数据批量导入
数据批量导入 • 使用 _bulk 批量导入数据 – 批 ...
- Elasticsearch —— bulk批量导入数据
在使用Elasticsearch的时候,一定会遇到这种场景--希望批量的导入数据,而不是一条一条的手动导入.那么此时,就一定会需要bulk命令! 更多内容参考我整理的Elk教程 bulk批量导入 批量 ...
- Shp数据批量导入Postgresql工具的原理和设计
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 在制作整体的开源工具箱产品中,数据入库是一个重要的环节.虽然 ...
- [Django]网页中利用ajax实现批量导入数据功能
url.py代码: url(r'^workimport/$', 'keywork.views.import_keywork', name='import_keywork') view.py代码: fr ...
- [diango]批量导入不重复数据
去年研究导入数据的时候写了一个批量导入数据的脚本,但有个问题,如果导入这批数据在数据库中已经存在,那么我们导入的数据不就重复了么,本文就讨论如何解决这个问题? 程序如下: #coding:utf-8 ...
随机推荐
- linux环境上报异常java.lang.NoSuchMethodError
23-Apr-2019 18:11:35.545 INFO [http-nio-10052-exec-10] org.apache.catalina.core.ApplicationContext.l ...
- Python爬虫入门教程之BeautifulSoup
模块安装 pip3 install beautifulsoup4 模块导入 from bs4 import BeautifulSoup 示例html内容 RPC是一种比较流行的RPC通信框架,由谷歌公 ...
- vue+element-ui动态生成多级表头,并且将有相同字段下不同子元素合并为同一个
element表头要多层生成,下一级表头数据源必须是当前表头的子一级,这样一层一层嵌套可以生成多层表头: 要把数据处理成这种类型的数据 var arr = []; for (var key in ob ...
- [转帖]超详细的PostgreSQL体系结构总结,值得收藏
超详细的PostgreSQL体系结构总结,值得收藏 https://www.toutiao.com/i6715390855772897800/ 原创 波波说运维 2019-07-26 00:03:00 ...
- .NET细节知识总结,不断更新
1.catch (Exception)和catch (Exception e) Exception 类包含许多子类 程序执行的时候要将每一个类都搜索一遍 以找到符合的异常类 这样是蛮消耗资源的 影响效 ...
- Python列表排序方法reverse、sort、sorted详解
python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...
- Python连接ORACLE操作
一.准备工作 1.安装cx_Oracle ttps://pypi.python.org/pypi下查找cx_Oracle并下载 执行安装命令 pip install cx_Oracle-6.0rc1- ...
- Python【HTTP响应状态码】
# HTTP响应状态码 ## 1xx:临时响应 #### 表示临时响应并需要请求者继续执行操作的状态代码. 100 **继续**请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正 ...
- 20191011-构建我们公司自己的自动化接口测试框架-Util的htmlreport模块
生成htmlreport的模块是我在网上随意找的一个版本,主要生成的report包括接口名称,接口url,请求数据,响应数据,断言词,断言结果等 具体的htmlreport代码如下: # -*- en ...
- 1234: 约瑟夫问题-输出最后的编号(Java)
WUSTOJ 1234: 约瑟夫问题-输出最后的编号 参考资料 约瑟夫问题--百度百科 Description n个人围成一圈,依次从1至n编号.从编号为1的人开始1至k报数,凡报数为k的人退出圈子, ...