Author: epbernard
Date: 2009-12-18 06:26:07 -0500 (Fri, 18 Dec 2009)
New Revision: 18266
Added:
search/trunk/src/main/java/org/hibernate/search/cfg/DynamicEntityBoostMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/DynamicFieldBoostMapping.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBoostStrategy.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomFieldBoostStrategy.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/DynamicBoostedDescLibrary.java
Modified:
search/trunk/src/main/docbook/en-US/modules/mapping.xml
search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticSearchMappingFactory.java
Log:
HSEARCH-411 hopefully the last feature :)
Modified: search/trunk/src/main/docbook/en-US/modules/mapping.xml
===================================================================
--- search/trunk/src/main/docbook/en-US/modules/mapping.xml 2009-12-18 11:02:25 UTC (rev
18265)
+++ search/trunk/src/main/docbook/en-US/modules/mapping.xml 2009-12-18 11:26:07 UTC (rev
18266)
@@ -1548,8 +1548,8 @@
<classname>SearchMapping</classname> object. This object is passed to
Hibernate Search via a property set to the <classname>Configuration
</classname>object. The property key is
- <literal>hibernate.search.mapping_model</literal> or it's tpye-safe
representation
- <classname>Environment.MODEL_MAPPING</classname>.</para>
+ <literal>hibernate.search.model_mapping</literal> or it's type-safe
+ representation
<classname>Environment.MODEL_MAPPING</classname>.</para>
<programlisting>SearchMapping mapping = new SearchMapping();
[...]
@@ -2158,8 +2158,8 @@
<section>
<title>Mapping class bridge</title>
- <para>You can define class bridges on entities programmatically.
- This is shown in the next example:</para>
+ <para>You can define class bridges on entities programmatically. This is
+ shown in the next example:</para>
<example>
<title>Defining class briges using API</title>
@@ -2214,8 +2214,63 @@
....
}</programlisting>
- </example> </para>
+ </example></para>
</example>
</section>
+
+ <section>
+ <title>Mapping dynamic boost</title>
+
+ <para>You can apply a dynamic boost
+ factor on either a field or a whole entity: </para>
+
+ <example>
+ <title>DynamicBoost mapping using programmatic model</title>
+
+ <programlisting>SearchMapping mapping = new SearchMapping();
+
+mapping
+ .entity(DynamicBoostedDescLibrary.class)
+ .indexed()
+ <emphasis>.dynamicBoost(CustomBoostStrategy.class)</emphasis>
+ .property("libraryId", ElementType.FIELD)
+ .documentId().name("id")
+ .property("name", ElementType.FIELD)
+ .field()
+ .store(Store.YES)
+ <emphasis>.dynamicBoost(CustomFieldBoostStrategy.class)</emphasis>;
+
+cfg.getProperties().put( "hibernate.search.model_mapping", mapping
);</programlisting>
+
+ <para>The next example shows the equivalent mapping using the
+ <classname>@DynamicBoost</classname> annotation: <example>
+ <title>Using the @DynamicBoost </title>
+
+ <programlisting>@Entity
+@Indexed
+@DynamicBoost(impl = CustomBoostStrategy.class)
+public class DynamicBoostedDescriptionLibrary {
+
+ @Id
+ @GeneratedValue
+ @DocumentId
+ private int id;
+
+ private float dynScore;
+
+ @Field(store = Store.YES)
+ @DynamicBoost(impl = CustomFieldBoostStrategy.class)
+ private String name;
+
+ public DynamicBoostedDescriptionLibrary() {
+ dynScore = 1.0f;
+ }
+
+ .......
+
+}</programlisting>
+ </example></para>
+ </example>
+ </section>
</section>
</chapter>
Added: search/trunk/src/main/java/org/hibernate/search/cfg/DynamicEntityBoostMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/DynamicEntityBoostMapping.java
(rev 0)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/DynamicEntityBoostMapping.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -0,0 +1,47 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.engine.BoostStrategy;
+
+public class DynamicEntityBoostMapping {
+
+ private final SearchMapping mapping;
+ private final Map<String,Object> dynamicEntityBoost;
+ private final EntityDescriptor entity;
+ private final EntityMapping entityMapping;
+
+ public DynamicEntityBoostMapping(SearchMapping mapping, Class<? extends
BoostStrategy> impl, EntityDescriptor entity, EntityMapping entityMapping) {
+ this.mapping = mapping;
+ this.entity = entity;
+ this.entityMapping = entityMapping;
+ this.dynamicEntityBoost = new HashMap<String, Object>();
+ this.entity.setDynamicEntityBoost(dynamicEntityBoost);
+ this.dynamicEntityBoost.put("impl", impl);
+
+ }
+
+ public FullTextFilterDefMapping fullTextFilterDef(String name, Class<?> impl) {
+ return new FullTextFilterDefMapping(mapping,name, impl);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+ public ClassBridgeMapping classBridge(Class<?> impl) {
+ return new ClassBridgeMapping(mapping, entity, impl, entityMapping);
+ }
+
+}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/DynamicFieldBoostMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/DynamicFieldBoostMapping.java
(rev 0)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/DynamicFieldBoostMapping.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -0,0 +1,59 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.annotations.Resolution;
+import org.hibernate.search.engine.BoostStrategy;
+
+public class DynamicFieldBoostMapping {
+
+ private final SearchMapping mapping;
+ private final EntityDescriptor entity;
+ private final Map<String,Object> dynamicFieldBoost;
+ private final PropertyDescriptor property;
+
+ public DynamicFieldBoostMapping(SearchMapping mapping, Class<? extends
BoostStrategy> impl, PropertyDescriptor property, EntityDescriptor entity) {
+ this.mapping = mapping;
+ this.property = property;
+ this.entity = entity;
+ dynamicFieldBoost = new HashMap<String, Object>();
+ this.property.setDynamicFieldBoost(dynamicFieldBoost);
+ dynamicFieldBoost.put("impl", impl);
+ }
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public DateBridgeMapping dateBridge(Resolution resolution) {
+ return new DateBridgeMapping(mapping, entity, property, resolution);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+ public CalendarBridgeMapping calendarBridge(Resolution resolution) {
+ return new CalendarBridgeMapping(mapping,entity,property, resolution);
+ }
+
+ public IndexEmbeddedMapping indexEmbedded() {
+ return new IndexEmbeddedMapping(mapping,property,entity);
+ }
+
+ public ContainedInMapping containedIn() {
+ return new ContainedInMapping(mapping, property, entity);
+ }
+
+}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-12-18
11:02:25 UTC (rev 18265)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -43,6 +43,7 @@
private Set<Map<String, Object>> fullTextFilterDefs = new
HashSet<Map<String, Object>>();
private Map<String,Object> providedId;
private Set<Map<String,Object>> classBridges = new
HashSet<Map<String,Object>>();
+ private Map<String, Object> dynamicEntityBoost;
public Map<String, Object> getIndexed() {
return indexed;
@@ -119,6 +120,14 @@
return this.providedId;
}
+ public void setDynamicEntityBoost(Map<String, Object> dynamicEntityBoost) {
+ this.dynamicEntityBoost = dynamicEntityBoost;
+ }
+
+ public Map<String, Object> getDynamicEntityBoost() {
+ return this.dynamicEntityBoost;
+ }
+
private static class PropertyKey {
private String name;
private ElementType type;
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java 2009-12-18
11:02:25 UTC (rev 18265)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -29,11 +29,11 @@
import java.util.Map;
import org.apache.solr.analysis.TokenizerFactory;
-
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TermVector;
+import org.hibernate.search.engine.BoostStrategy;
/**
* @author Emmanuel Bernard
@@ -120,4 +120,8 @@
return new CalendarBridgeMapping(mapping,entity,property, resolution);
}
+ public DynamicFieldBoostMapping dynamicBoost(Class<? extends BoostStrategy> impl)
{
+ return new DynamicFieldBoostMapping(mapping, impl, property, entity);
+ }
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java 2009-12-18
11:02:25 UTC (rev 18265)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -30,6 +30,7 @@
import org.apache.solr.analysis.TokenizerFactory;
import org.hibernate.search.analyzer.Discriminator;
+import org.hibernate.search.engine.BoostStrategy;
public class IndexedMapping {
@@ -92,5 +93,9 @@
public ProvidedIdMapping providedId() {
return new ProvidedIdMapping(mapping,entity);
}
+
+ public DynamicEntityBoostMapping dynamicBoost(Class<? extends BoostStrategy> impl)
{
+ return new DynamicEntityBoostMapping(mapping, impl, entity, entityMapping);
+ }
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-12-18
11:02:25 UTC (rev 18265)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -44,6 +44,7 @@
private Map<String, Object> documentId;
private Map<String, Object> analyzerDiscriminator;
+ private Map<String, Object> dynamicFieldBoost;
public PropertyDescriptor(String name, ElementType type) {
this.name = name;
@@ -105,4 +106,12 @@
this.containedIn = containedIn;
}
+ public void setDynamicFieldBoost(Map<String, Object> dynamicFieldBoost) {
+ this.dynamicFieldBoost = dynamicFieldBoost;
+ }
+
+ public Map<String,Object> getDynamicFieldBoost() {
+ return this.dynamicFieldBoost;
+ }
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java 2009-12-18
11:02:25 UTC (rev 18265)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Set;
+import org.apache.lucene.analysis.Tokenizer;
import org.apache.solr.analysis.TokenizerFactory;
/**
Modified:
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
===================================================================
---
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-12-18
11:02:25 UTC (rev 18265)
+++
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -56,6 +56,7 @@
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.DynamicBoost;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Fields;
import org.hibernate.search.annotations.FullTextFilterDef;
@@ -314,6 +315,17 @@
populateAnnotationArray();
}
}
+
+ private void createDynamicFieldBoost(PropertyDescriptor property) {
+ if (property.getDynamicFieldBoost() != null) {
+ AnnotationDescriptor dynamicBoostAnn = new AnnotationDescriptor( DynamicBoost.class
);
+ Set<Entry<String,Object>> entrySet =
property.getDynamicFieldBoost().entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ dynamicBoostAnn.setValue(entry.getKey(), entry.getValue());
+ }
+ annotations.put(DynamicBoost.class, AnnotationFactory.create( dynamicBoostAnn ));
+ }
+ }
private void createDateBridge(PropertyDescriptor property) {
Map<String, Object> map = property.getDateBridge();
@@ -405,10 +417,12 @@
final org.hibernate.search.annotations.Field[] fieldArray =
new org.hibernate.search.annotations.Field[fieldAnnotations.size()];
final org.hibernate.search.annotations.Field[] fieldAsArray =
fieldAnnotations.toArray( fieldArray );
+
fieldsAnnotation.setValue( "value", fieldAsArray );
annotations.put( Fields.class, AnnotationFactory.create( fieldsAnnotation ) );
createDateBridge(property);
createCalendarBridge(property);
+ createDynamicFieldBoost(property);
}
@@ -486,6 +500,15 @@
annotations.put(ClassBridges.class, AnnotationFactory.create( classBridgesAnn ));
}
+ if (entity.getDynamicEntityBoost() != null) {
+ AnnotationDescriptor dynamicBoostAnn = new AnnotationDescriptor( DynamicBoost.class
);
+ Set<Entry<String,Object>> entrySet =
entity.getDynamicEntityBoost().entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ dynamicBoostAnn.setValue(entry.getKey(), entry.getValue());
+ }
+ annotations.put(DynamicBoost.class, AnnotationFactory.create( dynamicBoostAnn ));
+ }
+
}
private ClassBridge[] createClassBridgesDefArray(Set<Map<String, Object>>
classBridgeDefs) {
Added:
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBoostStrategy.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBoostStrategy.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBoostStrategy.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -0,0 +1,42 @@
+/* $Id: CustomBoostStrategy.java 17630 2009-10-06 13:38:43Z 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.configuration;
+
+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) {
+ DynamicBoostedDescLibrary indexed = ( DynamicBoostedDescLibrary ) value;
+ return indexed.getDynScore();
+ }
+}
Added:
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomFieldBoostStrategy.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomFieldBoostStrategy.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomFieldBoostStrategy.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -0,0 +1,46 @@
+/* $Id: CustomFieldBoostStrategy.java 17630 2009-10-06 13:38:43Z 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.configuration;
+
+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/trunk/src/test/java/org/hibernate/search/test/configuration/DynamicBoostedDescLibrary.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/DynamicBoostedDescLibrary.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/DynamicBoostedDescLibrary.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -0,0 +1,75 @@
+/* $Id: DynamicBoostedDescriptionLibrary.java 17630 2009-10-06 13:38:43Z 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.configuration;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * Test entity using a custom <code>CustomBoostStrategy</code> to set
+ * the document boost as the dynScore field.
+ *
+ * @author Sanne Grinovero
+ * @author Hardy Ferentschik
+ */
+@Entity
+public class DynamicBoostedDescLibrary {
+
+ @Id
+ @GeneratedValue
+ private int libraryId;
+ private float dynScore;
+ private String name;
+
+ public DynamicBoostedDescLibrary() {
+ dynScore = 1.0f;
+ }
+
+
+ public int getLibraryId() {
+ return libraryId;
+ }
+
+ public void setLibraryId(int id) {
+ this.libraryId = id;
+ }
+
+ public float getDynScore() {
+ return dynScore;
+ }
+
+ public void setDynScore(float dynScore) {
+ this.dynScore = dynScore;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java 2009-12-18
11:02:25 UTC (rev 18265)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -33,32 +33,29 @@
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
-import org.apache.solr.analysis.EnglishPorterFilterFactory;
-import org.apache.solr.analysis.GermanStemFilterFactory;
import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.NGramFilterFactory;
import org.apache.solr.analysis.SnowballPorterFilterFactory;
import org.apache.solr.analysis.StandardTokenizerFactory;
+import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
+import org.hibernate.search.Environment;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
+import org.hibernate.search.ProjectionConstants;
import org.hibernate.search.Search;
-import org.hibernate.search.Environment;
-import org.hibernate.search.annotations.FilterCacheModeType;
import org.hibernate.search.annotations.Index;
-import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
-import org.hibernate.search.bridge.builtin.LongBridge;
-import org.hibernate.search.cfg.ConcatStringBridge;
import org.hibernate.search.cfg.SearchMapping;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.store.DirectoryProvider;
@@ -576,6 +573,89 @@
s.close();
}
+
+ public void testDynamicBoosts() throws Exception {
+
+ Session session = openSession();
+ session.beginTransaction();
+
+ DynamicBoostedDescLibrary lib1 = new DynamicBoostedDescLibrary();
+ lib1.setName( "one" );
+ session.persist( lib1 );
+
+ DynamicBoostedDescLibrary lib2 = new DynamicBoostedDescLibrary();
+ 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, DynamicBoostedDescLibrary.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;
+ }
+
private int nbrOfMatchingResults(String field, String token, FullTextSession s) throws
ParseException {
QueryParser parser = new QueryParser( field, new StandardAnalyzer() );
org.apache.lucene.search.Query luceneQuery = parser.parse( token );
@@ -636,8 +716,6 @@
protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.getProperties().put( Environment.MODEL_MAPPING,
ProgrammaticSearchMappingFactory.class.getName() );
- //cfg.setProperty( "hibernate.search.default.directory_provider",
FSDirectoryProvider.class.getName() );
-
}
public void NotUseddefineMapping() {
@@ -685,6 +763,7 @@
ProductCatalog.class,
Item.class,
Departments.class,
+ DynamicBoostedDescLibrary.class
};
}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticSearchMappingFactory.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticSearchMappingFactory.java 2009-12-18
11:02:25 UTC (rev 18265)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticSearchMappingFactory.java 2009-12-18
11:26:07 UTC (rev 18266)
@@ -79,6 +79,14 @@
.field().name("description").analyzer("en").index(Index.TOKENIZED).store(Store.YES)
.property("productCatalog", ElementType.FIELD)
.containedIn()
+ .entity(DynamicBoostedDescLibrary.class)
+ .indexed()
+ .dynamicBoost(CustomBoostStrategy.class)
+ .property("libraryId", ElementType.FIELD)
+ .documentId().name("id")
+ .property("name", ElementType.FIELD)
+ .field().store(Store.YES)
+ .dynamicBoost(CustomFieldBoostStrategy.class)
.entity(Departments.class)
.classBridge(CatDeptsFieldsClassBridge.class)
.name("branchnetwork")