spring data 对很多nosql以及sql提供了统一的操作 spring data elastic
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
在yml文件中添加支持
spring:
data:
elasticsearch:
cluster-nodes: 192.168.100.161:9200
cluster-name: "elasticsearch"
添加对应实体类
@Document注解
indexName表示es中的索引 type表示所属的类型
对应于 Elasticsearch 的 Index 和Type
@Document(indexName = "product", type = "book")
@Data
public class BookEntity {
@Id
String id;
String name;
String massage;
Date posDate;
String type;
}
编写Repository
ElasticsearchRepository
简单的包含了增删改查,同理与jpa
public interface BookDao extends ElasticsearchRepository<BookEntity, String> {
public List<BookEntity> getByMessage(String key);
}
es与spring data 的对应关系
如果包含翻页也可以使用page方法进行操控,同理与spring data jpa 的控制
## 尾声
到此为止es与springboot的简单整合就算是完成了,springboot目前对es的自动装配只支持到了2.x版本也就是说5.x以及6.xspringbooty都无法支持.