java整合ElasticSearch导入数据到es中
程序员文章站
2022-03-06 12:17:20
实体类:利用注解建立实体类会自动根据实体类创建索引,不需要自己建立mappingpackage com.msic.mall.search.domain;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.experimental.Accessors;import org.springframework.data.annotation.Id;imp...
实体类:利用注解建立实体类会自动根据实体类创建索引,不需要自己建立mapping
package com.msic.mall.search.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
/**
* 搜索中的商品信息
* Created by macro on 2018/6/19.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Document(indexName = "pms_product", type = "_doc",shards = 1,replicas = 0)
public class EsProduct implements Serializable {
@Id
private Long id;
private Long shopId;
@Field(type = FieldType.Keyword)
private String shopName;
private String shopIcon;
private Long spuId;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String productName;
private String productIcon;
private BigDecimal productPrice;
@Field(type = FieldType.Integer)
private Integer sales; //销量
private Integer stock;//库存,
private boolean hasStock;//库存,
@Field(type = FieldType.Integer)
private Integer newStatus; //新品
@Field(type = FieldType.Integer)
private Integer advanceSaleStatus;//预售
private Long brandId;// 品牌id
@Field(type = FieldType.Keyword)
private String brandName;// 品牌名称
private Long productCategoryId; //分类id
@Field(type = FieldType.Keyword)
private String productCategoryName; //商品分类名称
// @Field(analyzer = "ik_max_word",type = FieldType.Text)
// private String subTitle; // 副标题
// @Field(analyzer = "ik_max_word",type = FieldType.Text)
// private String keywords;
private Integer sort; //排序
// @Field(type = FieldType.Keyword)
private String specificationList;
private Long hotScore;// 热度值
// @Field(type = FieldType.Nested)
// private List<EsProductAttributeValue> attrValueList;
private static final long serialVersionUID = -1L;
// 货号
// @Field(type = FieldType.Keyword)
// private String productSn;
// private Integer promotionType;
// 是否推荐
// private Integer recommandStatus;
}
controller
@ApiOperation(value = "导入所有数据库中商品到ES")
@PostMapping(value = "/importAll")
public CommonResponseResult<Integer> importAllList() {
CommonResponseResult responseResult = null;
Map map = new HashMap();
try {
int count = esProductService.importAll();
map.put("result",count+"条记录被添加");
responseResult=CommonResponseResult.ok(map);
}catch (Exception e){
e.printStackTrace();
responseResult=CommonResponseResult.notOk(e.getMessage());
}
return responseResult;
}
service:查出需要导入的数据
@Override
public int importAll() {
List<EsProduct> esProductList = productDao.getAllEsProductList(null);//从数据库查询数据
for (EsProduct product:
esProductList) {
product.setProductIcon(dfsUrlPrefix+product.getProductIcon());
LOGGER.info(product.getSpecificationList());
}
int result = 0;
try {
try {
productRepository.deleteAll();
}catch (Exception e){
e.printStackTrace();
throw new MyException("原数据删除失败");
}
Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);
Iterator<EsProduct> iterator = esProductIterable.iterator();
while (iterator.hasNext()) {
result++;
iterator.next();
}
}catch (Exception e){
e.printStackTrace();
throw new MyException("数据导入失败,请重试");
}
return result;
}
本文地址:https://blog.csdn.net/qq_45151158/article/details/112008944