Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Last active December 26, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurangat/eac7eafa46c90aafbf3c754f666053c1 to your computer and use it in GitHub Desktop.
Save chathurangat/eac7eafa46c90aafbf3c754f666053c1 to your computer and use it in GitHub Desktop.
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