Spring Data ElasticSearch TransportClient Java Config

Nitin Arora picture Nitin Arora · Nov 21, 2014 · Viewed 11.2k times · Source

Does anybody know what's Java Config equivalent of :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300"
/>
</beans>

I specifically want to use nodeBuilder() to do it.

Answer

&#213;zbek picture Õzbek · Nov 21, 2014

Take a look into Spring Data documentations for ElasticSearch:

   @Configuration
   @EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
        static class Config {

        @Value("${esearch.port}") int port;
        @Value("${esearch.host}") String hostname;

        @Bean
        public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
         }

        @Bean
        public Client client(){
            TransportClient client= new TransportClient();
            TransportAddress address = new InetSocketTransportAddress(hostname, port); 
            client.addTransportAddress(address);
            return client;
        }
   }

Elasticsearch Repositories 2.1.2 Annotation based configuration

The Spring Data Elasticsearch repositories support cannot only be activated through an XML namespace but also using an annotation through JavaConfig.