15

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

1
  • Why aren't you just using Spring Boot but trying to work around it. Spring boot already loads a property file based on the profile you select. So you are basically making it to hard ... Also Spring Boot already configures ElasticSearch for you, so why are you trying to do it yourself again.. Commented Sep 29, 2015 at 7:05

2 Answers 2

30

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the feedback. Whenever I try to use just the properties without the custom configuration, it always runs the internal in memory Elasticsearch and not my external server.
That shouldn't be the case if you specify the cluster-node properties (this is also explained in the reference guide).
I did read the reference guide on ES. Something else I'm doing must be conflicting with the spring.data.elasticsearch.cluster-nodes line in the application properties. That's why I tried manually creating the configuration.
I created a whole new project using just the config as you recommended and it does start the transport node. So I need to move elements from my existing project over to see what breaks it. Thanks again!
looks like spring.data.elasticsearch.cluster-name is deprecated :-/ docs.spring.io/spring-data/elasticsearch/docs/current/reference/…
|
3

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);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.