How to index a inherited field in Hibernate-search?

烈酒焚心 提交于 2021-01-29 09:35:51

问题


I am working in a java jpa Hibernate-search application, I know Hibernate-search index automatically every @Id annotation in an entity. The problem is that I have a "master domain" class with contains the @Id annotation, and then I have another class with inherit "master domain", then seems to be the Hibernate search is not recognizing the @Id field inherited.

this is my master domain class.

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class MasterDomain<Key extends Object> implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;
}

I have a class "Language" which inherits this class:

@Indexed
@Entity
public class Language extends MasterDomain<Long>{

    @Field
    private String name;
}

Finally I have another class called "LanguageRelation" which is related with Language class. It looks like:

@Indexed
@Entity
public class LanguageRelation extends MasterDomain<Long>{

   @IndexedEmbedded
   private Language language;
}

So, when I build a lucene query to search LanguageRelation entities, I am able to search by language names like this:

queryBuilder.keyword().onField("language.name").matching(languageName).createQuery()

But I am not able to search by language id, like this:

queryBuilder.keyword().onField("language.id").matching(languageId).createQuery()

Previous query returns 0 results, as you can see, it seems to be Hibernate search is not recognizing the @Id inherited from MasterDomain, any suggestion?

UPDATE 1 => I forgot to tell MasterDomain class is in separated module from which I am trying to execute the Lucene Query. Maybe this could get into the problem?

UPDATE 2 This is the full code of how I am trying to build my Lucene query.

FullTextEntityManager fullTextEntityManager
                = Search.getFullTextEntityManager(entityManager);

org.hibernate.search.query.dsl.QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(LanguageRelation.class)
                .get();

Long languageId = 29L;
org.apache.lucene.search.Query query = queryBuilder.keyword().onField("language.id").matching(languageId).createQuery();

org.hibernate.search.jpa.FullTextQuery fullTextQuery
                = fullTextEntityManager.createFullTextQuery(query, LanguageRelation.class);
List<LanguageRelation> resultList = fullTextQuery.getResultList();

回答1:


I think the problem is simply that the ID isn't embedded by default.

Try replacing this:

@IndexedEmbedded

With this:

@IndexedEmbedded(includeEmbeddedObjectId = true)

Then reindex your data, and run your query again.



来源:https://stackoverflow.com/questions/58489569/how-to-index-a-inherited-field-in-hibernate-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!