Index: src/test/org/apache/lucene/index/TestSegmentReader.java =================================================================== --- src/test/org/apache/lucene/index/TestSegmentReader.java (revision 962932) +++ src/test/org/apache/lucene/index/TestSegmentReader.java (working copy) @@ -139,7 +139,21 @@ assertTrue(positions != null); assertTrue(positions.doc() == 0); assertTrue(positions.nextPosition() >= 0); - } + } + + /** validate that positions of all terms are same as exposed by SegmentTermEnum + * @throws IOException + */ + public void testGetPosition() throws IOException { + long i = 0; + SegmentTermEnum terms = (SegmentTermEnum) reader.terms(); + while (terms.next() == true) { + Term term = terms.term(); + assertTrue(term != null); + assertEquals(terms.position, reader.getPosition(term)); + assertEquals(i++, terms.position); + } + } public void testNorms() throws IOException { //TODO: Not sure how these work/should be tested Index: src/java/org/apache/lucene/index/SegmentReader.java =================================================================== --- src/java/org/apache/lucene/index/SegmentReader.java (revision 962932) +++ src/java/org/apache/lucene/index/SegmentReader.java (working copy) @@ -891,6 +891,16 @@ return core.getTermsReader().terms(t); } + /** + * @param t a Term to locate + * @return the position of a Term in the set or -1. + * @throws IOException + */ + public long getPosition(final Term t) throws IOException { + ensureOpen(); + return core.getTermsReader().getPosition(t); + } + FieldInfos fieldInfos() { return core.fieldInfos; } Index: src/java/org/apache/lucene/index/TermInfosReader.java =================================================================== --- src/java/org/apache/lucene/index/TermInfosReader.java (revision 962932) +++ src/java/org/apache/lucene/index/TermInfosReader.java (working copy) @@ -256,13 +256,13 @@ SegmentTermEnum enumerator = getThreadResources().termEnum; seekEnum(enumerator, indexOffset); + enumerator.scanTo(term); - while(term.compareTo(enumerator.term()) > 0 && enumerator.next()) {} - - if (term.compareTo(enumerator.term()) == 0) + Term target = enumerator.term(); + if (target != null && term.compareTo(target) == 0) { return enumerator.position; - else - return -1; + } + return -1; } /** Returns an enumeration of all the Terms and TermInfos in the set. */