I have the model below and I'm trying to use Hibernate Search for full text search.
I need to search into the "about" field for all the users with the UserRole.PROFESSIONIST role.
This is the model:
@Entity
@Indexed
public class User
{
@Id
@GeneratedValue
Long id;
...
String about;
@IndexedEmbedded
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = UserRole.class, fetch = FetchType.EAGER)
@JoinTable(name = "user_roles", joinColumns = {@JoinColumn(name = "user_id")})
List<UserRole> roles = new ArrayList<>();
}
This is the query:
List results = new ArrayList<>();
org.apache.lucene.search.Query containText = queryBuilder
.keyword()
.onField("about")
.matching(text)
.createQuery();
org.apache.lucene.search.Query isProfessionist = queryBuilder
.keyword()
.onFields("roles")
.matching(UserRole.PROFESSIONIST.toString())
.createQuery();
org.apache.lucene.search.Query query = queryBuilder
.bool()
.must(containText)
.must(isProfessionist)
.createQuery();
org.hibernate.search.jpa.FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, User.class);
results = jpaQuery.getResultList();
When I execute the query I get this error:
org.hibernate.search.exception.SearchException: Unable to find field roles in com.plusimple.core.models.User
Is what I'm trying to do possible? And if yes, what am I doing wrong?