SpringBoot项目中整合ElasticSearch:
1、首先在maven的pom.xml中加入以下配置:
<properties>
<!-- 设定和你的es版本相同的版本号,springboot默认的是2.x的版本-->
<elasticsearch.version>6.5.4</elasticsearch.version>
</properties>
2、在pom.xml中增加:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j.core</artifactId>
<version>2.7</version>
</dependency>
3、在application.properties文件中添加
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%t] %-5p %c - %m%n
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
4、在项目中新建config目录,添加EsConfig.class文件:
@Configuration
public class EsConfig {
@Bean
public TransportClient client() throws UnknownHostException {
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("localhost"),
9300
);
Settings settings = Settings.builder().put("cluster.name","biges").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
}