• 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
    }


  • 相关阅读:
    Chrome浏览器扩展开发系列之三:Google Chrome浏览器扩展的架构
    Chrome浏览器扩展开发系列之一:初识Google Chrome扩展
    Chrome浏览器扩展开发系列之五:Page Action类型的Chrome浏览器扩展
    Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
    鼠标定位问题总结
    Chrome浏览器扩展开发系列之八:Chrome扩展的数据存储
    Chrome浏览器扩展开发系列之七:override页面
    Chrome浏览器扩展开发系列之六:options 页面
    Chrome浏览器扩展开发系列之二:Google Chrome浏览器扩展的调试
    Chrome浏览器扩展开发系列之九:Chrome浏览器的chrome.alarms.* API
  • 原文地址:https://www.cnblogs.com/javawxid/p/10967923.html
Copyright © 2020-2023  润新知