Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 500
Dorian Avatar asked Oct 11 '15 20:10

Dorian


People also ask

Does spring data support Elasticsearch?

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.

Why ElasticsearchTemplate is deprecated?

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 .

Is Elasticsearch reactive?

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.


1 Answers

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

like image 56
Bruno Avatar answered Sep 19 '22 08:09

Bruno