Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene: termFreqVector is always null?

Tags:

java

lucene

for any document, the termFreqVector is always null. I'm sure the documents are in the collection and the field exist. So where is the problem ?

for (int i = 0; i < reader.numDocs(); i++){
TermFreqVector tfv = reader.getTermFreqVector(i, "tags");

thanks

like image 526
user680406 Avatar asked Apr 21 '11 08:04

user680406


1 Answers

Are you sure you're indexing with your fields with Field.TermVector.YES? Here's a working example:

Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
MaxFieldLength mlf = MaxFieldLength.UNLIMITED;
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);

Document doc = new Document();
doc.add(new Field("tags", "foo bar", Field.Store.NO,
        Field.Index.ANALYZED, Field.TermVector.YES));

writer.addDocument(doc);
writer.close();

IndexReader reader = IndexReader.open(directory);
for (int i = 0; i < reader.numDocs(); i++) {
    TermFreqVector tfv = reader.getTermFreqVector(i, "tags");
    System.out.println(tfv);
}
like image 54
WhiteFang34 Avatar answered Nov 08 '22 03:11

WhiteFang34