2012-08-14 11 views
9

Nel metodo di indicizzazione che uso la seguente riga:Come utilizzare TermVector Lucene 4.0

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

Tuttavia, in Lucene 4,0 questo costruttore è deprecato e new TextField dovrebbe essere usato al posto di new Field.

Ma il problema con TextField è che non accetta TermVector nei suoi costruttori.

C'è un modo per includere Term Vector nella mia indicizzazione in Lucene 4.0 con i nuovi costruttori?

Grazie

risposta

11

TextField è una classe convenienza per gli utenti che hanno bisogno di campi indicizzati senza vettori termine. Se hai bisogno di vettori di termini, usa semplicemente uno Field. Sono necessarie poche altre righe di codice poiché è necessario creare prima un'istanza di FieldType, impostare storeTermVectors e tokenizer su true e quindi utilizzare questa istanza FieldType nel costruttore Field.

12

Ho avuto lo stesso problema, quindi ho solo semplicemente creato il mio campo:

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); 
} 

}

Spero che questo aiuti