Spring Boot集成ElasticSearch

下载ElasticSearch

(这个是2.4.4版本的 , 用的Spring Boot版本是1.5.2.RELEASE , 版本不对会导致无法使用 , 具体对应版本有待研究)

https://www.elastic.co/downloads/past-releases/elasticsearch-2-4-4

Spring Boot集成ElasticSearch

// 添加  Spring Data Elasticsearch 的依赖
compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')

// 添加  JNA 的依赖
compile('net.java.dev.jna:jna:4.3.0'

application.properties

# Elasticsearch服务地址
spring.data.elasticsearch.cluster-nodes=localhost:9300
# 设置连接超时时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

定义索引

org.springframework.data.elasticsearch.annotations.Document

类名上加@Document(indexName = "索引名")注解

org.springframework.data.annotation.Id

定义一个主键字段加@Id注解  索引ID , 类型为String

定义资源库

自定义接口继承ElasticsearchRepository<索引,String>

猜你喜欢

转载自blog.csdn.net/weixin_40196043/article/details/79990896