Spring Data Elasticsearch @Document indexName defined at runtime

Dorian picture Dorian · Oct 11, 2015 · Viewed 7.3k times · Source

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!

Answer

Bruno picture Bruno · Nov 4, 2015

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