Shreesha N
Shreesha N

Reputation: 922

Hibernate search , querying on associations

I am new to Hibernate search , can anyone suggest me how to query on Embedded entities (one to many)

@Indexed
@Entity
public class EventDetails implements Serializable 
{
    @OneToMany( cascade = CascadeType.ALL )
    @IndexedEmbedded
    @JoinColumn( name = "event_id" )
    private Set<Batches> batches;

    --setter and getter--
}

and

@Entity
@Indexed
public class Batches 
{
    @Column( name = "batch" )
    private String batch;

    @ManyToOne
    @ContainedIn(mappedBy="batches")
    private EventDetails eventDetails;

    --setter and getter--
}

Service class

public List<EventDetails> search()
{
fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(getEntityManager());
    QueryBuilder q = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(EventDetails.class).get();
    org.apache.lucene.search.Query luceneQuery = q.keyword().wildcard().onField("").matching(text).createQuery();
    javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, EventDetails.class);
    List<EventDetails> list = jpaQuery.getResultList();
 return list;
}

Now if i have to implement a full text query on "batch" property in batches table , what should i pass as a parameter to the "onField()" method in my service??

Thanks !

Upvotes: 4

Views: 592

Answers (1)

Michal
Michal

Reputation: 2423

Please use batches.batch. You also have to index the batch field using the @Field annotation.

See also hibernate search documentation here and here

You can always use luke to see and query the fields in your index.

Upvotes: 1

Related Questions