Last active
December 26, 2018 17:16
-
-
Save chathurangat/eac7eafa46c90aafbf3c754f666053c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.elasticsearch.client.Client; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.transport.TransportAddress; | |
import org.elasticsearch.transport.client.PreBuiltTransportClient; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; | |
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | |
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; | |
import java.net.InetAddress; | |
@Configuration | |
@EnableElasticsearchRepositories(basePackages = "com.springbootdev.elasticsearch.examples.springbootelasticsearchexample") | |
public class ElasticSearchConfiguration | |
{ | |
@Value("${elasticsearch.cluster.name}") | |
private String clusterName; | |
@Value("${elasticsearch.host}") | |
private String esHost; | |
@Value("${elasticsearch.port}") | |
private int esPort; | |
@Bean | |
public Client client() throws Exception | |
{ | |
Settings elasticsearchSettings = Settings.builder() | |
.put("client.transport.sniff", true) | |
.put("cluster.name", clusterName).build(); | |
TransportClient client = new PreBuiltTransportClient(elasticsearchSettings); | |
client.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost), esPort)); | |
return client; | |
} | |
@Bean | |
public ElasticsearchOperations elasticsearchTemplate() throws Exception | |
{ | |
return new ElasticsearchTemplate(client()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment