Using Elasticsearch java config client in Spring Batch

415 views Asked by At

I am trying to write a custom writer of elasticsearch which would index data in a spring batch implementation.

I could find the below code as Java config for the elasticsearch.

Anyone who has used this, Can please share where to call this configuration?

@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;
        }
   }
1

There are 1 answers

0
Sabir Khan On

The code that you listed above is basically your implementation details of a - Transport Client element pointing to an instance of Elasticsearch Server i.e. this defines your persistent layer by using Spring Data.

This code will be used by your elasticsearch repositories i.e. repositories that you define by extending - ElasticsearchRepository from Spring Data.

You need to edit @EnableElasticsearchRepositories in code listed by you to actually point to package where you are keeping your repository definitions - no other call would be needed.

When you are going to write/index data to elasticsearch, you work with ElasticsearchRepository interface and you need to define your own repositories and these repositories work with instances as per listing in your code.

Hope it helps !!