Search code examples
spring-mvcspring-bootspring-dataspring-data-elasticsearch

Spring Data Elasticsearch @Document indexName defined at runtime


Is it possible to specify dynamically (at runtime) the indexName for each @Document, for example, via a configuration file? Or is it possible to make @Document Spring environment (dev, prod) dependant?

Thank you!


Solution

  • The @Document annotation does not permit to pass the indexname in parameter directly. However I found a work around.

    In my configuration class I created a Bean returning a string. In this string I injected the name of the index with @Value :

    @Value("${etrali.indexname}")
    private String indexName;
    
    @Bean
    public String indexName(){
        return indexName;
    }
    

    Afterward it is possible to inject the index into the @Documentation annotation like this :

    @Document(indexName="#{@indexName}",type = "syslog_watcher")
    

    It works for me, I hope it will help you.

    Best regards