Author: epbernard
Date: 2009-04-08 19:11:28 -0400 (Wed, 08 Apr 2009)
New Revision: 16283
Added:
search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java
Modified:
search/trunk/src/main/java/org/hibernate/search/annotations/Similarity.java
search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java
Log:
HSEARCH-352 similarity and documentId support for programmatic mapping
Modified: search/trunk/src/main/java/org/hibernate/search/annotations/Similarity.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/annotations/Similarity.java 2009-04-08
16:58:37 UTC (rev 16282)
+++ search/trunk/src/main/java/org/hibernate/search/annotations/Similarity.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -11,10 +11,10 @@
@Target( ElementType.TYPE )
@Documented
/**
- * Specifies a similarity implementation to use.
+ * Specifies a similarity implementation to use for a given class
*
* @author Nick Vincent
*/
public @interface Similarity {
- public Class impl();
+ public Class<?> impl();
}
Copied: search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java (from
rev 16269, search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java)
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java
(rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -0,0 +1,54 @@
+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.Index;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.annotations.TermVector;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DocumentIdMapping {
+ private final SearchMapping mapping;
+ private final EntityDescriptor entity;
+ private final PropertyDescriptor property;
+ private final Map<String, Object> documentId = new HashMap<String,
Object>();
+
+ public DocumentIdMapping(PropertyDescriptor property, EntityDescriptor entity,
SearchMapping mapping) {
+ this.mapping = mapping;
+ this.entity = entity;
+ this.property = property;
+ property.setDocumentId( documentId );
+ }
+
+ public DocumentIdMapping name(String fieldName) {
+ documentId.put( "name", fieldName );
+ return this;
+ }
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+ 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 indexedClass(Class<?> entityType) {
+ return new EntityMapping(entityType, null, mapping);
+ }
+
+ public EntityMapping indexedClass(Class<?> entityType, String indexName) {
+ return new EntityMapping(entityType, indexName, mapping);
+ }
+
+}
\ No newline at end of file
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-04-08
16:58:37 UTC (rev 16282)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -11,6 +11,7 @@
private Class<?> entityType;
private Map<String, Object> indexed;
private Map<PropertyKey, PropertyDescriptor> properties = new
HashMap<PropertyKey, PropertyDescriptor>();
+ private Map<String, Object> similarity;
public Map<String, Object> getIndexed() {
return indexed;
@@ -38,7 +39,14 @@
return properties.get( new PropertyKey( name, type ) );
}
+ public void setSimilariy(Map<String, Object> similarity) {
+ this.similarity = similarity;
+ }
+ public Map<String, Object> getSimilarity() {
+ return similarity;
+ }
+
private static class PropertyKey {
private String name;
private ElementType type;
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java 2009-04-08
16:58:37 UTC (rev 16282)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -23,6 +23,13 @@
entity.setIndexed(indexed);
}
+ public EntityMapping similarity(Class<?> impl) {
+ Map<String, Object> similarity = new HashMap<String, Object>(1);
+ similarity.put( "impl", impl );
+ entity.setSimilariy(similarity);
+ return this;
+ }
+
public PropertyMapping property(String name, ElementType type) {
return new PropertyMapping(name, type, entity, mapping);
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-04-08
16:58:37 UTC (rev 16282)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -14,11 +14,16 @@
private ElementType type;
private String name;
private Collection<Map<String, Object>> fields = new
ArrayList<Map<String, Object>>();
+ private Map<String, Object> documentId;
public PropertyDescriptor(String name, ElementType type) {
this.name = name;
this.type = type;
}
+
+ public void setDocumentId(Map<String, Object> documentId) {
+ this.documentId = documentId;
+ }
public void addField(Map<String, Object> field) {
fields.add( field );
@@ -27,4 +32,8 @@
public Collection<Map<String, Object>> getFields() {
return fields;
}
+
+ public Map<String, Object> getDocumentId() {
+ return documentId;
+ }
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java 2009-04-08
16:58:37 UTC (rev 16282)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -18,6 +18,10 @@
property = entity.getProperty(name, type);
}
+ public DocumentIdMapping documentId() {
+ return new DocumentIdMapping( property, entity, mapping );
+ }
+
public FieldMapping field() {
return new FieldMapping(property, entity, mapping);
}
Modified:
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
===================================================================
---
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-04-08
16:58:37 UTC (rev 16282)
+++
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -32,6 +32,8 @@
import org.hibernate.search.annotations.AnalyzerDefs;
import org.hibernate.search.annotations.Boost;
import org.hibernate.search.annotations.FieldBridge;
+import org.hibernate.search.annotations.Similarity;
+import org.hibernate.search.annotations.DocumentId;
/**
* @author Emmanuel Bernard
@@ -224,6 +226,7 @@
final PropertyDescriptor property = entity.getPropertyDescriptor( propertyName,
elementType );
if (property != null) {
// property name overriding
+ createDocumentId( property );
createFields( property );
}
}
@@ -237,6 +240,17 @@
}
}
+ private void createDocumentId(PropertyDescriptor property) {
+ Map<String, Object> documentId = property.getDocumentId();
+ if (documentId != null) {
+ AnnotationDescriptor documentIdAnnotation = new AnnotationDescriptor(
DocumentId.class );
+ for ( Map.Entry<String, Object> entry : documentId.entrySet() ) {
+ documentIdAnnotation.setValue( entry.getKey(), entry.getValue() );
+ }
+ annotations.put( DocumentId.class, AnnotationFactory.create( documentIdAnnotation )
);
+ }
+ }
+
private void createFields(PropertyDescriptor property) {
final Collection<Map<String,Object>> fields = property.getFields();
List<org.hibernate.search.annotations.Field> fieldAnnotations =
@@ -291,6 +305,7 @@
annotations.put( Fields.class, AnnotationFactory.create( fieldsAnnotation ) );
}
+ //TODO add something around Similarity
private void createIndexed(EntityDescriptor entity) {
Class<? extends Annotation> annotationType = Indexed.class;
AnnotationDescriptor annotation = new AnnotationDescriptor( annotationType );
@@ -298,6 +313,14 @@
annotation.setValue( entry.getKey(), entry.getValue() );
}
annotations.put( annotationType, AnnotationFactory.create( annotation ) );
+
+ if ( entity.getSimilarity() != null ) {
+ annotation = new AnnotationDescriptor( Similarity.class );
+ for ( Map.Entry<String, Object> entry : entity.getSimilarity().entrySet() ) {
+ annotation.setValue( entry.getKey(), entry.getValue() );
+ }
+ annotations.put( annotationType, AnnotationFactory.create( annotation ) );
+ }
}
private void populateAnnotationArray() {
Modified: search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java 2009-04-08
16:58:37 UTC (rev 16282)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -12,12 +12,16 @@
public class Address {
@Id
@GeneratedValue
- private Long id;
+ private Long addressId;
private String street1;
private String street2;
@ManyToOne
private Country country;
+ public Long getAddressId() {
+ return addressId;
+ }
+
public String getStreet1() {
return street1;
}
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-04-08
16:58:37 UTC (rev 16282)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java 2009-04-08
23:11:28 UTC (rev 16283)
@@ -46,8 +46,13 @@
tx = s.beginTransaction();
QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
- org.apache.lucene.search.Query luceneQuery = parser.parse(
"street1:peachtree" );
- FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
"idx_street2", FullTextQuery.THIS );
+ org.apache.lucene.search.Query luceneQuery = parser.parse( "" +
address.getAddressId() );
+ System.out.println(luceneQuery.toString( ));
+ FullTextQuery query = s.createFullTextQuery( luceneQuery );
+ assertEquals( "documenId does not work properly", 1, query.getResultSize()
);
+
+ luceneQuery = parser.parse( "street1:peachtree" );
+ query = s.createFullTextQuery( luceneQuery ).setProjection( "idx_street2",
FullTextQuery.THIS );
assertEquals( "Not properly indexed", 1, query.getResultSize() );
Object[] firstResult = (Object[]) query.list().get( 0 );
assertEquals( "(a)Field.store not respected", "JBoss", firstResult[0]
);
@@ -160,6 +165,7 @@
.param( "maxGramSize", "3" )
.indexedClass( Address.class )
+ .property( "addressId", ElementType.FIELD ).documentId().name(
"id" )
.property( "street1", ElementType.FIELD )
.field()
.field().name( "street1_ngram" ).analyzer( "ngram" )