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!
The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elastichsearch Documents and easily writing a Repository style data access layer.
The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7. + Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate .
ElasticSearch is well known as a search engine, also working well as document based NoSQL. Spring Data ElasticSearch adds basic Reactive support. Generate a project skeleton from Spring Intializr. Create a Post class to present a document in ElasticSearch, and add a @Document annotation on the class level.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With