# ElasticsearchRepository with dynamic elastic index names spring boot

**URL:** <https://discuss.elastic.co/t/elasticsearchrepository-with-dynamic-elastic-index-names-spring-boot/222089>\
**Category:** Elasticsearch\
**Created:** [March 4, 2020, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearchrepository-with-dynamic-elastic-index-names-spring-boot/222089 "2020-03-04T13:44:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![srkavay](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@srkavay](https://discuss.elastic.co/u/srkavay)\
**Post date:** [March 4, 2020, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearchrepository-with-dynamic-elastic-index-names-spring-boot/222089/1 "2020-03-04T13:44:25Z")

</div>

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 indexs created in dynamic way and pass the index name and type as parameter ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 1, 2020, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearchrepository-with-dynamic-elastic-index-names-spring-boot/222089/2 "2020-04-01T13:44:26Z")

</div>

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