[hibernate-commits] Hibernate SVN: r19465 - in search/branches/v3_1_1_GA_CP/src: main/java/org/hibernate/search/engine and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue May 11 05:39:55 EDT 2010
Author: stliu
Date: 2010-05-11 05:39:54 -0400 (Tue, 11 May 2010)
New Revision: 19465
Added:
search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/annotations/DynamicBoost.java
search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/BoostStrategy.java
search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DefaultBoostStrategy.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomBoostStrategy.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomFieldBoostStrategy.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostedDescriptionLibrary.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostingTest.java
Modified:
search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java
search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/FieldBoostTest.java
Log:
JBPAPP-4279 Hibernate Search @DynamicBoost feature back port
Added: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/annotations/DynamicBoost.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/annotations/DynamicBoost.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/annotations/DynamicBoost.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,52 @@
+/* $Id: DynamicBoost.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.hibernate.search.engine.BoostStrategy;
+
+/**
+ * Apply a dynamic boost factor on a field or a whole entity.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
+ at Documented
+public @interface DynamicBoost {
+
+ /**
+ * @return An implementation of <code>BoostStrategy</code> to apply a boost
+ * value as function of the annotated object.
+ *
+ * @see org.hibernate.search.engine.BoostStrategy
+ */
+ public abstract Class<? extends BoostStrategy> impl();
+}
Added: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/BoostStrategy.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/BoostStrategy.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/BoostStrategy.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,37 @@
+/* $Id: BoostStrategy.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.engine;
+
+/**
+ * Interface to implement boost values as functions
+ * of the object value being boosted.
+ * Implementations must be threadsafe.
+ *
+ * @author Hardy Ferentschik
+ * @see org.hibernate.search.annotations.Boost
+ */
+public interface BoostStrategy {
+ public float defineBoost(Object value);
+}
Added: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DefaultBoostStrategy.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DefaultBoostStrategy.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DefaultBoostStrategy.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,35 @@
+/* $Id: DefaultBoostStrategy.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.engine;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DefaultBoostStrategy implements BoostStrategy {
+
+ public float defineBoost(Object value) {
+ return 1.0f;
+ }
+}
Modified: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java 2010-05-11 01:48:17 UTC (rev 19464)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderContainedEntity.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -35,6 +35,7 @@
import org.hibernate.search.annotations.ClassBridges;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.DynamicBoost;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;
@@ -99,6 +100,7 @@
protected void init(XClass clazz, InitContext context) {
metadata.boost = getBoost( clazz );
+ metadata.classBoostStrategy = getDynamicBoost( clazz );
metadata.analyzer = context.getDefaultAnalyzer();
Set<XClass> processedClasses = new HashSet<XClass>();
@@ -461,6 +463,7 @@
propertiesMetadata.fieldStore.add( getStore( fieldAnn.store() ) );
propertiesMetadata.fieldIndex.add( getIndex( fieldAnn.index() ) );
propertiesMetadata.fieldBoosts.add( getBoost( member, fieldAnn ) );
+ propertiesMetadata.dynamicFieldBoosts.add( getDynamicBoost( member ) );
propertiesMetadata.fieldTermVectors.add( getTermVector( fieldAnn.termVector() ) );
propertiesMetadata.fieldBridges.add( BridgeFactory.guessType( fieldAnn, member, reflectionManager ) );
@@ -485,7 +488,24 @@
}
return computedBoost;
}
+ protected BoostStrategy getDynamicBoost(XProperty member) {
+ DynamicBoost boostAnnotation = member.getAnnotation( DynamicBoost.class );
+ if ( boostAnnotation == null ) {
+ return new DefaultBoostStrategy();
+ }
+ Class<? extends BoostStrategy> boostStrategyClass = boostAnnotation.impl();
+ BoostStrategy strategy;
+ try {
+ strategy = boostStrategyClass.newInstance();
+ }
+ catch ( Exception e ) {
+ throw new SearchException(
+ "Unable to instantiate boost strategy implementation: " + boostStrategyClass.getName()
+ );
+ }
+ return strategy;
+ }
private String buildEmbeddedPrefix(String prefix, IndexedEmbedded embeddedAnn, XProperty member) {
String localPrefix = prefix;
if ( ".".equals( embeddedAnn.prefix() ) ) {
@@ -544,13 +564,36 @@
}
protected Float getBoost(XClass element) {
+ float boost = 1.0f;
if ( element == null ) {
+ return boost;
+ }
+ Boost boostAnnotation = element.getAnnotation( Boost.class );
+ if( boostAnnotation != null ){
+ boost = boostAnnotation.value();
+ }
+ return boost;
+ }
+ protected BoostStrategy getDynamicBoost(XClass element) {
+ if ( element == null ) {
return null;
}
- Boost boost = element.getAnnotation( Boost.class );
- return boost != null ?
- boost.value() :
- null;
+ DynamicBoost boostAnnotation = element.getAnnotation( DynamicBoost.class );
+ if ( boostAnnotation == null ) {
+ return new DefaultBoostStrategy();
+ }
+
+ Class<? extends BoostStrategy> boostStrategyClass = boostAnnotation.impl();
+ BoostStrategy strategy;
+ try {
+ strategy = boostStrategyClass.newInstance();
+ }
+ catch ( Exception e ) {
+ throw new SearchException(
+ "Unable to instantiate boost strategy implementation: " + boostStrategyClass.getName()
+ );
+ }
+ return strategy;
}
//TODO could we use T instead of EntityClass?
@@ -673,19 +716,24 @@
}
/**
- * Wrapper class containing all the meta data extracted out of the entities.
+ * Wrapper class containing all the meta data extracted out of a single entity.
+ * All field/property related properties are kept in lists. Retrieving all metadata for a given
+ * property/field means accessing all the lists with the same index.
*/
protected static class PropertiesMetadata {
- public Float boost;
+ public float boost;
public Analyzer analyzer;
public Discriminator discriminator;
public XMember discriminatorGetter;
+ public BoostStrategy classBoostStrategy;
public final List<String> fieldNames = new ArrayList<String>();
public final List<XMember> fieldGetters = new ArrayList<XMember>();
public final List<FieldBridge> fieldBridges = new ArrayList<FieldBridge>();
public final List<Field.Store> fieldStore = new ArrayList<Field.Store>();
public final List<Field.Index> fieldIndex = new ArrayList<Field.Index>();
public final List<Float> fieldBoosts = new ArrayList<Float>();
+ public final List<BoostStrategy> dynamicFieldBoosts = new ArrayList<BoostStrategy>();
+
public final List<Field.TermVector> fieldTermVectors = new ArrayList<Field.TermVector>();
public final List<XMember> embeddedGetters = new ArrayList<XMember>();
public final List<PropertiesMetadata> embeddedPropertiesMetadata = new ArrayList<PropertiesMetadata>();
@@ -712,13 +760,19 @@
);
}
- protected LuceneOptions getFieldLuceneOptions(int i) {
+ protected LuceneOptions getFieldLuceneOptions(int i, Object value) {
LuceneOptions options;
options = new LuceneOptionsImpl(
fieldStore.get( i ),
- fieldIndex.get( i ), fieldTermVectors.get( i ), fieldBoosts.get( i )
+ fieldIndex.get( i ),
+ fieldTermVectors.get( i ),
+ fieldBoosts.get( i ) * dynamicFieldBoosts.get( i ).defineBoost( value )
);
return options;
}
+
+ protected float getClassBoost(Object value) {
+ return boost * classBoostStrategy.defineBoost( value );
+ }
}
}
\ No newline at end of file
Modified: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java 2010-05-11 01:48:17 UTC (rev 19464)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -197,6 +197,7 @@
propertiesMetadata.fieldTermVectors.add( getTermVector( TermVector.NO ) );
propertiesMetadata.fieldBridges.add( BridgeFactory.guessType( null, member, reflectionManager ) );
propertiesMetadata.fieldBoosts.add( getBoost( member, null ) );
+ propertiesMetadata.dynamicFieldBoosts.add( getDynamicBoost( member ) );
// property > entity analyzer (no field analyzer)
Analyzer analyzer = getAnalyzer( member, context );
if ( analyzer == null ) {
@@ -222,28 +223,33 @@
* @return the annotation used as document id or <code>null</code> if id annotation is specified on the property.
*/
private Annotation getIdAnnotation(XProperty member, InitContext context) {
+ Annotation idAnnotation = null;
+
// check for explicit DocumentId
- Annotation documentIdAnn = member.getAnnotation( DocumentId.class );
+ DocumentId documentIdAnn = member.getAnnotation( DocumentId.class );
if ( documentIdAnn != null ) {
explicitDocumentId = true;
- return documentIdAnn;
+ idAnnotation = documentIdAnn;
}
-
// check for JPA @Id
- if ( !explicitDocumentId && context.isJpaPresent() ) {
- Class idClass;
+ else if ( !explicitDocumentId && context.isJpaPresent() ) {
+ Annotation jpaId;
try {
- idClass = org.hibernate.util.ReflectHelper.classForName( "javax.persistence.Id", InitContext.class );
+ @SuppressWarnings("unchecked")
+ Class<? extends Annotation> jpaIdClass =
+ org.hibernate.annotations.common.util.ReflectHelper
+ .classForName( "javax.persistence.Id", InitContext.class );
+ jpaId = member.getAnnotation( jpaIdClass );
}
catch ( ClassNotFoundException e ) {
throw new SearchException( "Unable to load @Id.class even though it should be present ?!" );
}
- documentIdAnn = member.getAnnotation( idClass );
- if ( documentIdAnn != null ) {
+ if ( jpaId != null ) {
log.debug( "Found JPA id and using it as document id" );
+ idAnnotation = jpaId;
}
}
- return documentIdAnn;
+ return idAnnotation;
}
private ProvidedId findProvidedId(XClass clazz, ReflectionManager reflectionManager) {
@@ -353,10 +359,7 @@
Document doc = new Document();
final Class<?> entityType = Hibernate.getClass( instance );
- if ( metadata.boost != null ) {
- doc.setBoost( metadata.boost );
- }
-
+ doc.setBoost( metadata.getClassBoost( instance ) );
// add the class name of the entity to the document
Field classField =
new Field(
@@ -405,7 +408,7 @@
Object value = ReflectionHelper.getMemberValue( unproxiedInstance, member );
propertiesMetadata.fieldBridges.get( i ).set(
propertiesMetadata.fieldNames.get( i ), value, doc,
- propertiesMetadata.getFieldLuceneOptions( i )
+ propertiesMetadata.getFieldLuceneOptions( i, value )
);
}
Added: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomBoostStrategy.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomBoostStrategy.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomBoostStrategy.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,42 @@
+/* $Id: CustomBoostStrategy.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.query.boost;
+
+import org.hibernate.search.engine.BoostStrategy;
+
+/**
+ * Example for a custom <code>BoostStrategy</code> implementation.
+ *
+ * @author Sanne Grinovero
+ * @author Hardy Ferentschik
+ * @see org.hibernate.search.engine.BoostStrategy
+ */
+public class CustomBoostStrategy implements BoostStrategy {
+
+ public float defineBoost(Object value) {
+ DynamicBoostedDescriptionLibrary indexed = ( DynamicBoostedDescriptionLibrary ) value;
+ return indexed.getDynScore();
+ }
+}
Added: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomFieldBoostStrategy.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomFieldBoostStrategy.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/CustomFieldBoostStrategy.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,46 @@
+/* $Id: CustomFieldBoostStrategy.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.query.boost;
+
+import org.hibernate.search.engine.BoostStrategy;
+
+/**
+ * Example for a custom <code>BoostStrategy</code> implementation.
+ *
+ * @author Hardy Ferentschik
+ * @see org.hibernate.search.engine.BoostStrategy
+ */
+public class CustomFieldBoostStrategy implements BoostStrategy {
+
+ public float defineBoost(Object value) {
+ String name = ( String ) value;
+ if ( "foobar".equals( name ) ) {
+ return 3.0f;
+ }
+ else {
+ return 1.0f;
+ }
+ }
+}
Added: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostedDescriptionLibrary.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostedDescriptionLibrary.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostedDescriptionLibrary.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,87 @@
+/* $Id: DynamicBoostedDescriptionLibrary.java 19172 2010-04-06 09:17:34Z sannegrinovero $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.query.boost;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.annotations.DynamicBoost;
+
+/**
+ * Test entity using a custom <code>CustomBoostStrategy</code> to set
+ * the document boost as the dynScore field.
+ *
+ * @author Sanne Grinovero
+ * @author Hardy Ferentschik
+ */
+ at Entity
+ at Indexed
+ at DynamicBoost(impl = CustomBoostStrategy.class)
+ at Table(name="DynBoostLibrary")
+public class DynamicBoostedDescriptionLibrary {
+
+ private int id;
+ private float dynScore;
+ private String name;
+
+ public DynamicBoostedDescriptionLibrary() {
+ dynScore = 1.0f;
+ }
+
+ @Id
+ @GeneratedValue
+ @DocumentId
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public float getDynScore() {
+ return dynScore;
+ }
+
+ public void setDynScore(float dynScore) {
+ this.dynScore = dynScore;
+ }
+
+ @Field(store = Store.YES)
+ @DynamicBoost(impl = CustomFieldBoostStrategy.class)
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostingTest.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostingTest.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/DynamicBoostingTest.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -0,0 +1,132 @@
+/* $Id: DynamicBoostingTest.java 19002 2010-03-16 01:28:07Z hardy.ferentschik $
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.query.boost;
+
+import java.util.List;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.slf4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.ProjectionConstants;
+import org.hibernate.search.Search;
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.util.LoggerFactory;
+
+public class DynamicBoostingTest extends SearchTestCase {
+
+ private static final Logger log = LoggerFactory.make();
+
+ public void testDynamicBoosts() throws Exception {
+
+ Session session = openSession();
+ session.beginTransaction();
+
+ DynamicBoostedDescriptionLibrary lib1 = new DynamicBoostedDescriptionLibrary();
+ lib1.setName( "one" );
+ session.persist( lib1 );
+
+ DynamicBoostedDescriptionLibrary lib2 = new DynamicBoostedDescriptionLibrary();
+ lib2.setName( "two" );
+ session.persist( lib2 );
+
+ session.getTransaction().commit();
+ session.close();
+
+ float lib1Score = getScore( new TermQuery( new Term( "name", "one" ) ) );
+ float lib2Score = getScore( new TermQuery( new Term( "name", "two" ) ) );
+ assertEquals( "The scores should be equal", lib1Score, lib2Score );
+
+ // set dynamic score and reindex!
+ session = openSession();
+ session.beginTransaction();
+
+ session.refresh( lib2 );
+ lib2.setDynScore( 2.0f );
+
+ session.getTransaction().commit();
+ session.close();
+
+ lib1Score = getScore( new TermQuery( new Term( "name", "one" ) ) );
+ lib2Score = getScore( new TermQuery( new Term( "name", "two" ) ) );
+ assertTrue( "lib2score should be greater than lib1score", lib1Score < lib2Score );
+
+
+
+ lib1Score = getScore( new TermQuery( new Term( "name", "foobar" ) ) );
+ assertEquals( "lib1score should be 0 since term is not yet indexed.", 0.0f, lib1Score );
+
+ // index foobar
+ session = openSession();
+ session.beginTransaction();
+
+ session.refresh( lib1 );
+ lib1.setName( "foobar" );
+
+ session.getTransaction().commit();
+ session.close();
+
+ lib1Score = getScore( new TermQuery( new Term( "name", "foobar" ) ) );
+ lib2Score = getScore( new TermQuery( new Term( "name", "two" ) ) );
+ assertTrue( "lib1score should be greater than lib2score", lib1Score > lib2Score );
+ }
+
+ private float getScore(Query query) {
+ Session session = openSession();
+ Object[] queryResult;
+ float score;
+ try {
+ FullTextSession fullTextSession = Search.getFullTextSession( session );
+ List resultList = fullTextSession
+ .createFullTextQuery( query, DynamicBoostedDescriptionLibrary.class )
+ .setProjection( ProjectionConstants.SCORE, ProjectionConstants.EXPLANATION )
+ .setMaxResults( 1 )
+ .list();
+
+ if ( resultList.size() == 0 ) {
+ score = 0.0f;
+ }
+ else {
+ queryResult = ( Object[] ) resultList.get( 0 );
+ score = ( Float ) queryResult[0];
+ String explanation = queryResult[1].toString();
+ log.debug( "score: " + score + " explanation: " + explanation );
+ }
+ }
+ finally {
+ session.close();
+ }
+ return score;
+ }
+
+ protected Class<?>[] getMappings() {
+ return new Class[] {
+ DynamicBoostedDescriptionLibrary.class
+ };
+ }
+}
Modified: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/FieldBoostTest.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/FieldBoostTest.java 2010-05-11 01:48:17 UTC (rev 19464)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/query/boost/FieldBoostTest.java 2010-05-11 09:39:54 UTC (rev 19465)
@@ -12,12 +12,14 @@
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.util.LoggerFactory;
+import org.slf4j.Logger;
/**
* @author John Griffin
*/
public class FieldBoostTest extends SearchTestCase {
-
+ private static final Logger log = LoggerFactory.make();
public void testBoostedGetDesc() throws Exception {
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
buildBoostedGetIndex( fullTextSession );
@@ -33,14 +35,14 @@
BooleanQuery query = new BooleanQuery();
query.add( author, BooleanClause.Occur.SHOULD );
query.add( desc, BooleanClause.Occur.SHOULD );
- //System.out.println( query.toString() );
+ log.debug( query.toString() );
org.hibernate.search.FullTextQuery hibQuery =
fullTextSession.createFullTextQuery( query, BoostedGetDescriptionLibrary.class );
List results = hibQuery.list();
- //System.out.println( hibQuery.explain( 0 ) );
- //System.out.println( hibQuery.explain( 1 ) );
+ log.debug( hibQuery.explain( 0 ).toString() );
+ log.debug( hibQuery.explain( 1 ).toString() );
assertTrue(
"incorrect document returned",
@@ -71,7 +73,7 @@
BooleanQuery query = new BooleanQuery();
query.add( author, BooleanClause.Occur.SHOULD );
query.add( desc, BooleanClause.Occur.SHOULD );
- //System.out.println( query.toString() );
+ log.debug( query.toString() );
org.hibernate.search.FullTextQuery hibQuery =
fullTextSession.createFullTextQuery( query, BoostedFieldDescriptionLibrary.class );
@@ -109,14 +111,14 @@
BooleanQuery query = new BooleanQuery();
query.add( author, BooleanClause.Occur.SHOULD );
query.add( desc, BooleanClause.Occur.SHOULD );
- //System.out.println( query.toString() );
+ log.debug( query.toString() );
org.hibernate.search.FullTextQuery hibQuery =
fullTextSession.createFullTextQuery( query, BoostedDescriptionLibrary.class );
List results = hibQuery.list();
- //System.out.println( hibQuery.explain( 0 ) );
- //System.out.println( hibQuery.explain( 1 ) );
+ log.debug( hibQuery.explain( 0 ).toString() );
+ log.debug( hibQuery.explain( 1 ).toString() );
assertTrue(
"incorrect document returned",
More information about the hibernate-commits
mailing list