4

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 !

Shreesha N
  • 892
  • 2
  • 8
  • 20
  • I already tried that , but its not returning any result even if the "batch" value matches . Thanks Michal – Shreesha N Jan 05 '16 at 10:36
  • Have a look with Luke at the index. I am sure you will see the batches.batch field there. If you do not find anything the problem might be with analysers used for indexing and quering. Could you post what is in the batch field, what is your analyser and what is your search text? – Michal Jan 05 '16 at 10:40
  • batch value is say "BTech" and if i pass "BTech" as a parameter its returning an empty list !! **edit** - i am using StandardAnalyzer to index the data – Shreesha N Jan 05 '16 at 10:54

1 Answers1

1

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.

Michal
  • 2,353
  • 1
  • 15
  • 18
  • i have added @Field(store=Store.YES,analyze=Analyze.YES,index=Index.YES) on batch attribute , but no use – Shreesha N Jan 05 '16 at 14:24
  • Thanks michal , i did as you told ! The mistake was i was passing data to the service which was in upper case , but data will be indexed in lower case ! – Shreesha N Jan 05 '16 at 14:45