[hibernate-commits] Hibernate SVN: r15351 - in search/trunk/src: test/org/hibernate/search/test/analyzer and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Oct 15 09:25:52 EDT 2008


Author: hardy.ferentschik
Date: 2008-10-15 09:25:51 -0400 (Wed, 15 Oct 2008)
New Revision: 15351

Added:
   search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/
   search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/AnalyzerInheritanceTest.java
   search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/BaseClass.java
   search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/ISOLatin1Analyzer.java
   search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/SubClass.java
Modified:
   search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
   search/trunk/src/test/org/hibernate/search/test/util/AnalyzerUtils.java
Log:
HSEARCH-267 
Added test case and changed code in DocumentBuilder. When a the @Field annotation is bound no scoped analyzer should be added for this field if not explicitly defined. This will ensure that global analyzer will be used later. The old code was adding the current global analyzer which in an inheritance scenario might be wrong.

Modified: search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java	2008-10-15 08:16:43 UTC (rev 15350)
+++ search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -420,9 +420,9 @@
 		// Field > property > entity analyzer
 		Analyzer analyzer = getAnalyzer( fieldAnn.analyzer(), context );
 		if ( analyzer == null ) analyzer = getAnalyzer( member, context );
-		if ( analyzer == null ) analyzer = propertiesMetadata.analyzer;
-		if ( analyzer == null ) throw new AssertionFailure( "Analizer should not be undefined" );
-		this.analyzer.addScopedAnalyzer( fieldName, analyzer );
+		if ( analyzer != null ) {
+			this.analyzer.addScopedAnalyzer( fieldName, analyzer );
+		}
 	}
 
 	private String buildEmbeddedPrefix(String prefix, IndexedEmbedded embeddedAnn, XProperty member) {

Added: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/AnalyzerInheritanceTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/AnalyzerInheritanceTest.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/AnalyzerInheritanceTest.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -0,0 +1,99 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.search.test.analyzer.inheritance;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.Token;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.test.util.AnalyzerUtils;
+
+/**
+ * Test to verify HSEARCH-267.
+ *
+ * A base class defines a field as indexable without specifying an explicit analyzer. A subclass then defines ab analyzer
+ * at class level. This should also be the analyzer used for indexing the field in the base class.
+ *
+ * @author Hardy Ferentschik
+ */
+public class AnalyzerInheritanceTest extends SearchTestCase {
+
+	public static final Logger log = LoggerFactory.getLogger( AnalyzerInheritanceTest.class );
+
+	/**
+	 * Try to verify that the right analyzer is used by indexing and searching.
+	 *
+	 * @throws Exception in case the test fails.
+	 */
+	public void testBySearch() throws Exception {
+		SubClass testClass = new SubClass();
+		testClass.setName( "Proca•ne" );
+		FullTextSession s = Search.getFullTextSession( openSession() );
+		Transaction tx = s.beginTransaction();
+		s.persist( testClass );
+		tx.commit();
+
+		tx = s.beginTransaction();
+
+
+		QueryParser parser = new QueryParser( "name", s.getSearchFactory().getAnalyzer( SubClass.class ) );
+		org.apache.lucene.search.Query luceneQuery = parser.parse( "name:Proca•ne" );
+		FullTextQuery query = s.createFullTextQuery( luceneQuery, SubClass.class );
+		assertEquals( 1, query.getResultSize() );
+
+		luceneQuery = parser.parse( "name:Procaine" );
+		query = s.createFullTextQuery( luceneQuery, SubClass.class );
+		assertEquals( 1, query.getResultSize() );
+
+		// make sure the result is not always 1
+		luceneQuery = parser.parse( "name:foo" );
+		query = s.createFullTextQuery( luceneQuery, SubClass.class );
+		assertEquals( 0, query.getResultSize() );
+
+		tx.commit();
+		s.close();
+	}
+
+	/**
+	 * Try to verify that the right analyzer is used by explicitly retrieving the analyzer form the factory.
+	 *
+	 * @throws Exception in case the test fails.
+	 */
+	public void testByAnalyzerRetrieval() throws Exception {
+
+		FullTextSession s = Search.getFullTextSession( openSession() );
+		Analyzer analyzer = s.getSearchFactory().getAnalyzer( SubClass.class );
+
+		Token[] tokens = AnalyzerUtils.tokensFromAnalysis(analyzer, "name", "Proca•ne");
+		AnalyzerUtils.assertTokensEqual( tokens, new String[]{"Procaine"});
+
+		s.close();
+	}
+
+
+	protected Class[] getMappings() {
+		return new Class[] { SubClass.class };
+	}
+}


Property changes on: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/AnalyzerInheritanceTest.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/BaseClass.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/BaseClass.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/BaseClass.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -0,0 +1,63 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.search.test.analyzer.inheritance;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at Inheritance(strategy = InheritanceType.SINGLE_TABLE)
+public abstract class BaseClass {
+
+	private Integer id;
+
+	protected String name;
+
+	@Id
+	@GeneratedValue
+	@DocumentId
+	public Integer getId() {
+		return id;
+	}
+
+	public void setId(Integer id) {
+		this.id = id;
+	}
+
+
+	@Field(name = "name", index = Index.TOKENIZED, store = Store.YES)
+	public String getName() {
+		return name;
+	}
+
+
+	public void setName(String name) {
+		this.name = name;
+	}
+}


Property changes on: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/BaseClass.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/ISOLatin1Analyzer.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/ISOLatin1Analyzer.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/ISOLatin1Analyzer.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -0,0 +1,35 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.search.test.analyzer.inheritance;
+
+import java.io.Reader;
+
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.ISOLatin1AccentFilter;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.standard.StandardTokenizer;
+
+/**
+ * @author Hardy Ferentschik
+ */
+	public class ISOLatin1Analyzer extends Analyzer {
+		public TokenStream tokenStream(String s, Reader reader) {
+			TokenStream result = new StandardTokenizer( reader );
+			return new ISOLatin1AccentFilter( result );
+		}
+	}


Property changes on: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/ISOLatin1Analyzer.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/SubClass.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/SubClass.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/SubClass.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -0,0 +1,33 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.search.test.analyzer.inheritance;
+
+import javax.persistence.Entity;
+
+import org.hibernate.search.annotations.Analyzer;
+import org.hibernate.search.annotations.Indexed;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at Indexed
+ at Analyzer(impl = ISOLatin1Analyzer.class)
+public class SubClass extends BaseClass {
+
+}


Property changes on: search/trunk/src/test/org/hibernate/search/test/analyzer/inheritance/SubClass.java
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: search/trunk/src/test/org/hibernate/search/test/util/AnalyzerUtils.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/util/AnalyzerUtils.java	2008-10-15 08:16:43 UTC (rev 15350)
+++ search/trunk/src/test/org/hibernate/search/test/util/AnalyzerUtils.java	2008-10-15 13:25:51 UTC (rev 15351)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 package org.hibernate.search.test.util;
 
 import java.io.IOException;
@@ -7,7 +7,6 @@
 import java.util.List;
 
 import junit.framework.Assert;
-
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.Token;
 import org.apache.lucene.analysis.TokenStream;
@@ -16,38 +15,39 @@
 
 /**
  * Helper class to test analyzers. Taken and modified from <i>Lucene in Action</i>.
- * 
+ *
  * @author Hardy Ferentschik
  */
 public class AnalyzerUtils {
 
-	public static final Logger log = LoggerFactory.getLogger(AnalyzerUtils.class);
+	public static final Logger log = LoggerFactory.getLogger( AnalyzerUtils.class );
 
 	public static Token[] tokensFromAnalysis(Analyzer analyzer, String field, String text) throws IOException {
-		TokenStream stream = analyzer.tokenStream(field, new StringReader(text));
+		TokenStream stream = analyzer.tokenStream( field, new StringReader( text ) );
 		List<Token> tokenList = new ArrayList<Token>();
 		while ( true ) {
 			Token token = stream.next();
-			if ( token == null )
+			if ( token == null ) {
 				break;
+			}
 
-			tokenList.add(token);
+			tokenList.add( token );
 		}
 
-		return (Token[]) tokenList.toArray(new Token[0]);
+		return ( Token[] ) tokenList.toArray( new Token[0] );
 	}
 
 	public static void displayTokens(Analyzer analyzer, String field, String text) throws IOException {
-		Token[] tokens = tokensFromAnalysis(analyzer, field, text);
+		Token[] tokens = tokensFromAnalysis( analyzer, field, text );
 
 		for ( int i = 0; i < tokens.length; i++ ) {
 			Token token = tokens[i];
-			log.debug("[" + getTermText(token) + "] ");
+			log.debug( "[" + getTermText( token ) + "] " );
 		}
 	}
 
 	public static void displayTokensWithPositions(Analyzer analyzer, String field, String text) throws IOException {
-		Token[] tokens = tokensFromAnalysis(analyzer, field, text);
+		Token[] tokens = tokensFromAnalysis( analyzer, field, text );
 
 		int position = 0;
 
@@ -59,15 +59,15 @@
 			if ( increment > 0 ) {
 				position = position + increment;
 				System.out.println();
-				System.out.print(position + ": ");
+				System.out.print( position + ": " );
 			}
 
-			log.debug("[" + getTermText(token) + "] ");
+			log.debug( "[" + getTermText( token ) + "] " );
 		}
 	}
 
 	public static void displayTokensWithFullDetails(Analyzer analyzer, String field, String text) throws IOException {
-		Token[] tokens = tokensFromAnalysis(analyzer, field, text);
+		Token[] tokens = tokensFromAnalysis( analyzer, field, text );
 		StringBuilder builder = new StringBuilder();
 		int position = 0;
 
@@ -78,24 +78,33 @@
 
 			if ( increment > 0 ) {
 				position = position + increment;
-				builder.append("\n").append(position).append(": ");
+				builder.append( "\n" ).append( position ).append( ": " );
 			}
 
-			builder.append("[").append(getTermText(token)).append(":").append(token.startOffset()).append("->").append(
-					token.endOffset()).append(":").append(token.type()).append("] ");
-			log.debug(builder.toString());
+			builder.append( "[" )
+					.append( getTermText( token ) )
+					.append( ":" )
+					.append( token.startOffset() )
+					.append( "->" )
+					.append(
+							token.endOffset()
+					)
+					.append( ":" )
+					.append( token.type() )
+					.append( "] " );
+			log.debug( builder.toString() );
 		}
 	}
 
 	public static void assertTokensEqual(Token[] tokens, String[] strings) {
-		Assert.assertEquals(strings.length, tokens.length);
+		Assert.assertEquals( strings.length, tokens.length );
 
 		for ( int i = 0; i < tokens.length; i++ ) {
-			Assert.assertEquals("index " + i, strings[i], getTermText(tokens[i]));
+			Assert.assertEquals( "index " + i, strings[i], getTermText( tokens[i] ) );
 		}
 	}
-	
+
 	public static String getTermText(Token token) {
-		return new String(token.termBuffer(), 0, token.termLength());
+		return new String( token.termBuffer(), 0, token.termLength() );
 	}
 }




More information about the hibernate-commits mailing list