# Get ES index name for Entity at runtime

**URL:** <https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037>\
**Category:** Hibernate Search\
**Created:** [April 17, 2020, 5:46pm UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037 "2020-04-17T17:46:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugheba](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/hugheba/32/1028_2.png) [@hugheba](https://discourse.hibernate.org/u/hugheba)\
**Post date:** [April 17, 2020, 5:46pm UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/1 "2020-04-17T17:46:03Z")

</div>

Is there a way to get the Elasticsearch index for an entity at runtime?

I’m using SpringDataJpa, not sure if there is a bean where the index name for an entity is stored.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [April 20, 2020, 7:09am UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/2 "2020-04-20T07:09:46Z")

</div>

> [@hugheba](#):
>
> Is there a way to get the Elasticsearch index for an entity at runtime?

I assume you meant Elasticsearch index _name_.

There is no direct API to retrieve that information: Hibernate Search 5 doesn’t expose it through its metadata lookup API, and Hibernate Search 6 doesn’t have a metadata lookup API yet ([HSEARCH-3589](https://hibernate.atlassian.net/browse/HSEARCH-3589)).

However, here are the rules that Hibernate Search uses to derive the Elasticsearch index name for the configuration you use. That should allow you to infer the index name for a given entity.

In Hibernate Search 5:

- Hibernate Search’s internal index name is the name you put in `@Indexed(index = ...)`. By default it is the fully qualified name of your entity class.
- The Elasticsearch index name is the Hibernate Search index name, lowercased.
- For example:
  - `com.acme.MyEntity`, annotated with `@Indexed()` (no parameter) would be named `com.acme.myentity` in Elasticsearch.
  - `com.acme.MyEntity2`, annotated with `@Indexed(index = "INDEX2")` would be named `index2` in Elasticsearch.

In Hibernate Search 6, we don’t use the index name directly to talk to Elasticsearch: it could be anything, really. We use one alias for reads and one alias for writes:

- Hibernate Search’s internal index name is the name you put in `@Indexed(index = ...)`. By default it is the **JPA entity name** , specified with `@Entity(name = ...)` and which defaults to the simple (unqualified) name of your entity class.
- The read alias is the Hibernate Search index name, lowercased, suffixed with `_read`.
- The write alias is similar, except the suffix is `_write` instead of `_read`.
- For example:
  - `com.acme.MyEntity`, annotated with `@Entity()` (no parameter) and `@Indexed()` (no parameter) would have `myentity_read` for a read alias in Elasticsearch.
  - `com.acme.MyEntity2`, annotated with `@Entity(name = "ENTITY2")` and `@Indexed()` (no parameter) would have `entity2_read` for a read alias in Elasticsearch.
  - `com.acme.MyEntity3`, annotated with `@Entity(name = "ENTITY3")` and `@Indexed(index = "INDEX3")` (no parameter) would have `index3_read` for a read alias in Elasticsearch.

In Hibernate Search 6 you can customize the read and write aliases by setting a custom [index layout strategy](https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#backend-elasticsearch-indexlayout).

---

<div class="post-metadata">

**Author:** ![hugheba](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/hugheba/32/1028_2.png) [@hugheba](https://discourse.hibernate.org/u/hugheba)\
**Post date:** [April 20, 2020, 11:05am UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/3 "2020-04-20T11:05:21Z")

</div>

Thanks @yrodiere. Good information. I’ll try to develop around those requirements.

---

<div class="post-metadata">

**Author:** ![Alexis\_Cucumel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/alexis_cucumel/32/687_2.png) [@Alexis\_Cucumel](https://discourse.hibernate.org/u/Alexis_Cucumel)\
**Post date:** [March 29, 2021, 10:09am UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/4 "2021-03-29T10:09:50Z")

</div>

Hi @yrodiere, any plans to build that API one day ?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [March 30, 2021, 10:30am UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/5 "2021-03-30T10:30:07Z")

</div>

Hi @Alexis_Cucumel .

The metamodel API [is there](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-mapping-inspect), but it is currently technology-agnostic, so you can see the fields and their type, but not something specific to Elasticsearch such as the Elasticsearch index names.

The best you can get is the Hibernate Search index name, which depending on your [index layout strategy](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#backend-elasticsearch-indexlayout) may or may not be the same as the Elasticsearch index names.

There are no plans to expose the Elasticsearch index names through the metamodel API at the moment, but this would be rather easy to implement. I opened [HSEARCH-4204](https://hibernate.atlassian.net/browse/HSEARCH-4204); feel free to submit a PR.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [June 17, 2021, 3:28pm UTC](https://discourse.hibernate.org/t/get-es-index-name-for-entity-at-runtime/4037/6 "2021-06-17T15:28:49Z")

</div>

FYI, index names can now be retrieved from the metamodel in Hibernate Search 6.0.4.Final: [Hibernate Search 6.0.4.Final: Reference Documentation](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_retrieving_the_read_and_write_index_names)

```java
SearchMapping mapping = Search.mapping( entityManagerFactory ); 
IndexManager indexManager = mapping.indexManager( "Book" ); 
ElasticsearchIndexManager esIndexManager = indexManager.unwrap( ElasticsearchIndexManager.class ); 
ElasticsearchIndexDescriptor descriptor = esIndexManager.descriptor();
String readName = descriptor.readName();
String writeName = descriptor.writeName();

```
