ElasticsearchRepository with dynamic elastic index names spring boot

I have several indices where I save products:


I have the a document in elastic search that has the same structure and it can used for different indices:

@org.springframework.data.elasticsearch.annotations.Document(indexName = "", type = "", createIndex = false)
public class ProductDocument {

private String id;
private String title;
private String seller;
private String releaseDate;

So basically I want to use same settings, same mappings same search service.
So I made indexName and type parametric in spring boot java, instead of creating 5 classes extending ProductDocument ?

private final ElasticsearchTemplate elasticsearchTemplate;	

this.elasticsearchTemplate.createIndex("product-example1", loadFile("/files/settings.json"));
this.elasticsearchTemplate.putMapping("product-example1", "product-type1",   loadFile("/files/mapping.json"));

this.elasticsearchTemplate.createIndex("product-example2", loadFile("/files/settings.json"));
this.elasticsearchTemplate.putMapping("product-example2", "product-type2",   loadFile("/files/mapping.json"));   


Now I want to create a ProductRepository but I don't have a class with defined index name. If I use generic class:

>     public interface DocumentRepository extends ElasticsearchRepository<ProductDocument, String> {
>     }

I get the error which is totally understable cause I created the index names in dynamic way:
lang.IllegalArgumentException: Unknown indexName. Make sure the indexName is defined. e.g @ProductDocument(indexName="foo")

So is it possible somehow to create repository for indexs created in dynamic way and pass the index name and type as parameter ?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.