[hibernate-commits] Hibernate SVN: r11519 - in trunk/HibernateExt/search/src: test/org/hibernate/search/test/query and 1 other directory.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Mon May 14 01:27:52 EDT 2007
Author: epbernard
Date: 2007-05-14 01:27:52 -0400 (Mon, 14 May 2007)
New Revision: 11519
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
Log:
HSEARCH-43 expose resultSize, compute resultSize even if list() has not been executed
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java 2007-05-13 22:05:24 UTC (rev 11518)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java 2007-05-14 05:27:52 UTC (rev 11519)
@@ -27,7 +27,10 @@
/**
* Returns the number of hits for this search
*
- * @return The number of hits for this search
+ * Caution:
+ * The number of results might be slightly different from
+ * <code>list().size()</code> because list() if the index is
+ * not in sync with the database at the time of query.
*/
int resultSize();
}
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-13 22:05:24 UTC (rev 11518)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-14 05:27:52 UTC (rev 11519)
@@ -53,7 +53,7 @@
private Set<Class> classesAndSubclasses;
private Integer firstResult;
private Integer maxResults;
- private int resultSize;
+ private Integer resultSize;
private Sort sort;
/**
@@ -156,7 +156,7 @@
if (searcher == null) return new ArrayList(0);
Hits hits;
try {
- hits = getHits( searcher );;
+ hits = getHits( searcher );
setResultSize( hits );
int first = first();
int max = max( first, hits );
@@ -317,8 +317,35 @@
resultSize = hits.length();
}
- //FIXME does it make sense
+
public int resultSize() {
+ if (resultSize == null) {
+ //get result size without object initialization
+ SearchFactory searchFactory = ContextHelper.getSearchFactoryBySFI( session );
+ Searcher searcher = buildSearcher( searchFactory );
+ if (searcher == null) {
+ resultSize = 0;
+ }
+ else {
+ Hits hits;
+ try {
+ hits = getHits( searcher );
+ setResultSize( hits );
+ }
+ catch (IOException e) {
+ throw new HibernateException( "Unable to query Lucene index", e );
+ }
+ finally {
+ //searcher cannot be null
+ try {
+ searcher.close();
+ }
+ catch (IOException e) {
+ log.warn( "Unable to properly close searcher during lucene query: " + getQueryString(), e );
+ }
+ }
+ }
+ }
return this.resultSize;
}
Modified: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-05-13 22:05:24 UTC (rev 11518)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQueryTest.java 2007-05-14 05:27:52 UTC (rev 11519)
@@ -7,9 +7,11 @@
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
+import org.hibernate.search.query.FullTextQuery;
import org.hibernate.Transaction;
import org.hibernate.Hibernate;
import org.hibernate.ScrollableResults;
+import org.hibernate.stat.Statistics;
import org.apache.lucene.search.Query;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.StopAnalyzer;
@@ -23,18 +25,18 @@
public void testList() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
- Clock clock = new Clock(1, "Seiko");
+ Clock clock = new Clock( 1, "Seiko" );
s.save( clock );
- clock = new Clock( 2, "Festina");
+ clock = new Clock( 2, "Festina" );
s.save( clock );
- Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");
- s.save(book);
- book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
- s.save(book);
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ book = new Book( 2, "La gloire de mon père", "Les deboires de mon père en vélo" );
+ s.save( book );
tx.commit();
s.clear();
tx = s.beginTransaction();
- QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
Query query = parser.parse( "summary:noword" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
@@ -48,42 +50,78 @@
assertNotNull( result );
assertEquals( "Query with explicit class filter", 2, result.size() );
- query = parser.parse( "summary:Festina Or brand:Seiko" );
+ query = parser.parse( "summary:Festina Or brand:Seiko" );
hibQuery = s.createFullTextQuery( query );
result = hibQuery.list();
assertNotNull( result );
assertEquals( "Query with no class filter", 2, result.size() );
- for (Object element : result) {
+ for (Object element : result) {
assertTrue( Hibernate.isInitialized( element ) );
s.delete( element );
}
- s.flush();
- query = parser.parse( "summary:Festina Or brand:Seiko" );
+ s.flush();
+ query = parser.parse( "summary:Festina Or brand:Seiko" );
hibQuery = s.createFullTextQuery( query );
result = hibQuery.list();
assertNotNull( result );
assertEquals( "Query with delete objects", 0, result.size() );
- for (Object element : s.createQuery( "from java.lang.Object" ).list() ) s.delete( element );
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
tx.commit();
s.close();
}
+ public void testResultSize() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+ Clock clock = new Clock( 1, "Seiko" );
+ s.save( clock );
+ clock = new Clock( 2, "Festina" );
+ s.save( clock );
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ book = new Book( 2, "La gloire de mon père", "Les deboires de mon père en vélo" );
+ s.save( book );
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
+
+ Query query = parser.parse( "summary:Festina Or brand:Seiko" );
+ Statistics stats = s.getSessionFactory().getStatistics();
+ stats.clear();
+ boolean enabled = stats.isStatisticsEnabled();
+ if (!enabled) stats.setStatisticsEnabled( true );
+ FullTextQuery hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
+ assertEquals( "Exection of resultSize without actual results", 2, hibQuery.resultSize() );
+ assertEquals( "No entity should be loaded", 0, stats.getEntityLoadCount() );
+
+ query = parser.parse( "summary:Festina Or brand:Seiko" );
+ hibQuery = s.createFullTextQuery( query );
+ List result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "2 entities should be loaded", 2, stats.getEntityLoadCount() );
+ if (!enabled) stats.setStatisticsEnabled( false );
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
public void testFirstMax() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
- Clock clock = new Clock(1, "Seiko");
+ Clock clock = new Clock( 1, "Seiko" );
s.save( clock );
- clock = new Clock( 2, "Festina");
+ clock = new Clock( 2, "Festina" );
s.save( clock );
- Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");
- s.save(book);
- book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
- s.save(book);
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ book = new Book( 2, "La gloire de mon père", "Les deboires de mon père en vélo" );
+ s.save( book );
tx.commit();
s.clear();
tx = s.beginTransaction();
- QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
Query query = parser.parse( "summary:Festina Or brand:Seiko" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
@@ -109,8 +147,8 @@
result = hibQuery.list();
assertNotNull( result );
assertEquals( "first result out of limit", 0, result.size() );
-
- for (Object element : s.createQuery( "from java.lang.Object" ).list() ) s.delete( element );
+
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
tx.commit();
s.close();
}
@@ -118,18 +156,18 @@
public void testIterator() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
- Clock clock = new Clock(1, "Seiko");
+ Clock clock = new Clock( 1, "Seiko" );
s.save( clock );
- clock = new Clock( 2, "Festina");
+ clock = new Clock( 2, "Festina" );
s.save( clock );
- Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");
- s.save(book);
- book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
- s.save(book);
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ book = new Book( 2, "La gloire de mon père", "Les deboires de mon père en vélo" );
+ s.save( book );
tx.commit();//post commit events for lucene
s.clear();
tx = s.beginTransaction();
- QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
Query query = parser.parse( "summary:noword" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
@@ -148,15 +186,15 @@
}
assertEquals( 2, index );
- s.flush();
+ s.flush();
- query = parser.parse( "summary:Festina Or brand:Seiko" );
- hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
- result = hibQuery.iterate();
- assertNotNull( result );
- assertFalse( result.hasNext() );
+ query = parser.parse( "summary:Festina Or brand:Seiko" );
+ hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
+ result = hibQuery.iterate();
+ assertNotNull( result );
+ assertFalse( result.hasNext() );
- for (Object element : s.createQuery( "from java.lang.Object" ).list() ) s.delete( element );
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
tx.commit();
s.close();
}
@@ -164,31 +202,31 @@
public void testScrollableResultSet() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
- Clock clock = new Clock(1, "Seiko");
+ Clock clock = new Clock( 1, "Seiko" );
s.save( clock );
- clock = new Clock( 2, "Festina");
+ clock = new Clock( 2, "Festina" );
s.save( clock );
- Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");
- s.save(book);
- book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
- s.save(book);
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ book = new Book( 2, "La gloire de mon père", "Les deboires de mon père en vélo" );
+ s.save( book );
tx.commit();//post commit events for lucene
s.clear();
tx = s.beginTransaction();
- QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
Query query = parser.parse( "summary:noword" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
ScrollableResults result = hibQuery.scroll();
assertNotNull( result );
- assertEquals(-1, result.getRowNumber() );
- assertEquals(false, result.next() );
+ assertEquals( -1, result.getRowNumber() );
+ assertEquals( false, result.next() );
result.close();
query = parser.parse( "summary:Festina Or brand:Seiko" );
hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
result = hibQuery.scroll();
- assertEquals(0, result.getRowNumber() );
+ assertEquals( 0, result.getRowNumber() );
result.beforeFirst();
assertEquals( true, result.next() );
assertTrue( result.isFirst() );
@@ -199,7 +237,7 @@
while ( result.next() ) {
s.delete( result.get()[0] );
}
- for (Object element : s.createQuery( "from java.lang.Object" ).list() ) s.delete( element );
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
tx.commit();
s.close();
}
@@ -207,23 +245,23 @@
public void testMultipleEntityPerIndex() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
- Clock clock = new Clock(1, "Seiko");
+ Clock clock = new Clock( 1, "Seiko" );
s.save( clock );
- Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");
- s.save(book);
- AlternateBook alternateBook = new AlternateBook(1, "La chute de la petite reine a travers les yeux de Festina");
- s.save(alternateBook);
+ Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
+ s.save( book );
+ AlternateBook alternateBook = new AlternateBook( 1, "La chute de la petite reine a travers les yeux de Festina" );
+ s.save( alternateBook );
tx.commit();
s.clear();
tx = s.beginTransaction();
- QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ QueryParser parser = new QueryParser( "title", new StopAnalyzer() );
Query query = parser.parse( "summary:Festina" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
List result = hibQuery.list();
assertNotNull( result );
assertEquals( "Query with explicit class filter", 1, result.size() );
-
+
query = parser.parse( "summary:Festina" );
hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
Iterator it = hibQuery.iterate();
@@ -251,11 +289,11 @@
result = hibQuery.list();
assertNotNull( result );
assertEquals( "Query with no class filter", 2, result.size() );
- for (Object element : result) {
+ for (Object element : result) {
assertTrue( Hibernate.isInitialized( element ) );
s.delete( element );
}
- for (Object element : s.createQuery( "from java.lang.Object" ).list() ) s.delete( element );
+ for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
tx.commit();
s.close();
}
More information about the hibernate-commits
mailing list