Search code examples
lucenehibernate-searchenumerated

How to index and search an ENUM type with Hibernate-Search


I am working in a Spring, Jpa, Hibernate-Search, Lucene application. I want to index a JPA entity which has a ENUM property and use Lucene to search on that. Every element in my ENUM has a int value.

I put the @Field annotation on the property, but is not working. In the database the field is being stored as an Integer.

@Field
@Enumerated
private STATUS status = STATUS.FIRST;

Now I want to use Lucene for search using this field

//lucene query to search by that enum field 
queryStatus.should(queryBuilder.keyword().onField("status").matching(1).createQuery());

I cannot change the enum storage to string type because it has other queries working and using this enum propoerty as an integer and I do not know how to index and use it.


Solution

  • Actually I was able to solve the issue, when searching instead of use the int value, I convert it to the explicit enum.

    queryStatus.should(queryBuilder.keyword().onField("status").matching(MyClass.STATUS.fromValue(status)).createQuery());
    

    That solved my issue.