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 }