2012-08-14 34 views
9

在我用下面的線索引方法:如何使用Lucene的TermVector 4.0

Field contentsField = new Field("contents", new FileReader(f), Field.TermVector.YES); 

然而,在Lucene的4.0這個構造已被廢棄,new TextField應該用來代替new Field

TextField的問題是,它在其構造函數中不接受TermVector

有沒有一種方法包括術語向量在我的Lucene索引4.0與新構造?

感謝

回答

11

文本字段是誰需要長期無向量索引字段用戶一個方便的類。如果您需要條款向量,只需使用Field即可。它需要的代碼幾行,因爲你需要首先創建的FieldType一個實例,設置storeTermVectorstokenizer爲true,然後在Field構造函數中使用這個FieldType實例。

12

我有同樣的問題,所以我只是簡單地創建了自己的領域:

public class VecTextField extends Field { 

/* Indexed, tokenized, not stored. */ 
public static final FieldType TYPE_NOT_STORED = new FieldType(); 

/* Indexed, tokenized, stored. */ 
public static final FieldType TYPE_STORED = new FieldType(); 

static { 
    TYPE_NOT_STORED.setIndexed(true); 
    TYPE_NOT_STORED.setTokenized(true); 
    TYPE_NOT_STORED.setStoreTermVectors(true); 
    TYPE_NOT_STORED.setStoreTermVectorPositions(true); 
    TYPE_NOT_STORED.freeze(); 

    TYPE_STORED.setIndexed(true); 
    TYPE_STORED.setTokenized(true); 
    TYPE_STORED.setStored(true); 
    TYPE_STORED.setStoreTermVectors(true); 
    TYPE_STORED.setStoreTermVectorPositions(true); 
    TYPE_STORED.freeze(); 
} 

// TODO: add sugar for term vectors...? 

/** Creates a new TextField with Reader value. */ 
public VecTextField(String name, Reader reader, Store store) { 
    super(name, reader, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED); 
} 

/** Creates a new TextField with String value. */ 
public VecTextField(String name, String value, Store store) { 
    super(name, value, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED); 
} 

/** Creates a new un-stored TextField with TokenStream value. */ 
public VecTextField(String name, TokenStream stream) { 
    super(name, stream, TYPE_NOT_STORED); 
} 

}

希望這有助於