Joe Reymann
Joe Reymann

Reputation: 277

Spring Boot Elasticsearch Configuration

I've got a working Spring Boot Elasticsearch Application which uses one of two profiles: application.dev.properties or application.prod.properties. That part works fine. I am having issue with getting the external elasticsearch to read from the application.xxx.properties.

This works:

@Configuration
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ElasticsearchConfiguration {

    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(
                environment.getProperty("elasticsearch.host"), 
                Integer.parseInt(environment.getProperty("elasticsearch.port"))
        );
        client.addTransportAddress(address);        
        return client;
    }

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

but obviously doesn't solve my multi-environment issue.

I've also tried @Value annotations for host and port variables without success.

How can I convert the above to read its values from the application properties file or choose a different @PropertySource file based on whichever profile I want to run?

spring.data.elasticsearch.properties.host = 10.10.1.10
spring.data.elasticsearch.properties.port = 9300

Thanks

Upvotes: 15

Views: 45139

Answers (2)

M. Deinum
M. Deinum

Reputation: 124441

Remove your configuration class and properties.

Add the following dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Just add the spring.data.elasticsearch properties to an application-prod.properties and application-dev.properties and change for the desired environment. This is described in the ElasticSearch section of the Spring Boot guide.

spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300

The value in either file will of course differ (or put the default in the application.properties and simply override with an application-dev.properties.

Spring Boot will based on the spring.profiles.active load the desired properties file.

There is no need to hack around yourself.

Upvotes: 30

ignacio.suay
ignacio.suay

Reputation: 787

I agree with Deinum, if you are using Spring boot it will get the properties from the active profile active.

I have different profiles in my project and this is my elasticsearch configuration:

@Configuration
public class ElasticSearchConfiguration {
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
            String server = clusterNodes.split(":")[0];
            Integer port = Integer.parseInt(clusterNodes.split(":")[1]);
            Settings settings = Settings.settingsBuilder()
                .put("cluster.name", clusterName).build();
            client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port));
            return new ElasticsearchTemplate(client);

    }

Upvotes: 3

Related Questions