Sachin Sachin - 2 months ago 33
Java Question

How to set the annotation properties in spring data Elasticsearch

I've a question in spring data elasticsearch.I would like to know how whether we can set the annotation values of @Document annotation from a properties file or set it dynamically.

For eg :-

@Document(indexName = "myindex",type="mytype")
public class DocumentModel {
......
}


Here,I want to set the values of this annotation from a .properties file or use some setter methods for the same instead of hard coding it. Is there any proper way to do this ? Please help!

Answer

If you're using elasticsearchTemplate, there is a simpler variant, you can do it like this:

IndexQuery indexQuery = new IndexQueryBuilder()
     .withId(docModel.getId())
     .withObject(docModel)
     .withIndex("myindex"+docModel.getUserId()).withTypes(<type_name>).build();

the call to withIndex("...") will override whatever index name you have in the @Document annotation

Comments