Get ES index name for Entity at runtime

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.

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).

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.

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