lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrien Grand <jpou...@gmail.com>
Subject Re: Term vector Lucene 4.2
Date Tue, 02 Apr 2013 10:05:12 GMT
Hi Andi,

Here is how you could retrieve positions from your document:

    Terms termVector = indexReader.getTermVector(docId, fieldName);
    TermsEnum reuse = null;
    TermsEnum iterator = termVector.iterator(reuse);
    BytesRef ref = null;
    DocsAndPositionsEnum docsAndPositions = null;
    while ((ref = iterator.next()) != null) {
        docsAndPositions = iterator.docsAndPositions(null, docsAndPositions);
        // beware that docsAndPositions will be null if you didn't
index positions
        if (docsAndPositions.nextDoc() != 0) { // you need to call
nextDoc() to have the enum positioned
          throw new AssertionError();
        }
        final int freq = docsAndPositions.freq(); // number of
occurrences of the term
        for (int i = 0; i < freq; ++i) {
          final int position = docsAndPositions.nextPosition();
          // 'position' is the i-th position of the current term in the document
        }
    }

I hope this helps.

-- 
Adrien

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message