lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon Stewart <>
Subject Re: Document term vectors in Lucene 4
Date Thu, 17 Jan 2013 14:49:38 GMT
On Thu, Jan 17, 2013 at 9:08 AM, Robert Muir <> wrote:
> Which statistics in particular (which methods)?

I'd like to know the frequency of each term in each document. Those
term counts for the most frequent terms in the corpus will make it
into the document vectors for clustering.

Looking at Terms and TermsEnum, I'm actually somewhat baffled about
how to do this. Iterating over the TermsEnums in a Terms retrieved by
IndexReader.getTermVector() will tell me about the presence of a term
within a document, but I don't see a simple "count" or "freq" method
in TermsEnum--the methods there look like corpus statistics.

Based on Ian's reply, I created the following one-file test program.
The results I get are weird: I get a term vector back for the first
document, but not for the second.

doc 0 had term 'baz'
doc 0 had term 'foobar'
doc 0 had term 'gibberish'
doc 0 had 3 terms
doc 1 had no term vector for body

Thanks again for the responses and assistance.



import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.index.SlowCompositeReaderWrapper;
import org.apache.lucene.index.DirectoryReader;


import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Version;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.FieldType;

public class LuceneTest {

  static void createIndex(final String path) throws IOException,
CorruptIndexException {
    final Directory dir = File(path));
    final StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    final IndexWriterConfig iwc = new
IndexWriterConfig(Version.LUCENE_40, analyzer);
    final IndexWriter writer = new IndexWriter(dir, iwc);

    final FieldType bodyOptions = new FieldType();

    final Document doc = new Document();
    doc.add(new Field("body", "this foobar is gibberish, baz", bodyOptions));

    final Document doc2 = new Document();
    doc.add(new Field("body", "I don't know what to tell you, qux.
Some foobar is just fubar.", bodyOptions));


  static void readIndex(final String path) throws IOException,
CorruptIndexException {
    final DirectoryReader dirReader = File(path)));
    final SlowCompositeReaderWrapper rdr = new

    int max = rdr.maxDoc();

    TermsEnum term = null;
    // iterate docs
    for (int i = 0; i < max; ++i) {
      // get term vector for body field
      final Terms terms = rdr.getTermVector(i, "body");
      if (terms != null) {
        // count terms in doc
        int numTerms = 0;
        term = terms.iterator(term);
        while ( != null) {
          System.out.println("doc " + i + " had term '" +
term.term().utf8ToString() + "'");

          // would like to record doc term frequencies here, i.e.,
counts[i][term.term()] = term.freq()
        System.out.println("doc " + i + " had " + numTerms + " terms");
      else {
        System.err.println("doc " + i + " had no term vector for body");

  public static void main(String[] args) throws IOException,
InterruptedException, CorruptIndexException {
    final String path = args[0];

Jon Stewart, Principal
(646) 719-0317 | | Arlington, VA

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message