Author: epbernard
Date: 2008-03-20 15:00:58 -0400 (Thu, 20 Mar 2008)
New Revision: 14472
Added:
search/trunk/src/java/org/hibernate/search/annotations/TermVector.java
search/trunk/src/test/org/hibernate/search/test/query/ElectricalProperties.java
search/trunk/src/test/org/hibernate/search/test/query/TermVectorTest.java
Modified:
search/trunk/src/java/org/hibernate/search/annotations/ClassBridge.java
search/trunk/src/java/org/hibernate/search/annotations/Field.java
search/trunk/src/java/org/hibernate/search/bridge/FieldBridge.java
search/trunk/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java
search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
search/trunk/src/test/org/hibernate/search/test/bridge/CatDeptsFieldsClassBridge.java
search/trunk/src/test/org/hibernate/search/test/bridge/CatFieldsClassBridge.java
search/trunk/src/test/org/hibernate/search/test/bridge/DateSplitBridge.java
search/trunk/src/test/org/hibernate/search/test/bridge/EquipmentType.java
search/trunk/src/test/org/hibernate/search/test/bridge/TruncateFieldBridge.java
search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java
Log:
HSEARCH-141 add term vector
Modified: search/trunk/src/java/org/hibernate/search/annotations/ClassBridge.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/annotations/ClassBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++ search/trunk/src/java/org/hibernate/search/annotations/ClassBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -42,6 +42,12 @@
Index index() default Index.TOKENIZED;
/**
+ * Define term vector storage requirements,
+ * default to NO.
+ */
+ TermVector termVector() default TermVector.NO;
+
+ /**
* A float value of the amount of lucene defined
* boost to apply to a field.
*/
Modified: search/trunk/src/java/org/hibernate/search/annotations/Field.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/annotations/Field.java 2008-03-19 21:33:37
UTC (rev 14471)
+++ search/trunk/src/java/org/hibernate/search/annotations/Field.java 2008-03-20 19:00:58
UTC (rev 14472)
@@ -17,7 +17,7 @@
* @author Emmanuel Bernard
*/
@Retention( RetentionPolicy.RUNTIME )
-@Target( {ElementType.METHOD, ElementType.FIELD} )
+@Target( { ElementType.METHOD, ElementType.FIELD } )
@Documented
public @interface Field {
/**
@@ -38,6 +38,12 @@
Index index() default Index.TOKENIZED;
/**
+ * Define term vector storage requirements,
+ * default to NO.
+ */
+ TermVector termVector() default TermVector.NO;
+
+ /**
* Define an analyzer for the field, default to
* the inherited analyzer
*/
Added: search/trunk/src/java/org/hibernate/search/annotations/TermVector.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/annotations/TermVector.java
(rev 0)
+++ search/trunk/src/java/org/hibernate/search/annotations/TermVector.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -0,0 +1,31 @@
+//$
+package org.hibernate.search.annotations;
+
+/**
+ * Defines the term vector storing strategy
+ *
+ * @author John Griffin
+ */
+public enum TermVector {
+ /**
+ * Store term vectors.
+ */
+ YES,
+ /**
+ * Do not store term vectors.
+ */
+ NO,
+ /**
+ * Store the term vector + Token offset information
+ */
+ WITH_OFFSETS,
+ /**
+ * Store the term vector + token position information
+ */
+ WITH_POSITIONS,
+ /**
+ * Store the term vector + Token position and offset information
+ */
+ WITH_POSITION_OFFSETS
+}
+
Modified: search/trunk/src/java/org/hibernate/search/bridge/FieldBridge.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/bridge/FieldBridge.java 2008-03-19 21:33:37
UTC (rev 14471)
+++ search/trunk/src/java/org/hibernate/search/bridge/FieldBridge.java 2008-03-20 19:00:58
UTC (rev 14472)
@@ -19,5 +19,5 @@
* the parameters (<code>store</code>, <code>index</code>,
<code>boost</code>) if the
* <code>value</code> is not null
*/
- void set(String name, Object value, Document document, Field.Store store, Field.Index
index, Float boost);
+ void set(String name, Object value, Document document, Field.Store store, Field.Index
index, Field.TermVector termVector, Float boost);
}
Modified:
search/trunk/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java 2008-03-19
21:33:37 UTC (rev 14471)
+++
search/trunk/src/java/org/hibernate/search/bridge/String2FieldBridgeAdaptor.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -17,12 +17,12 @@
this.stringBridge = stringBridge;
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
String indexedString = stringBridge.objectToString( value );
//Do not add fields on empty strings, seems a sensible default in most situations
//TODO if Store, probably also save empty ones
if ( StringHelper.isNotEmpty( indexedString ) ) {
- Field field = new Field( name, indexedString, store, index );
+ Field field = new Field( name, indexedString, store, index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
}
Modified: search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java 2008-03-19
21:33:37 UTC (rev 14471)
+++ search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -28,13 +28,14 @@
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.search.SearchException;
import org.hibernate.search.annotations.Boost;
+import org.hibernate.search.annotations.ClassBridge;
+import org.hibernate.search.annotations.ClassBridges;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;
-import org.hibernate.search.annotations.ClassBridge;
-import org.hibernate.search.annotations.ClassBridges;
+import org.hibernate.search.annotations.TermVector;
import org.hibernate.search.backend.AddLuceneWork;
import org.hibernate.search.backend.DeleteLuceneWork;
import org.hibernate.search.backend.LuceneWork;
@@ -142,18 +143,18 @@
propertiesMetadata.analyzer = analyzer;
}
// Check for any ClassBridges annotation.
- ClassBridges classBridgesAnn = currClass.getAnnotation( ClassBridges.class);
- if (classBridgesAnn != null) {
+ ClassBridges classBridgesAnn = currClass.getAnnotation( ClassBridges.class );
+ if ( classBridgesAnn != null ) {
ClassBridge[] cbs = classBridgesAnn.value();
for (ClassBridge cb : cbs) {
- bindClassAnnotation(prefix, propertiesMetadata, cb);
+ bindClassAnnotation( prefix, propertiesMetadata, cb );
}
- }
+ }
// Check for any ClassBridge style of annotations.
- ClassBridge classBridgeAnn = currClass.getAnnotation(ClassBridge.class);
- if (classBridgeAnn != null) {
- bindClassAnnotation(prefix, propertiesMetadata, classBridgeAnn);
+ ClassBridge classBridgeAnn = currClass.getAnnotation( ClassBridge.class );
+ if ( classBridgeAnn != null ) {
+ bindClassAnnotation( prefix, propertiesMetadata, classBridgeAnn );
}
//rejecting non properties (ie regular methods) because the object is loaded from
Hibernate,
@@ -205,6 +206,7 @@
propertiesMetadata.fieldNames.add( fieldName );
propertiesMetadata.fieldStore.add( getStore( Store.YES ) );
propertiesMetadata.fieldIndex.add( getIndex( Index.UN_TOKENIZED ) );
+ propertiesMetadata.fieldTermVectors.add( getTermVector( TermVector.NO ) );
propertiesMetadata.fieldBridges.add( BridgeFactory.guessType( null, member,
reflectionManager ) );
// Field > property > entity analyzer
Analyzer analyzer = null; //no field analyzer
@@ -313,6 +315,7 @@
propertiesMetadata.classNames.add( fieldName );
propertiesMetadata.classStores.add( getStore( ann.store() ) );
propertiesMetadata.classIndexes.add( getIndex( ann.index() ) );
+ propertiesMetadata.classTermVectors.add( getTermVector( ann.termVector() ) );
propertiesMetadata.classBridges.add( BridgeFactory.extractType( ann ) );
propertiesMetadata.classBoosts.add( ann.boost().value() );
@@ -329,7 +332,9 @@
propertiesMetadata.fieldNames.add( fieldName );
propertiesMetadata.fieldStore.add( getStore( fieldAnn.store() ) );
propertiesMetadata.fieldIndex.add( getIndex( fieldAnn.index() ) );
+ propertiesMetadata.fieldTermVectors.add( getTermVector( fieldAnn.termVector() ) );
propertiesMetadata.fieldBridges.add( BridgeFactory.guessType( fieldAnn, member,
reflectionManager ) );
+
// Field > property > entity analyzer
Analyzer analyzer = getAnalyzer( fieldAnn.analyzer() );
if ( analyzer == null ) analyzer = getAnalyzer( member );
@@ -363,6 +368,23 @@
}
}
+ private Field.TermVector getTermVector(TermVector vector) {
+ switch ( vector ) {
+ case NO:
+ return Field.TermVector.NO;
+ case YES:
+ return Field.TermVector.YES;
+ case WITH_OFFSETS:
+ return Field.TermVector.WITH_OFFSETS;
+ case WITH_POSITIONS:
+ return Field.TermVector.WITH_POSITIONS;
+ case WITH_POSITION_OFFSETS:
+ return Field.TermVector.WITH_POSITIONS_OFFSETS;
+ default:
+ throw new AssertionFailure( "Unexpected TermVector: " + vector );
+ }
+ }
+
private Field.Index getIndex(Index index) {
switch ( index ) {
case NO:
@@ -403,9 +425,9 @@
for (LuceneWork luceneWork : queue) {
//any work on the same entity should be ignored
if ( luceneWork.getEntityClass() == entityClass
- ) {
+ ) {
Serializable currentId = luceneWork.getId();
- if ( currentId != null && currentId.equals( id ) ) { //find a way to use
Type.equals(x,y)
+ if ( currentId != null && currentId.equals( id ) ) { //find a way to use
Type.equals(x,y)
return;
}
//TODO do something to avoid multiple PURGE ALL and OPTIMIZE
@@ -518,9 +540,9 @@
}
{
Field classField =
- new Field( CLASS_FIELDNAME, instanceClass.getName(), Field.Store.YES,
Field.Index.UN_TOKENIZED );
+ new Field( CLASS_FIELDNAME, instanceClass.getName(), Field.Store.YES,
Field.Index.UN_TOKENIZED, Field.TermVector.NO );
doc.add( classField );
- idBridge.set( idKeywordName, id, doc, Field.Store.YES, Field.Index.UN_TOKENIZED,
idBoost );
+ idBridge.set( idKeywordName, id, doc, Field.Store.YES, Field.Index.UN_TOKENIZED,
Field.TermVector.NO, idBoost );
}
buildDocumentFields( instance, doc, rootPropertiesMetadata );
return doc;
@@ -533,19 +555,24 @@
for (int i = 0; i < propertiesMetadata.classBridges.size(); i++) {
FieldBridge fb = propertiesMetadata.classBridges.get( i );
- fb.set( propertiesMetadata.classNames.get(i),
+ fb.set( propertiesMetadata.classNames.get( i ),
unproxiedInstance,
doc,
- propertiesMetadata.classStores.get(i),
- propertiesMetadata.classIndexes.get(i),
- propertiesMetadata.classBoosts.get(i));
+ propertiesMetadata.classStores.get( i ),
+ propertiesMetadata.classIndexes.get( i ),
+ propertiesMetadata.classTermVectors.get( i ),
+ propertiesMetadata.classBoosts.get( i ) );
}
for (int i = 0; i < propertiesMetadata.fieldNames.size(); i++) {
XMember member = propertiesMetadata.fieldGetters.get( i );
Object value = getMemberValue( unproxiedInstance, member );
propertiesMetadata.fieldBridges.get( i ).set(
- propertiesMetadata.fieldNames.get( i ), value, doc,
propertiesMetadata.fieldStore.get( i ),
- propertiesMetadata.fieldIndex.get( i ), getBoost( member )
+ propertiesMetadata.fieldNames.get( i ),
+ value, doc,
+ propertiesMetadata.fieldStore.get( i ),
+ propertiesMetadata.fieldIndex.get( i ),
+ propertiesMetadata.fieldTermVectors.get( i ),
+ getBoost( member )
);
}
for (int i = 0; i < propertiesMetadata.embeddedGetters.size(); i++) {
@@ -726,6 +753,7 @@
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<Field.TermVector> fieldTermVectors = new
ArrayList<Field.TermVector>();
public final List<XMember> embeddedGetters = new ArrayList<XMember>();
public final List<PropertiesMetadata> embeddedPropertiesMetadata = new
ArrayList<PropertiesMetadata>();
public final List<Container> embeddedContainers = new
ArrayList<Container>();
@@ -734,6 +762,7 @@
public final List<Field.Store> classStores = new ArrayList<Field.Store>();
public final List<Field.Index> classIndexes = new
ArrayList<Field.Index>();
public final List<FieldBridge> classBridges = new
ArrayList<FieldBridge>();
+ public final List<Field.TermVector> classTermVectors = new
ArrayList<Field.TermVector>();
public final List<Float> classBoosts = new ArrayList<Float>();
public enum Container {
Modified:
search/trunk/src/test/org/hibernate/search/test/bridge/CatDeptsFieldsClassBridge.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/bridge/CatDeptsFieldsClassBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++
search/trunk/src/test/org/hibernate/search/test/bridge/CatDeptsFieldsClassBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -2,10 +2,10 @@
import java.util.Map;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.ParameterizedBridge;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
/**
* @author John Griffin
@@ -18,7 +18,7 @@
this.sepChar = (String) parameters.get( "sepChar" );
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
// In this particular class the name of the new field was passed
// from the name field of the ClassBridge Annotation. This is not
// a requirement. It just works that way in this instance. The
@@ -33,7 +33,7 @@
fieldValue2 = "";
}
String fieldValue = fieldValue1 + sepChar + fieldValue2;
- Field field = new Field( name, fieldValue, store, index );
+ Field field = new Field( name, fieldValue, store, index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
}
Modified:
search/trunk/src/test/org/hibernate/search/test/bridge/CatFieldsClassBridge.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/bridge/CatFieldsClassBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++
search/trunk/src/test/org/hibernate/search/test/bridge/CatFieldsClassBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -2,12 +2,10 @@
import java.util.Map;
-import org.hibernate.search.bridge.StringBridge;
-import org.hibernate.search.bridge.ParameterizedBridge;
-import org.hibernate.search.bridge.FieldBridge;
-import org.hibernate.util.StringHelper;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
+import org.hibernate.search.bridge.FieldBridge;
+import org.hibernate.search.bridge.ParameterizedBridge;
/**
* @author John Griffin
@@ -20,7 +18,7 @@
this.sepChar = (String) parameters.get( "sepChar" );
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
// In this particular class the name of the new field was passed
// from the name field of the ClassBridge Annotation. This is not
// a requirement. It just works that way in this instance. The
@@ -35,8 +33,8 @@
fieldValue2 = "";
}
String fieldValue = fieldValue1 + sepChar + fieldValue2;
- Field field = new Field( name, fieldValue, store, index );
+ Field field = new Field( name, fieldValue, store, index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
}
-}
\ No newline at end of file
+}
Modified: search/trunk/src/test/org/hibernate/search/test/bridge/DateSplitBridge.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/bridge/DateSplitBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++ search/trunk/src/test/org/hibernate/search/test/bridge/DateSplitBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -1,14 +1,14 @@
//$Id$
package org.hibernate.search.test.bridge;
+import java.util.Calendar;
import java.util.Date;
-import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
-import org.hibernate.search.bridge.FieldBridge;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
+import org.hibernate.search.bridge.FieldBridge;
/**
* Store the date in 3 different field year, month, day
@@ -18,9 +18,9 @@
* @author Emmanuel Bernard
*/
public class DateSplitBridge implements FieldBridge {
- private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
+ private final static TimeZone GMT = TimeZone.getTimeZone( "GMT" );
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
Date date = (Date) value;
Calendar cal = GregorianCalendar.getInstance( GMT );
cal.setTime( date );
@@ -28,15 +28,15 @@
int month = cal.get( Calendar.MONTH ) + 1;
int day = cal.get( Calendar.DAY_OF_MONTH );
//set year
- Field field = new Field( name + ".year", String.valueOf(year), store, index
);
+ Field field = new Field( name + ".year", String.valueOf( year ), store,
index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
//set month and pad it if needed
- field = new Field( name + ".month", month < 10 ? "0" :
"" + String.valueOf(month), store, index );
+ field = new Field( name + ".month", month < 10 ? "0" :
"" + String.valueOf( month ), store, index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
//set day and pad it if needed
- field = new Field( name + ".day", day < 10 ? "0" : ""
+ String.valueOf(day), store, index );
+ field = new Field( name + ".day", day < 10 ? "0" : ""
+ String.valueOf( day ), store, index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
}
Modified: search/trunk/src/test/org/hibernate/search/test/bridge/EquipmentType.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/bridge/EquipmentType.java 2008-03-19
21:33:37 UTC (rev 14471)
+++ search/trunk/src/test/org/hibernate/search/test/bridge/EquipmentType.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -10,7 +10,7 @@
/**
* @author John Griffin
*/
-public class EquipmentType implements FieldBridge, ParameterizedBridge {
+public class EquipmentType implements FieldBridge, ParameterizedBridge {
private Map equips;
public void setParameterValues(Map parameters) {
@@ -18,7 +18,7 @@
this.equips = parameters;
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
// In this particular class the name of the new field was passed
// from the name field of the ClassBridge Annotation. This is not
// a requirement. It just works that way in this instance. The
@@ -31,8 +31,8 @@
fieldValue1 = "";
}
else {
- String fieldValue = (String)equips.get( fieldValue1);
- field = new Field( name, fieldValue, store, index );
+ String fieldValue = (String) equips.get( fieldValue1 );
+ field = new Field( name, fieldValue, store, index, termVector );
if ( boost != null ) field.setBoost( boost );
}
document.add( field );
Modified: search/trunk/src/test/org/hibernate/search/test/bridge/TruncateFieldBridge.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/bridge/TruncateFieldBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++
search/trunk/src/test/org/hibernate/search/test/bridge/TruncateFieldBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -1,27 +1,27 @@
//$Id$
package org.hibernate.search.test.bridge;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.util.StringHelper;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
/**
* @author Emmanuel Bernard
*/
public class TruncateFieldBridge implements FieldBridge {
- public Object get(String name, Document document) {
+ public Object get(String name, Document document) {
Field field = document.getField( name );
return field.stringValue();
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
- String indexedString = (String) value;
- //Do not add fields on empty strings, seems a sensible default in most
situations
- if ( StringHelper.isNotEmpty( indexedString ) ) {
- Field field = new Field(name, indexedString.substring(0,
indexedString.length() / 2), store, index);
- if (boost != null) field.setBoost( boost );
- document.add( field );
- }
- }
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
+ String indexedString = (String) value;
+ //Do not add fields on empty strings, seems a sensible default in most situations
+ if ( StringHelper.isNotEmpty( indexedString ) ) {
+ Field field = new Field( name, indexedString.substring( 0, indexedString.length() / 2
), store, index, termVector );
+ if ( boost != null ) field.setBoost( boost );
+ document.add( field );
+ }
+ }
}
Modified: search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java 2008-03-19
21:33:37 UTC (rev 14471)
+++ search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -26,12 +26,12 @@
return sb.toString();
}
- public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Field.TermVector termVector, Float boost) {
PersonPK id = (PersonPK) value;
- Field field = new Field( name + ".firstName", id.getFirstName(), store, index
);
+ Field field = new Field( name + ".firstName", id.getFirstName(), store,
index, termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
- field = new Field( name + ".lastName", id.getLastName(), store, index );
+ field = new Field( name + ".lastName", id.getLastName(), store, index,
termVector );
if ( boost != null ) field.setBoost( boost );
document.add( field );
}
Added: search/trunk/src/test/org/hibernate/search/test/query/ElectricalProperties.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/query/ElectricalProperties.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/query/ElectricalProperties.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -0,0 +1,49 @@
+package org.hibernate.search.test.query;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.annotations.TermVector;
+
+/**
+ * @author John Griffin
+ */
+@Entity
+@Indexed
+public class ElectricalProperties {
+ private int id;
+ private String content;
+
+ public ElectricalProperties() {
+
+ }
+
+ public ElectricalProperties(int id, String content) {
+ this.id = id;
+ this.content = content;
+ }
+
+ @Field( index = Index.TOKENIZED, store = Store.YES, termVector =
TermVector.WITH_POSITION_OFFSETS )
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ @Id
+ @DocumentId
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/query/TermVectorTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/query/TermVectorTest.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/query/TermVectorTest.java 2008-03-20
19:00:58 UTC (rev 14472)
@@ -0,0 +1,116 @@
+package org.hibernate.search.test.query;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.TermPositionVector;
+import org.apache.lucene.index.TermVectorOffsetInfo;
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.SearchFactory;
+import org.hibernate.search.reader.ReaderProvider;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.test.SearchTestCase;
+
+/**
+ * @author John Griffin
+ */
+public class TermVectorTest extends SearchTestCase {
+ private static Log log = LogFactory.getLog( TermVectorTest.class );
+
+ public void testPositionOffsets() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ createIndex( s );
+
+ s.clear();
+ Transaction tx = s.beginTransaction();
+
+ // Here's how to get a reader from a FullTextSession
+ SearchFactory searchFactory = s.getSearchFactory();
+ DirectoryProvider provider = searchFactory.getDirectoryProviders(
ElectricalProperties.class )[0];
+ ReaderProvider readerProvider = searchFactory.getReaderProvider();
+ IndexReader reader = readerProvider.openReader( provider );
+
+ /**
+ * Since there are so many combinations of results here, rather
+ * than try to do assertions, this test prints out all the results
+ * found from the three ElectricalProperties entities. This will
+ * do a better job of demonstrating exactly what the result are. - J.G.
+ */
+ ///TODO: try and find some ways to assert it. Nobody reads the results. I've added
+ for (int x = 0; x < 3; x++) {
+ TermPositionVector vector = (TermPositionVector) reader.getTermFreqVector( x,
"content" );
+ assertNotNull( vector );
+ String[] terms = vector.getTerms();
+ int[] freqs = vector.getTermFrequencies();
+
+ for (int y = 0; y < vector.size(); y++) {
+ log.info( "doc# =>" + x );
+ log.info( " term => " + terms[y] );
+ log.info( " freq => " + freqs[y] );
+
+ int[] positions = vector.getTermPositions( y );
+ TermVectorOffsetInfo[] offsets = vector.getOffsets( y );
+ for (int z = 0; z < positions.length; z++) {
+ log.info( " position => " + positions[z] );
+ log.info( " starting offset => " + offsets[z].getStartOffset() );
+ log.info( " ending offset => " + offsets[z].getEndOffset() );
+ }
+ log.info( "---------------" );
+ }
+ }
+
+ //cleanup
+ for (Object element : s.createQuery( "from " +
ElectricalProperties.class.getName() ).list())
+ s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
+ public void testNoTermVector() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ Employee e1 = new Employee( 1000, "Griffin", "ITech" );
+ s.save( e1 );
+ tx.commit();
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ // Here's how to get a reader from a FullTextSession
+ SearchFactory searchFactory = s.getSearchFactory();
+ DirectoryProvider provider = searchFactory.getDirectoryProviders( Employee.class )[0];
+ ReaderProvider readerProvider = searchFactory.getReaderProvider();
+ IndexReader reader = readerProvider.openReader( provider );
+
+ TermPositionVector vector = (TermPositionVector) reader.getTermFreqVector( 0,
"dept" );
+ assertNull( "should not find a term position vector", vector );
+
+ //cleanup
+ for (Object element : s.createQuery( "from " +
ElectricalProperties.class.getName() ).list())
+ s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
+ private void createIndex(FullTextSession s) {
+ Transaction tx = s.beginTransaction();
+ ElectricalProperties e1 = new ElectricalProperties( 1000, "Electrical Engineers
measure Electrical Properties" );
+ s.save( e1 );
+ ElectricalProperties e2 = new ElectricalProperties( 1001, "Electrical Properties
are interesting" );
+ s.save( e2 );
+ ElectricalProperties e3 = new ElectricalProperties( 1002, "Electrical Properties
are measurable properties" );
+ s.save( e3 );
+
+ tx.commit();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ ElectricalProperties.class,
+ Employee.class
+ };
+ }
+}