Hibernate SVN: r11521 - trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/builtin.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-05-14 12:07:48 -0400 (Mon, 14 May 2007)
New Revision: 11521
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/builtin/BooleanBridge.java
Log:
HSEARCH-50 BooleanBridge should return null and not NPE on null objects
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/builtin/BooleanBridge.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/builtin/BooleanBridge.java 2007-05-14 05:59:55 UTC (rev 11520)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/builtin/BooleanBridge.java 2007-05-14 16:07:48 UTC (rev 11521)
@@ -16,8 +16,9 @@
}
public String objectToString(Object object) {
- Boolean b = (Boolean) object;
- return b.toString();
+ return object == null ?
+ null :
+ ( (Boolean) object ).toString();
}
}
16 years, 11 months
Hibernate SVN: r11520 - trunk/HibernateExt/search/src/java/org/hibernate/search/util.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-05-14 01:59:55 -0400 (Mon, 14 May 2007)
New Revision: 11520
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/util/ContextHelper.java
Log:
HSEARCH-36 better exception message
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/util/ContextHelper.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/util/ContextHelper.java 2007-05-14 05:27:52 UTC (rev 11519)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/util/ContextHelper.java 2007-05-14 05:59:55 UTC (rev 11520)
@@ -28,7 +28,9 @@
break;
}
}
- if ( listener == null ) throw new HibernateException( "Lucene event listener not initialized" );
+ if ( listener == null ) throw new HibernateException(
+ "Hibernate Search Event listeners not configured, please check the reference documentation and the " +
+ "application's hibernate.cfg.xml" );
return listener.getSearchFactory();
}
}
16 years, 11 months
Hibernate SVN: r11519 - in trunk/HibernateExt/search/src: test/org/hibernate/search/test/query and 1 other directory.
by hibernate-commits@lists.jboss.org
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();
}
16 years, 11 months
Hibernate SVN: r11518 - in trunk/HibernateExt/search/src: java/org/hibernate/search/impl and 2 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-05-13 18:05:24 -0400 (Sun, 13 May 2007)
New Revision: 11518
Added:
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java
trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextSession.java
trunk/HibernateExt/search/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
trunk/HibernateExt/search/src/java/org/hibernate/search/query/IteratorImpl.java
Log:
HSEARCH-4 Support for Lucene sort (Hardy Ferentschik)
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextSession.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextSession.java 2007-05-13 21:21:11 UTC (rev 11517)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/FullTextSession.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -3,6 +3,7 @@
import org.hibernate.classic.Session;
import org.hibernate.Query;
+import org.hibernate.search.query.FullTextQuery;
/**
* Extends the Hibernate {@link Session} with Full text search and indexing capabilities
@@ -15,7 +16,7 @@
* of type <code>entities</code> and their respective subclasses.
* If no entity is provided, no type filtering is done.
*/
- Query createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities);
+ FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities);
/**
* Force the (re)indexing of a given <b>managed</b> object.
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2007-05-13 21:21:11 UTC (rev 11517)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -45,6 +45,7 @@
import org.hibernate.search.backend.WorkType;
import org.hibernate.search.engine.DocumentBuilder;
import org.hibernate.search.query.FullTextQueryImpl;
+import org.hibernate.search.query.FullTextQuery;
import org.hibernate.search.util.ContextHelper;
import org.hibernate.stat.SessionStatistics;
import org.hibernate.type.Type;
@@ -71,7 +72,7 @@
*
* @param entities must be immutable for the lifetime of the query object
*/
- public Query createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities) {
+ public FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities) {
return new FullTextQueryImpl( luceneQuery, entities, sessionImplementor, new ParameterMetadata(null, null) );
}
Added: trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java (rev 0)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQuery.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -0,0 +1,33 @@
+//$Id: $
+package org.hibernate.search.query;
+
+import org.apache.lucene.search.Sort;
+import org.hibernate.Query;
+
+/**
+ * The base interface for lucene powered searches.
+ *
+ * @author Hardy Ferentschik
+ */
+public interface FullTextQuery extends Query {
+ //todo Determine what other lucene specific features to expose via this
+ // interface. Maybe we should give access to the underlying lucene hit
+ // objects?
+
+ /**
+ * Allows to let lucene sort the results. This is useful when you have
+ * additional sort requirements on top of the default lucene ranking.
+ * Without lucene sorting you would have to retrieve the full result set and
+ * order the hibernate objects.
+ *
+ * @param sort The lucene sort object.
+ */
+ void setSort(Sort sort);
+
+ /**
+ * Returns the number of hits for this search
+ *
+ * @return The number of hits for this search
+ */
+ 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 21:21:11 UTC (rev 11517)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -22,6 +22,7 @@
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.Sort;
import org.apache.lucene.store.Directory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
@@ -39,10 +40,13 @@
import org.hibernate.search.util.ContextHelper;
/**
+ * Implementation of {@link org.hibernate.search.query.FullTextQuery}
+ *
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
//TODO implements setParameter()
-public class FullTextQueryImpl extends AbstractQueryImpl {
+public class FullTextQueryImpl extends AbstractQueryImpl implements FullTextQuery {
private static final Log log = LogFactory.getLog( FullTextQueryImpl.class );
private org.apache.lucene.search.Query luceneQuery;
private Class[] classes;
@@ -50,6 +54,7 @@
private Integer firstResult;
private Integer maxResults;
private int resultSize;
+ private Sort sort;
/**
* classes must be immutable
@@ -62,6 +67,11 @@
this.classes = classes;
}
+
+ public void setSort(Sort sort) {
+ this.sort = sort;
+ }
+
/**
* Return an interator on the results.
* Retrieve the object one by one (initialize it during the next() operation)
@@ -79,8 +89,7 @@
return new IteratorImpl( new ArrayList<EntityInfo>(0), (Session) this.session);
}
try {
- org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
- Hits hits = searcher.search( query );
+ Hits hits = getHits( searcher );
setResultSize( hits );
int first = first();
int max = max( first, hits );
@@ -118,8 +127,7 @@
//FIXME: handle null searcher
Hits hits;
try {
- org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
- hits = searcher.search( query );
+ hits = getHits( searcher );
setResultSize( hits );
int first = first();
int max = max( first, hits );
@@ -148,8 +156,7 @@
if (searcher == null) return new ArrayList(0);
Hits hits;
try {
- org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
- hits = searcher.search( query );
+ hits = getHits( searcher );;
setResultSize( hits );
int first = first();
int max = max( first, hits );
@@ -193,6 +200,25 @@
}
}
+ /**
+ * Execute the lucene search and return the machting hits.
+ *
+ * @param searcher The index searcher.
+ * @return The lucene hits.
+ * @throws IOException in case there is an error executing the lucene search.
+ */
+ private Hits getHits(Searcher searcher) throws IOException {
+ Hits hits;
+ org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
+ if(sort == null){
+ hits = searcher.search( query );
+ }
+ else {
+ hits = searcher.search( query, sort );
+ }
+ return hits;
+ }
+
private org.apache.lucene.search.Query filterQueryByClasses(org.apache.lucene.search.Query luceneQuery) {
//A query filter is more practical than a manual class filtering post query (esp on scrollable resultsets)
//it also probably minimise the memory footprint
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/query/IteratorImpl.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/query/IteratorImpl.java 2007-05-13 21:21:11 UTC (rev 11517)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/IteratorImpl.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -17,43 +17,43 @@
private final Session session;
private int index = 0;
private final int size;
- private Object next;
- private int nextObjectIndex = -1;
+ private Object next;
+ private int nextObjectIndex = -1;
- public IteratorImpl(List<EntityInfo> entityInfos, Session session) {
+ public IteratorImpl(List<EntityInfo> entityInfos, Session session) {
this.entityInfos = entityInfos;
this.session = session;
this.size = entityInfos.size();
}
- //side effect is to set up next
- public boolean hasNext() {
- if (nextObjectIndex == index) return next != null;
- next = null;
- nextObjectIndex = -1;
- do {
- if ( index >= size ) {
- nextObjectIndex = index;
- next = null;
- return false;
- }
- next = session.get( entityInfos.get( index ).clazz, entityInfos.get( index ).id );
- if (next == null) {
- index++;
- }
- else {
- nextObjectIndex = index;
- }
- }
- while( next == null );
- return true;
- }
+ //side effect is to set up next
+ public boolean hasNext() {
+ if ( nextObjectIndex == index ) return next != null;
+ next = null;
+ nextObjectIndex = -1;
+ do {
+ if ( index >= size ) {
+ nextObjectIndex = index;
+ next = null;
+ return false;
+ }
+ next = session.get( entityInfos.get( index ).clazz, entityInfos.get( index ).id );
+ if ( next == null ) {
+ index++;
+ }
+ else {
+ nextObjectIndex = index;
+ }
+ }
+ while ( next == null );
+ return true;
+ }
public Object next() {
- //hasNext() has side effect
- if ( ! hasNext() ) throw new NoSuchElementException("Out of boundaries");
- index++;
- return next;
+ //hasNext() has side effect
+ if ( !hasNext() ) throw new NoSuchElementException( "Out of boundaries" );
+ index++;
+ return next;
}
public void remove() {
Added: trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java
===================================================================
--- trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java (rev 0)
+++ trunk/HibernateExt/search/src/test/org/hibernate/search/test/query/LuceneQuerySortTest.java 2007-05-13 22:05:24 UTC (rev 11518)
@@ -0,0 +1,100 @@
+//$Id: $
+package org.hibernate.search.test.query;
+
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.Version;
+import org.hibernate.search.query.FullTextQuery;
+import org.hibernate.search.test.SearchTestCase;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class LuceneQuerySortTest extends SearchTestCase {
+
+ /**
+ * Class logger.
+ */
+ private static Log log = LogFactory.getLog( Version.class );
+
+ /**
+ * Test that we can change the default sort order of the lucene search result.
+ *
+ * @throws Exception in case the test fails.
+ */
+ public void testList() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ createTestBooks(s);
+ Transaction tx = s.beginTransaction();
+ QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+
+ Query query = parser.parse( "summary:lucene" );
+ FullTextQuery hibQuery = s.createFullTextQuery( query, Book.class );
+ List<Book> result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "Wrong number of test results.", 3, result.size() );
+ // make sure that the order is according to in which order the books got inserted
+ // into the index.
+ int id = 1;
+ for(Book b : result) {
+ assertEquals("Expected another id", new Integer(id), b.getId());
+ id++;
+ }
+
+ // now the same query, but with a lucene sort specified.
+ query = parser.parse( "summary:lucene" );
+ hibQuery = s.createFullTextQuery( query, Book.class );
+ Sort sort = new Sort(new SortField("id", true));
+ hibQuery.setSort(sort);
+ result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "Wrong number of test results.", 3, result.size() );
+ id = 3;
+ for(Book b : result) {
+ assertEquals("Expected another id", new Integer(id), b.getId());
+ id--;
+ }
+
+
+ tx.commit();
+ s.close();
+ }
+
+ /**
+ * Helper method creating three books with the same title and summary.
+ * When searching for these books the results should be returned in the order
+ * they got added to the index.
+ *
+ * @param s The full text session used to index the test data.
+ */
+ private void createTestBooks(FullTextSession s) {
+ Transaction tx = s.beginTransaction();
+ Book book = new Book(1, "Hibernate & Lucene", "This is a test book.");
+ s.save(book);
+ book = new Book(2, "Hibernate & Lucene", "This is a test book.");
+ s.save(book);
+ book = new Book(3, "Hibernate & Lucene", "This is a test book.");
+ s.save(book);
+ tx.commit();
+ s.clear();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ Book.class,
+ AlternateBook.class,
+ Clock.class
+ };
+ }
+}
16 years, 11 months
Hibernate SVN: r11517 - in trunk/HibernateExt/search/src/java/org/hibernate/search: query and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2007-05-13 17:21:11 -0400 (Sun, 13 May 2007)
New Revision: 11517
Modified:
trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java
trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java
Log:
Minor TODO
Modified: trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java
===================================================================
--- trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java 2007-05-10 12:21:05 UTC (rev 11516)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java 2007-05-13 21:21:11 UTC (rev 11517)
@@ -20,6 +20,7 @@
public void set(String name, Object value, Document document, Field.Store store, Field.Index index, Float boost) {
String indexedString = stringBridge.objectToString( value );
//Do not add fields on empty strings, seems a sensible default in most situations
+ //TODO if Store, probably also save empty ones
if ( StringHelper.isNotEmpty( indexedString ) ) {
Field field = new Field( name, indexedString, store, index );
if ( boost != null ) field.setBoost( boost );
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-10 12:21:05 UTC (rev 11516)
+++ trunk/HibernateExt/search/src/java/org/hibernate/search/query/FullTextQueryImpl.java 2007-05-13 21:21:11 UTC (rev 11517)
@@ -112,7 +112,7 @@
public ScrollableResults scroll() throws HibernateException {
//keep the searcher open until the resultset is closed
SearchFactory searchFactory = ContextHelper.getSearchFactoryBySFI( session );
- ;
+
//find the directories
Searcher searcher = buildSearcher( searchFactory );
//FIXME: handle null searcher
@@ -261,6 +261,7 @@
int dirNbr = directories.size();
if ( dirNbr > 1 ) {
try {
+ //TODO IndexSearcher on top of MultiReader
IndexSearcher[] searchers = new IndexSearcher[dirNbr];
Iterator<Directory> it = directories.iterator();
for ( int index = 0; index < dirNbr; index++ ) {
16 years, 11 months
Hibernate SVN: r11516 - trunk/Hibernate3/test/org/hibernate/test/sql/hand/custom/oracle.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-05-10 08:21:05 -0400 (Thu, 10 May 2007)
New Revision: 11516
Modified:
trunk/Hibernate3/test/org/hibernate/test/sql/hand/custom/oracle/OracleCustomSQLTest.java
Log:
missed change between 3.2 and trunk
Modified: trunk/Hibernate3/test/org/hibernate/test/sql/hand/custom/oracle/OracleCustomSQLTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/sql/hand/custom/oracle/OracleCustomSQLTest.java 2007-05-09 20:25:43 UTC (rev 11515)
+++ trunk/Hibernate3/test/org/hibernate/test/sql/hand/custom/oracle/OracleCustomSQLTest.java 2007-05-10 12:21:05 UTC (rev 11516)
@@ -21,7 +21,7 @@
}
public String[] getMappings() {
- return new String[] { "sql/OracleEmployment.hbm.xml", "sql/OracleDriverStoredProcedures.hbm.xml" };
+ return new String[] { "sql/hand/custom/oracle/Mappings.hbm.xml", "sql/hand/custom/oracle/StoredProcedures.hbm.xml" };
}
public static Test suite() {
16 years, 11 months
Hibernate SVN: r11515 - tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-05-09 16:25:43 -0400 (Wed, 09 May 2007)
New Revision: 11515
Added:
tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
Log:
correcting test mapping
Copied: tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml (from rev 11514, branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml)
===================================================================
--- tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml (rev 0)
+++ tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 20:25:43 UTC (rev 11515)
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.usercollection.parameterized" default-access="field">
+
+ <typedef name="DefaultableList" class="org.hibernate.test.usercollection.parameterized.DefaultableListType">
+ <param name="default">Hello</param>
+ </typedef>
+
+ <class name="Entity">
+ <id name="name" type="string"/>
+ <list name="values" fetch="join" table="ENT_VAL" collection-type="DefaultableList">
+ <key column="ENT_ID"/>
+ <list-index column="POS"/>
+ <element type="string" column="VAL"/>
+ </list>
+ </class>
+
+</hibernate-mapping>
\ No newline at end of file
16 years, 11 months
Hibernate SVN: r11514 - tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-05-09 16:24:58 -0400 (Wed, 09 May 2007)
New Revision: 11514
Removed:
tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
Log:
correcting test mapping
Deleted: tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
===================================================================
--- tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 19:56:43 UTC (rev 11513)
+++ tags/v324/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 20:24:58 UTC (rev 11514)
@@ -1,21 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
-<hibernate-mapping package="org.hibernate.test.usercollection.parameterized" default-access="field">
-
- <typedef name="DefaultableList" class="org.hibernate.test.usercollection.parameterized.DefaultableListType">
- <param name="default">Hello</param>
- </typedef>
-
- <class name="Entity">
- <id name="name" type="string"/>
- <list name="values" fetch="join" table="ENT_VAL" collection-type="DefaultableList">
- <key column="ENT_ID"/>
- <list-index column="POSITION"/>
- <element type="string" column="VAL"/>
- </list>
- </class>
-
-</hibernate-mapping>
\ No newline at end of file
16 years, 11 months
Hibernate SVN: r11513 - branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-05-09 15:56:43 -0400 (Wed, 09 May 2007)
New Revision: 11513
Modified:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
Log:
fixed test mapping issue
Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 19:56:22 UTC (rev 11512)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 19:56:43 UTC (rev 11513)
@@ -13,7 +13,7 @@
<id name="name" type="string"/>
<list name="values" fetch="join" table="ENT_VAL" collection-type="DefaultableList">
<key column="ENT_ID"/>
- <list-index column="POSITION"/>
+ <list-index column="POS"/>
<element type="string" column="VAL"/>
</list>
</class>
16 years, 11 months
Hibernate SVN: r11512 - trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2007-05-09 15:56:22 -0400 (Wed, 09 May 2007)
New Revision: 11512
Modified:
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
Log:
fixed test mapping issue
Modified: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 19:07:03 UTC (rev 11511)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 19:56:22 UTC (rev 11512)
@@ -13,7 +13,7 @@
<id name="name" type="string"/>
<list name="values" fetch="join" table="ENT_VAL" collection-type="DefaultableList">
<key column="ENT_ID"/>
- <list-index column="POSITION"/>
+ <list-index column="POS"/>
<element type="string" column="VAL"/>
</list>
</class>
16 years, 11 months