[hibernate-commits] Hibernate SVN: r15602 - in search/trunk/src: java/org/hibernate/search/query and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Nov 20 09:17:21 EST 2008


Author: hardy.ferentschik
Date: 2008-11-20 09:17:21 -0500 (Thu, 20 Nov 2008)
New Revision: 15602

Added:
   search/trunk/src/test/org/hibernate/search/test/inheritance/Bird.java
   search/trunk/src/test/org/hibernate/search/test/inheritance/Eagle.java
Modified:
   search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
   search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
   search/trunk/src/java/org/hibernate/search/query/FullTextQueryImpl.java
   search/trunk/src/test/org/hibernate/search/test/inheritance/InheritanceTest.java
Log:
HSEARCH-160 
- purging and searching on unindexed classes throws now an exception
- extented the tests

Modified: search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java	2008-11-20 13:09:44 UTC (rev 15601)
+++ search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -106,18 +106,15 @@
 			return;
 		}
 
-		// accessing the document builders is not strictly necessary but a small optimization plus let's make sure the
-		// client didn't mess something up.
 		SearchFactoryImplementor searchFactoryImplementor = getSearchFactoryImplementor();
 		Set<Class<?>> targetedClasses = searchFactoryImplementor.getIndexedTypesPolymorphic( new Class[] {entityType} );
+		if ( targetedClasses.isEmpty() ) {
+			String msg = entityType.getName() + " is not an indexed entity or a subclass of an indexed entity";
+			throw new IllegalArgumentException( msg );
+		}
 
 		for ( Class clazz : targetedClasses ) {
 			DocumentBuilder builder = searchFactoryImplementor.getDocumentBuilder( clazz );
-			if ( builder == null ) {
-				String msg = "Entity to index is not an @Indexed entity: " + clazz.getName();
-				throw new IllegalArgumentException( msg );
-			}
-
 			Work<T> work;
 			if ( id == null ) {
 				// purge the main entity

Modified: search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java	2008-11-20 13:09:44 UTC (rev 15601)
+++ search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -534,17 +534,19 @@
 		}
 
 		Set<Class<?>> getIndexedClasses(Class<?>[] classes) {
-			Set<Class<?>> classesSet = new HashSet<Class<?>>();
+			Set<Class<?>> idexedClasses = new HashSet<Class<?>>();
 			for ( Class clazz : classes ) {
 				Set<Class<?>> set = classToIndexedClass.get( clazz );
 				if ( set != null ) {
-					classesSet.addAll( set );
+					// at this point we don't have to care about including indexed subclasses of a indexed class
+					// MultiClassesQueryLoader will take care of this later and optimise the queries
+					idexedClasses.addAll( set );
 				}
 			}
 			if ( log.isTraceEnabled() ) {
-				log.trace( "Targeted indexed classes for {}: {}", Arrays.toString( classes ), classesSet );
+				log.trace( "Targeted indexed classes for {}: {}", Arrays.toString( classes ), idexedClasses );
 			}
-			return classesSet;
+			return idexedClasses;
 		}
 	}
 }

Modified: search/trunk/src/java/org/hibernate/search/query/FullTextQueryImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/query/FullTextQueryImpl.java	2008-11-20 13:09:44 UTC (rev 15601)
+++ search/trunk/src/java/org/hibernate/search/query/FullTextQueryImpl.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -104,6 +104,10 @@
 		super( query.toString(), null, session, parameterMetadata );
 		this.luceneQuery = query;
 		this.targetedEntities = getSearchFactoryImplementor().getIndexedTypesPolymorphic( classes );
+		if ( classes != null && classes.length > 0 && targetedEntities.size() == 0 ) {
+			String msg = "None of the specified entity types or any of their subclasses are indexed.";
+			throw new IllegalArgumentException( msg );
+		}
 	}
 
 	/**

Copied: search/trunk/src/test/org/hibernate/search/test/inheritance/Bird.java (from rev 15592, search/trunk/src/test/org/hibernate/search/test/inheritance/Fish.java)
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/inheritance/Bird.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/inheritance/Bird.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -0,0 +1,28 @@
+//$Id$
+package org.hibernate.search.test.inheritance;
+
+import javax.persistence.Entity;
+
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at Indexed
+public class Bird extends Animal {
+
+	private int numberOfEggs;
+
+	@Field(index = Index.UN_TOKENIZED, store = Store.YES)
+	public int getNumberOfEggs() {
+		return numberOfEggs;
+	}
+
+	public void setNumberOfEggs(int numberOfEggs) {
+		this.numberOfEggs = numberOfEggs;
+	}
+}
\ No newline at end of file

Added: search/trunk/src/test/org/hibernate/search/test/inheritance/Eagle.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/inheritance/Eagle.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/inheritance/Eagle.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -0,0 +1,33 @@
+//$Id: Fish.java 15592 2008-11-19 14:17:48Z hardy.ferentschik $
+package org.hibernate.search.test.inheritance;
+
+import javax.persistence.Entity;
+
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at Indexed
+public class Eagle extends Bird {
+
+	private WingType wingYype;
+
+	@Field(index = Index.UN_TOKENIZED, store = Store.YES)
+	public WingType getWingYype() {
+		return wingYype;
+	}
+
+	public void setWingYype(WingType wingYype) {
+		this.wingYype = wingYype;
+	}
+
+	public enum WingType {
+		BROAD,
+		LONG
+	}
+}
\ No newline at end of file

Modified: search/trunk/src/test/org/hibernate/search/test/inheritance/InheritanceTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/inheritance/InheritanceTest.java	2008-11-20 13:09:44 UTC (rev 15601)
+++ search/trunk/src/test/org/hibernate/search/test/inheritance/InheritanceTest.java	2008-11-20 14:17:21 UTC (rev 15602)
@@ -9,10 +9,13 @@
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.RangeQuery;
+import org.apache.lucene.search.TermQuery;
+import org.slf4j.Logger;
 
 import org.hibernate.Transaction;
 import org.hibernate.search.FullTextSession;
 import org.hibernate.search.Search;
+import org.hibernate.search.util.LoggerFactory;
 import org.hibernate.search.test.SearchTestCase;
 
 /**
@@ -20,10 +23,38 @@
  */
 public class InheritanceTest extends SearchTestCase {
 
+	private static final Logger log = LoggerFactory.make();
+
 	protected void setUp() throws Exception {
 		super.setUp();
 	}
 
+	public void testSearchUnindexClass() throws Exception {
+		createTestData();
+
+		QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
+		Query query = parser.parse( "Elephant" );
+
+		FullTextSession s = Search.getFullTextSession( openSession() );
+		Transaction tx = s.beginTransaction();
+		try {
+			org.hibernate.Query hibQuery = s.createFullTextQuery( query, String.class );
+			hibQuery.list();
+			tx.commit();
+			fail();
+		}
+		catch ( IllegalArgumentException iae ) {
+			log.debug( "success" );
+		}
+
+		tx = s.beginTransaction();
+		org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
+		assertItsTheElephant( hibQuery.list() );
+		tx.commit();
+
+		s.close();
+	}
+
 	public void testInheritance() throws Exception {
 		createTestData();
 
@@ -35,6 +66,10 @@
 		org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
 		assertItsTheElephant( hibQuery.list() );
 
+		query = parser.parse( "Elephant" );
+		hibQuery = s.createFullTextQuery( query);
+		assertItsTheElephant( hibQuery.list() );
+
 		query = parser.parse( "hasSweatGlands:false" );
 		hibQuery = s.createFullTextQuery( query, Animal.class, Mammal.class );
 		assertItsTheElephant( hibQuery.list() );
@@ -81,19 +116,51 @@
 		hibQuery = s.createFullTextQuery( query, Serializable.class );
 		assertItsTheElephant( hibQuery.list() );
 
-		hibQuery = s.createFullTextQuery( query, Mammal.class, Animal.class, Being.class, Object.class, Serializable.class );
+		hibQuery = s.createFullTextQuery(
+				query, Mammal.class, Animal.class, Being.class, Object.class, Serializable.class
+		);
 		assertItsTheElephant( hibQuery.list() );
 
 		tx.commit();
 		s.close();
 	}
 
-	private void assertItsTheElephant(List result) {
+	public void testSubclassInclusion() throws Exception {
+		createTestData();
+
+		FullTextSession s = Search.getFullTextSession( openSession() );
+		Transaction tx = s.beginTransaction();
+
+		Query query = new TermQuery( new Term( "numberOfEggs", "2" ) );
+		org.hibernate.Query hibQuery = s.createFullTextQuery( query, Eagle.class );
+		List result = hibQuery.list();
 		assertNotNull( result );
-		assertEquals( "Wrong number of results", 1, result.size() );
-		assertTrue( "Wrong result type", result.get( 0 ) instanceof Mammal );
-		Mammal mammal = ( Mammal ) result.get( 0 );
-		assertEquals( "Wrong animal name", "Elephant", mammal.getName() );
+		assertEquals( "Wrong number of hits. There should be two birds.", 1, result.size() );
+
+		query = new TermQuery( new Term( "numberOfEggs", "2" ) );
+		hibQuery = s.createFullTextQuery( query, Bird.class );
+		result = hibQuery.list();
+		assertNotNull( result );
+		assertEquals( "Wrong number of hits. There should be two birds.", 2, result.size() );
+
+		query = new TermQuery( new Term( "numberOfEggs", "2" ) );
+		hibQuery = s.createFullTextQuery( query, Mammal.class );
+		result = hibQuery.list();
+		assertNotNull( result );
+		assertEquals( "Wrong number of hits. There should be two birds.", 0, result.size() );
+
+		try {
+			query = new TermQuery( new Term( "numberOfEggs", "2" ) );
+			hibQuery = s.createFullTextQuery( query, String.class );
+			hibQuery.list();
+			fail();
+		}
+		catch ( IllegalArgumentException iae ) {
+			log.debug( "success" );
+		}
+
+		tx.commit();
+		s.close();
 	}
 
 	/**
@@ -104,27 +171,71 @@
 	public void testPurgeIndex() throws Exception {
 		createTestData();
 		FullTextSession s = Search.getFullTextSession( openSession() );
+
 		Transaction tx = s.beginTransaction();
-		QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
+		assertNumberOfAnimals( s, 5 );
+		tx.commit();
 
-		Query query = parser.parse( "Elephant OR White Pointer OR Chimpanzee" );
-		List result = s.createFullTextQuery( query, Animal.class ).list();
-		assertNotNull( result );
-		assertEquals( "Wrong number of hits. There should be one elephant and one shark.", 3, result.size() );
+		tx = s.beginTransaction();
+		s.purgeAll( Serializable.class );
+		tx.commit();
 
-		s.purgeAll( Animal.class );
+		tx = s.beginTransaction();
+		assertNumberOfAnimals( s, 3 );
 		tx.commit();
+
 		tx = s.beginTransaction();
-		result = s.createFullTextQuery( query, Animal.class ).list();
-		assertNotNull( result );
-		assertEquals(
-				"Wrong number of hits. Purging the Animal class should also purge the Mammals", 0, result.size()
-		);
+		s.purgeAll( Bird.class );
+		tx.commit();
 
+		tx = s.beginTransaction();
+		assertNumberOfAnimals( s, 1 );
 		tx.commit();
+
+		tx = s.beginTransaction();
+		s.purgeAll( Object.class );
+		tx.commit();
+
+		tx = s.beginTransaction();
+		assertNumberOfAnimals( s, 0 );
+		tx.commit();
+
 		s.close();
 	}
 
+	/**
+	 * Tests that purging the an uninexed class triggers an exception.
+	 *
+	 * @throws Exception in case the test fails.
+	 */
+	public void testPurgeUnIndexClass() throws Exception {
+		createTestData();
+		FullTextSession s = Search.getFullTextSession( openSession() );
+
+		Transaction tx = s.beginTransaction();
+		assertNumberOfAnimals( s, 5 );
+		tx.commit();
+
+		tx = s.beginTransaction();
+		try {
+			s.purgeAll( String.class );
+			tx.commit();
+			fail();
+		}
+		catch ( IllegalArgumentException iae ) {
+			log.debug( "Success" );
+		}
+		s.close();
+	}
+
+	private void assertNumberOfAnimals(FullTextSession s, int count) throws Exception {
+		QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
+		Query query = parser.parse( "Elephant OR White Pointer OR Chimpanzee OR Dove or Eagle" );
+		List result = s.createFullTextQuery( query, Animal.class ).list();
+		assertNotNull( result );
+		assertEquals( "Wrong number of hits. There should be one elephant and one shark.", count, result.size() );
+	}
+
 	private void createTestData() {
 		FullTextSession s = Search.getFullTextSession( openSession() );
 		Transaction tx = s.beginTransaction();
@@ -147,15 +258,36 @@
 		chimp.setWeight( 50 );
 		s.save( chimp );
 
+		Bird dove = new Bird();
+		dove.setName( "Dove" );
+		dove.setNumberOfEggs( 2 );
+		s.save( dove );
+
+		Eagle eagle = new Eagle();
+		eagle.setName( "Bald Eagle" );
+		eagle.setNumberOfEggs( 2 );
+		eagle.setWingYype( Eagle.WingType.BROAD );
+		s.save( eagle );
+
 		tx.commit();
 		s.clear();
 	}
 
+	private void assertItsTheElephant(List result) {
+		assertNotNull( result );
+		assertEquals( "Wrong number of results", 1, result.size() );
+		assertTrue( "Wrong result type", result.get( 0 ) instanceof Mammal );
+		Mammal mammal = ( Mammal ) result.get( 0 );
+		assertEquals( "Wrong animal name", "Elephant", mammal.getName() );
+	}
+
 	protected Class[] getMappings() {
 		return new Class[] {
 				Animal.class,
 				Mammal.class,
-				Fish.class
+				Fish.class,
+				Bird.class,
+				Eagle.class
 		};
 	}
 }




More information about the hibernate-commits mailing list