1.引入依赖
2.启动类上添加注解
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
因为已经配置过mysql数据源,所以要取消自动装配的数据源
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@EnableDiscoveryClient
@EnableFeignClients
/**
* 启动类
*/
public class ServiceSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSearchApplication.class, args);
}
}
3.创建Es对应实体类
@Data
@Document(indexName = "skues" ,shards = 3,replicas = 1)
public class SkuEs {
// 商品Id= skuId
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String keyword;
@Field(type = FieldType.Integer, index = false)
private Integer skuType;
@Field(type = FieldType.Integer, index = false)
private Integer isNewPerson;
@Field(type = FieldType.Long)
private Long categoryId;
@Field(type = FieldType.Text)
private String categoryName;
@Field(type = FieldType.Keyword, index = false)
private String imgUrl;
// es 中能分词的字段,这个字段数据类型必须是 text!keyword 不分词!
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Double)
private Double price;
@Field(type = FieldType.Integer, index = false)
private Integer stock;
@Field(type = FieldType.Integer, index = false)
private Integer perLimit;
@Field(type = FieldType.Integer, index = false)
private Integer sale;
@Field(type = FieldType.Long)
private Long wareId;
// 商品的热度!
@Field(type = FieldType.Long)
private Long hotScore = 0L;
@Field(type = FieldType.Object, index = false)
private List<String> ruleList;
4.创建Repository接口(继承ElasticsearchRepository)
public interface XxxRepository extends ElasticsearchRepository<SkuEs,Long> {
}
5.调用XxxRepository的方法
//此处只是简单举例
//直接 接口.可以调用操作es的方法
xxxRepository.save(skuEs);