7

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.

Nitin Arora
  • 2,650
  • 1
  • 26
  • 27

1 Answers1

11

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.

Jama A.
  • 15,680
  • 10
  • 55
  • 88
  • Code above does not create a Transport Client instead it creates a Node client and is Java Config equivalent of following xml configuration – Nitin Arora Nov 21 '14 at 09:33
  • Please see updated with TransportClient where you can call ElasticsearchTemplate with that constructor – Jama A. Nov 21 '14 at 10:23
  • 2
    also you can pass Settings settings = ImmutableSettings.settingsBuilder().put("","").build() to TransportClient's constructor to configure it... – Jama A. Nov 21 '14 at 10:25
  • Thanks Jama. That works. Is there a way to create Transport client using `nodeBuilder()`? – Nitin Arora Nov 21 '14 at 21:01
  • They are two different things. NodeBuilder creates a client for the embedded version of ElasticSearch. TransportClient is a client for externally running ES clusters. – EricGreg Aug 26 '16 at 22:51