1. 我的springboot版本为2.2.6.RELEASE, 本文推荐elasticsearch-rest-high-level-client在 springboot里集成elasticsearch,首先找到当前es对应的elasticsearch-rest-high-level-client版本
1.1 登录es官网https://www.elastic.co/en/elasticsearch/
1.2 找到doc页面
1.2 搜索ElasticSearch Client,页面左边选择clients和对应的es版本
1.3 往下拉找到Java Rest Client
1.4 点进去一步一步找到需要依赖的包的版本页面,包的版本位置如下
2. 拷贝依赖代码到pom.xml里
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.1</version>
</dependency>
3. springboot yml里配置es properties
elasticsearch:
schema: http #请求协议
clusterNodes: 10.67.9.31:9200,10.67.9.31:9201
connectTimeout: 1000 #连接超时时间(毫秒)
socketTimeout: 30000 # socket 超时时间
connRequestTimeout: 500 #连接请求超时时间
index:
numberOfShards: 5 #分片数量
numberOfReplicas: 1 #副本数
4. 编写EsProperties类获取es配置
@Configuration
@ConfigurationProperties("elasticsearch")
public class ESProperties {
/**
* 请求协议
*/
private String schema;
/**
* 集群节点
*/
private String clusterNodes;
/**
* 连接超时时间(毫秒)
*/
private Integer connectTimeout;
/**
* socket 超时时间
*/
private Integer socketTimeout;
/**
* 连接请求超时时间
*/
private Integer connRequestTimeout;
/**
* 索引配置信息
*/
private Index index;
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getClusterNodes() {
return clusterNodes;
}
public void setClusterNodes(String clusterNodes) {
this.clusterNodes = clusterNodes;
}
public Integer getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
}
public Integer getSocketTimeout() {
return socketTimeout;
}
public void setSocketTimeout(Integer socketTimeout) {
this.socketTimeout = socketTimeout;
}
p