6

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

1 Answer 1

6

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);
}
1
  • 2
    @user680406: You should never use numDocs() to access the documents in the index, but rather maxDoc() instead. Once the index has deletions, you will not reach out all docs. Apr 26, 2011 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.