2

I am trying using hibernate full text by following this link: hibernate/search/4.1/reference/en-US/html/getting-started

Basically, it works, but I want to know how to get total count while I execute a full text query,then I can tell user how many results and how many pages would be in such a query.

Here is the code(Using JPA to create and execute a search):

EntityManager em = entityManagerFactory.createEntityManager();
FullTextEntityManager fullTextEntityManager = 
org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
em.getTransaction().begin();

// create native Lucene query unsing the query DSL
// alternatively you can write the Lucene query using the Lucene query parser
// or the Lucene programmatic API. The Hibernate Search DSL is recommended though
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
    .buildQueryBuilder().forEntity( Book.class ).get();
org.apache.lucene.search.Query query = qb
  .keyword()
  .onFields("title", "subtitle", "authors.name", "publicationDate")
  .matching("Java rocks!")
  .createQuery();

// wrap Lucene query in a javax.persistence.Query
javax.persistence.Query persistenceQuery = 
    fullTextEntityManager.createFullTextQuery(query, Book.class);
 persistenceQuery.setFirstResult((page - 1) * PAGECOUNT);
    persistenceQuery.setMaxResults(PAGECOUNT);
// execute search
List result = persistenceQuery.getResultList();

em.getTransaction().commit();
em.close();

In SQL, I can use select count(*) from something, but here I don't know how to do that. I want to just fetch one page of data every time and use another API to get total count.

Sanne
  • 6,027
  • 19
  • 34
Tom
  • 2,857
  • 9
  • 46
  • 59

4 Answers4

11
query.getResultSize(); //return the total number of matching ... regardless of pagination
Bart
  • 19,692
  • 7
  • 68
  • 77
skrymir1
  • 557
  • 6
  • 5
  • What's the type of your "query"? None of javax.persistence.Query and org.apache.lucene.search.Query has such a method:getResaultSize – Tom Jul 30 '12 at 03:02
  • 1
    FullTextQuery as returned by fullTextEntityManager.createFullTextQuery(..) above. – skrymir1 Aug 01 '12 at 10:50
  • A bit short for a an answer. Perhaps move the comment out of the code? – nalply Oct 10 '12 at 20:32
3

I'm not sure if there is such a way when using the Hibernate full text search.

If you want to know the total number of results then you have to perform the full query. After you have the full count you can set your page limiter and perform it again.

javax.persistence.Query persistenceQuery = 
    fullTextEntityManager.createFullTextQuery(query, Book.class);

int count = persistenceQuery.getResultList().size();

persistenceQuery = 
    fullTextEntityManager.createFullTextQuery(query, Book.class);
persistenceQuery.setFirstResult((page - 1) * PAGECOUNT);
persistenceQuery.setMaxResults(PAGECOUNT);
List result = persistenceQuery.getResultList();
magomi
  • 6,599
  • 5
  • 31
  • 38
  • Sorry, I didn't describe my question clearly, actually I added two line code:javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(query, PSubject.class); persistenceQuery.setFirstResult((page - 1) * PAGECOUNT); persistenceQuery.setMaxResults(PAGECOUNT); So I want to know another way to count total, and just get one page actual data every time. – Tom Mar 26 '12 at 08:44
  • Changed my answer according to your changed requirement. Now the code looks quite dirty ;) Its also not very performant because you have to execute the query twice. Hope, someone comes up with a better answer. – magomi Mar 26 '12 at 09:45
1

For Hibernate(maybe for JPA)

public interface FullTextQuery extends Query

in other words, you need use

org.hibernate.search.FullTextQuery query =  fullTextEntityManager.createFullTextQuery(query, Book.class);

instead of

org.hibernate.Query query =  fullTextEntityManager.createFullTextQuery(query, Book.class);

and method getResultSize() will be available

rdm
  • 330
  • 1
  • 4
  • 18
0

When using directly Lucene/Solr, I usually use a hack* by searching for *:*, setting it to return the least possible results BUT that does return the total result count for "everything", and I proceed to extract it. Basically it's the same as the SELECT count(*) FROM whatever :P

*I say hack because I'm not sure if it's supposed to be that way or not, but it works for me...

Rafael Lins
  • 458
  • 7
  • 12