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.

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

Hi @Alexis_Cucumel .

The metamodel API is there, 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 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; feel free to submit a PR.

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

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();