Migration from Hibernate Search 5 to 6.1

Hi all,

In Hibernate 5 there was a field called “_hibernate_class” which was automatically added to the Lucene document, but I don’t see this field in Hibernate Search 6.1, Is there any way to be added?

I used this fielt to look for some specific indexed entity because these entities has a superclass from they are descendants. The only way of achieving the same result it to pass less or more class to SearchSession.search(...) method but this would break my backward compatibility.

Thanks in advance.

Hi,

No, we don’t plan to add this field back. It was supposed to be an implementation detail :slight_smile:

Are you trying to filter by type? If so, you can just add the field yourself:

@javax.persistence.Transient
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.NO) // The content of this field never changes
@KeywordField(name = "_hibernate_class")
public String getHibernateClass() {
    return Hibernate.getClass(this).getName();
}

As to adding a built-in way to filter by type, that will happen eventually, but probably not with just a hardcoded field: [HSEARCH-3434] - Hibernate JIRA

I’ve tried and the field “_hibernate_search” appears, but it’s empty :confused:

What do you mean, empty? Indexing a field does not store it automatically.

I’d suggest you test your search query instead of using Luke, because the Lucene index structure is more complex than it might seem. If your search query doesn’t work, please show it here.

Also, if you just wanted to project on the entity class, not to filter by entity class, you don’t need the _hibernate_class field. You can just project to an entity reference: Hibernate Search 6.1.5.Final: Reference Documentation. That reference includes the entity ID and, more importantly in your case, the entity class.

The value was missing because I forgot to add the “projectable = Projectable.YES”.

Thanks for the tip!

Ok, then if you just need this for projections, you definitely shouldn’t be defining your own field. You can simply use a projection to the entity reference:

List<EntityReference> hits = searchSession.search( Book.class )
        .select( f -> f.entityReference() )
        .where( f -> f.matchAll() )
        .fetchHits( 20 );

Or if you want to project to the class and ignore the ID:

List<Class<?>> hits = searchSession.search( Book.class )
        .select( f -> f.composite( EntityReference::type, f.entityReference() ) )
        .where( f -> f.matchAll() )
        .fetchHits( 20 );

That’s mentioned in the migration guide, by the way, in this section (see the OBJECT_CLASS constant): Hibernate Search 6.0.9.Final: Migration Guide from 5.11

Yes, I already use projections to get the document id and the score.

Thanks.