[hibernate-commits] Hibernate SVN: r20812 - in search/trunk/hibernate-search/src: test/java/org/hibernate/search/test/query and 1 other directory.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue Oct 12 09:57:54 EDT 2010
Author: epbernard
Date: 2010-10-12 09:57:53 -0400 (Tue, 12 Oct 2010)
New Revision: 20812
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/ProjectionQueryTest.java
Log:
HSEARCH-545 For score calculation when custom sort and SCORE projection is used
The behavior has changed in Lucene 3
Modified: search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java
===================================================================
--- search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java 2010-10-12 13:57:03 UTC (rev 20811)
+++ search/trunk/hibernate-search/src/main/java/org/hibernate/search/query/FullTextQueryImpl.java 2010-10-12 13:57:53 UTC (rev 20812)
@@ -357,7 +357,7 @@
public Explanation explain(int documentId) {
Explanation explanation = null;
- Searcher searcher = buildSearcher( searchFactoryImplementor );
+ Searcher searcher = buildSearcher( searchFactoryImplementor, true );
if ( searcher == null ) {
throw new SearchException(
"Unable to build explanation for document id:"
@@ -690,15 +690,20 @@
0;
}
+ private IndexSearcher buildSearcher(SearchFactoryImplementor searchFactoryImplementor) {
+ return buildSearcher(searchFactoryImplementor, null);
+ }
+
/**
* Build the index searcher for this fulltext query.
*
* @param searchFactoryImplementor the search factory.
+ * @param forceScoring if true, force SCORE computation, if false, force not to compute score, if null used best choice
*
* @return the <code>IndexSearcher</code> for this query (can be <code>null</code>.
* TODO change classesAndSubclasses by side effect, which is a mismatch with the Searcher return, fix that.
*/
- private IndexSearcher buildSearcher(SearchFactoryImplementor searchFactoryImplementor) {
+ private IndexSearcher buildSearcher(SearchFactoryImplementor searchFactoryImplementor, Boolean forceScoring) {
Map<Class<?>, DocumentBuilderIndexedEntity<?>> builders = searchFactoryImplementor.getDocumentBuildersIndexedEntities();
List<DirectoryProvider> targetedDirectories = new ArrayList<DirectoryProvider>();
Set<String> idFieldNames = new HashSet<String>();
@@ -784,6 +789,27 @@
)
);
is.setSimilarity( searcherSimilarity );
+
+ //handle the sort and projection
+ final String[] projection = this.indexProjection;
+ if ( Boolean.TRUE.equals( forceScoring ) ) {
+ is.setDefaultFieldSortScoring(true, true);
+ }
+ else if ( Boolean.FALSE.equals( forceScoring ) ) {
+ is.setDefaultFieldSortScoring(false, false);
+ }
+ else if ( this.sort != null && projection != null ) {
+ boolean activate = false;
+ for(String field : projection) {
+ if ( SCORE.equals(field) ) {
+ activate = true;
+ break;
+ }
+ }
+ if (activate) {
+ is.setDefaultFieldSortScoring(true, false);
+ }
+ }
return is;
}
@@ -832,7 +858,7 @@
public int getResultSize() {
if ( resultSize == null ) {
//get result size without object initialization
- IndexSearcher searcher = buildSearcher( searchFactoryImplementor );
+ IndexSearcher searcher = buildSearcher( searchFactoryImplementor, false );
if ( searcher == null ) {
resultSize = 0;
}
Modified: search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/ProjectionQueryTest.java
===================================================================
--- search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/ProjectionQueryTest.java 2010-10-12 13:57:03 UTC (rev 20811)
+++ search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/ProjectionQueryTest.java 2010-10-12 13:57:53 UTC (rev 20812)
@@ -24,6 +24,7 @@
*/
package org.hibernate.search.test.query;
+import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
@@ -31,8 +32,9 @@
import java.util.Date;
import org.apache.lucene.document.Document;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.search.Query;
+import org.apache.lucene.search.*;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
@@ -309,6 +311,7 @@
assertEquals( "THIS incorrect", "Jackson", ( ( Employee ) projection[3] ).getLastname() );
assertEquals( "THIS incorrect", projection[3], s.get( Employee.class, ( Serializable ) projection[0] ) );
assertTrue( "SCORE incorrect", projection[4] instanceof Float );
+ assertFalse( "SCORE should not be a NaN", Float.isNaN( (Float) projection[4] ) );
assertTrue( "DOCUMENT incorrect", projection[5] instanceof Document );
assertEquals( "DOCUMENT size incorrect", 5, ( ( Document ) projection[5] ).getFields().size() );
assertEquals( "ID incorrect", 1001, projection[6] );
@@ -338,6 +341,18 @@
assertNotNull( "Date", projection[8] );
assertNotNull( "Lucene internal doc id", projection[9] );
+ hibQuery.setSort( new Sort( new SortField("lastname", SortField.STRING_VAL) ) );
+ hibQuery.setProjection(
+ FullTextQuery.THIS, FullTextQuery.SCORE
+ );
+
+ result = hibQuery.list();
+
+ projection = ( Object[] ) result.get( 0 );
+
+ assertTrue( "SCORE incorrect", projection[1] instanceof Float );
+ assertFalse( "SCORE should not be a NaN", Float.isNaN( (Float) projection[1] ) );
+
//cleanup
for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
s.delete( element );
More information about the hibernate-commits
mailing list