SpringBoot整合SpringDataElasticSearch
1.启动ES服务器
2.导入ElasticSearch起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
3.在配置文件中配置ES集群名称和服务器的地址
4.创建实体类和Dao接口
//指定类对应type名称,以及所在的索引
@Document(indexName = "blog1",type = "article")
public class Article {
//表示id属性
@Id
/*
index : 是否索引
store : 是否存储
type : 指定类型
analyzer : 指定存储时的分词器
searchAnalyzer : 指定查询该字段时,查询条件使用的分词器
*/
@Field(index = false,store = true,type = FieldType.Long)
private Long id;
@Field(index = true,store = true,analyzer = "ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text)
private String title;
@Field(index = true,store = true,analyzer = "ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text)
private String content;
Dao接口:
5.测试
运行时避免与redis发生冲突