1.elasticsearch配置类
package com.zc.docsearch.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* elasticsearch spring-data 目前支持的最高版本为5.5 所以需要自己注入生成客户端
* 这个三个接口可以使用 AbstractFactoryBean 通过继承重写方法代替
* Created by zengchao on 2018/4/19.
*/
@Configuration
public class ElasticsearchConfiguration implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchConfiguration.class);
@Value("${spring.data.elasticsearch.cluster-nodes}")
private String clusterNodes;
private RestHighLevelClient restHighLevelClient;
/**
* 控制Bean的实例化过程
*
* @return
* @throws Exception
*/
@Override
public RestHighLevelClient getObject() throws Exception {
return restHighLevelClient;
}
/**
* 获取接口返回的实例的class
*
* @return
*/
@Override
public Class<?> getObjectType() {
return RestHighLevelClient.class;
}
@Override
public void destroy() throws Exception {
try {
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
} catch (final Exception e) {
LOG.error("Error closing ElasticSearch client: ", e);
}
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public void afterPropertiesSet() throws Exception {
restHighLevelClient = buildClient();
}
private RestHighLevelClient buildClient() {
try {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost(
clusterNodes.split(":")[0],
Integer.parseInt(clusterNodes.split(":")[1]),
"http")));
} catch (Exception e) {
LOG.error(e.getMessage());
}
return restHighLevelClient;
}
}
2.application.properties
spring.data.elasticsearch.cluster-nodes=112.74.63.235:9200
3.测试
@Test
public void getIndexTest(){
GetRequest request=new GetRequest("store","product","4");
try {
GetResponse response = client.get(request);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
4.pom.xml 请根据自己版本修改
<!-- Elasticsearch Dependencies -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>6.2.3</version>
</dependency>