Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data ElasticSearch TransportClient Java Config

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.

like image 816
Nitin Arora Avatar asked Nov 21 '14 02:11

Nitin Arora


1 Answers

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.

like image 131
Jama A. Avatar answered Nov 15 '22 10:11

Jama A.