环境:
ES: 7.12.0
1、springboot工程引入es相关jar
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.12.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.12.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>reindex-client</artifactId>
<version>7.12.0</version>
</dependency>
<!-- Java High Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.0</version>
</dependency>
2、增加es自定义配置
elasticsearch:
configs:
sport: #索引配置
userName: 1
password: 2
host: 127.0.0.1
port: 9200
indexName: sport #索引名称
timeOut: 1000 #请求超时时间,单位秒
study:
indexName: study
help: sssssssssssssssssssss
3、JAVA代码
package com.example.elasticSearch; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.util.Map; @Configuration @ConfigurationProperties(prefix = "elasticsearch") public class ESConfig { Map<String, Config> configs; String help; public String getHelp() { return help; } public void setHelp(String help) { this.help = help; } public Map<String, Config> getConfigs() { return configs; } public void setConfigs(Map<String, Config> configs) { this.configs = configs; } static class Config { private String userName; private String password; private String host; private Integer port; private String indexName; private long timeout; public long getTimeout() { return timeout; } public void setTimeout(long timeout) { this.timeout = timeout; } public String getIndexName() { return indexName; } public void setIndexName(String indexName) { this.indexName = indexName; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } } }