0

I have several indices where I save products:

product-example1
product-example2
product-example3
product-example4 
product-example5

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

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

@Id
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 ?

@Autowired
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 indexes that I created in dynamic way as described above, and pass the index name and type as parameter ?

Please help! I'm stuck.

yof19690
  • 1
  • 2
  • Have a look at this https://stackoverflow.com/questions/28466200/spring-data-elasticsearch-multiple-index-with-same-document – SandOfTime Mar 05 '20 at 08:45
  • 1
    I checked this but it created a class for each index and I don't want that. I want to create a repository for each index without creating a specific class for each one? @SandOfTime – yof19690 Mar 05 '20 at 09:36
  • 1
    @SandOfTime do you have any other suggestion? – yof19690 Mar 05 '20 at 11:55

0 Answers0