0

I am using Hibernate Search to apply full text Search on a field in my database table. I am using it like this:

FullTextSession fts=Search.createFullTextSession(session);
QueryBuilder qb2 = fts.getSearchFactory().buildQueryBuilder()
                   .forEntity(DocumentVersion.class).get();
org.apache.lucene.search.Query searchQuery2 = qb2.keyword()
                                              .onField("content")
                                              .matching(search)
                                              .createQuery();

FullTextQuery fullTextQuery2 = fts.createFullTextQuery(searchQuery2,DocumentVersion.class);
fullTextQuery2.setMaxResults(100);
List res=fullTextQuery2 .list();

The last line is giving me the DocumentVersion objects arranged according to relevance of to the query. But when I use criteria to get only the top 100 most relevant documentId field in DocumentVersion, it is always returning the documentId arranged in ascending order which shows that it not taking care of relevance or ranking.

The code for this is:

FullTextSession fts=Search.createFullTextSession(session);
QueryBuilder qb2 =fts.getSearchFactory().buildQueryBuilder().forEntity(DocumentVersion.class).get();
    org.apache.lucene.search.Query searchQuery2 = qb2.keyword()
            .onField("content")
            .matching(search)
            .createQuery();

FullTextQuery fullTextQuery2 = fts.createFullTextQuery(searchQuery2, DocumentVersion.class);
org.hibernate.Criteria crquery = session.createCriteria(DocumentVersion.class);
crquery.setProjection(Projections.property("documentId"));
crquery.setMaxResults(100);
fullTextQuery2.setCriteriaQuery(crquery);
List res=crquery.list();

I have googled a lot to find a way to select top 100 most relevant or highest ranked documentIds as explained above but cannot find a way to do that. Some one please help me! The DocumentVersion class is like this:

@Entity
@Indexed
@Table(name = "t_document_version")
public class DocumentVersion {

  private long id;
  private long documentId;

  @Field(index=Index.TOKENIZED, store=Store.NO, name="content")
  private String content;
  private Timestamp tstamp;
   ...
}
Umair
  • 81
  • 1
  • 1
  • 8

1 Answers1

0

You cannot mix Hibernate Search queries with Hibernate ORM Criteria queries (org.hibernate.Criteria). Only setting the fetch style is supported via the ORM Criteria query. If you want to use Hibernate Search Projection have a look at http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#projections

You have to do something like:

fullTextQuery2.setProjection("documentId");
Hardy
  • 18,659
  • 3
  • 49
  • 65