Buggy code:
public class TestPositionIncrement extends LuceneTestCase { static final boolean VERBOSE = false ; public void testSetPosition ( ) throws Exception { } public void testPayloadsPos0 ( ) throws Exception { Directory dir = newDirectory ( ) ; RandomIndexWriter writer = new RandomIndexWriter ( random ( ) , dir , new MockPayloadAnalyzer ( ) ) ; Document doc = new Document ( ) ; <START_BUG> doc . add ( new org . apache . lucene . document . TextField ( "content" , new StringReader ( "a<seq2seq4repair_space>a<seq2seq4repair_space>b<seq2seq4repair_space>c<seq2seq4repair_space>d<seq2seq4repair_space>e<seq2seq4repair_space>a<seq2seq4repair_space>f<seq2seq4repair_space>g<seq2seq4repair_space>h<seq2seq4repair_space>i<seq2seq4repair_space>j<seq2seq4repair_space>a<seq2seq4repair_space>b<seq2seq4repair_space>k<seq2seq4repair_space>k" ) , Store . NO ) ) ; <END_BUG> writer . addDocument ( doc ) ; final IndexReader readerFromWriter = writer . getReader ( ) ; AtomicReader r = SlowCompositeReaderWrapper . wrap ( readerFromWriter ) ; DocsAndPositionsEnum tp = r . termPositionsEnum ( r . getLiveDocs ( ) , "content" , new BytesRef ( "a" ) ) ; int count = 0 ; assertTrue ( ( ( tp . nextDoc ( ) ) != ( DocIdSetIterator . NO_MORE_DOCS ) ) ) ; assertEquals ( 4 , tp . freq ( ) ) ; assertEquals ( 0 , tp . nextPosition ( ) ) ; assertEquals ( 1 , tp . nextPosition ( ) ) ; assertEquals ( 3 , tp . nextPosition ( ) ) ; assertEquals ( 6 , tp . nextPosition ( ) ) ; assertEquals ( NO_MORE_DOCS , tp . nextDoc ( ) ) ; IndexSearcher is = newSearcher ( readerFromWriter ) ; SpanTermQuery stq1 = new SpanTermQuery ( new Term ( "content" , "a" ) ) ; SpanTermQuery stq2 = new SpanTermQuery ( new Term ( "content" , "k" ) ) ; SpanQuery [ ] sqs = new SpanQuery [ ] { stq1 , stq2 } ; SpanNearQuery snq = new SpanNearQuery ( sqs , 30 , false ) ; count = 0 ; boolean sawZero = false ; if ( TestPositionIncrement . VERBOSE ) { System . out . println ( "\ngetPayloadSpans<seq2seq4repair_space>test" ) ; } Spans pspans = MultiSpansWrapper . wrap ( is . getTopReaderContext ( ) , snq ) ; while ( pspans . next ( ) ) { if ( TestPositionIncrement . VERBOSE ) { System . out . println ( ( ( ( ( ( "doc<seq2seq4repair_space>" + ( pspans . doc ( ) ) ) + ":<seq2seq4repair_space>span<seq2seq4repair_space>" ) + ( pspans . start ( ) ) ) + "<seq2seq4repair_space>to<seq2seq4repair_space>" ) + ( pspans . end ( ) ) ) ) ; } Collection < byte [ ] > payloads = pspans . getPayload ( ) ; sawZero |= ( pspans . start ( ) ) == 0 ; for ( byte [ ] bytes : payloads ) { count ++ ; if ( TestPositionIncrement . VERBOSE ) { System . out . println ( ( "<seq2seq4repair_space>payload:<seq2seq4repair_space>" + ( new String ( bytes , "UTF-8" ) ) ) ) ; } } } assertTrue ( sawZero ) ; assertEquals ( 5 , count ) ; Spans spans = MultiSpansWrapper . wrap ( is . getTopReaderContext ( ) , snq ) ; count = 0 ; sawZero = false ; while ( spans . next ( ) ) { count ++ ; sawZero |= ( spans . start ( ) ) == 0 ; } assertEquals ( 4 , count ) ; assertTrue ( sawZero ) ; sawZero = false ; PayloadSpanUtil psu = new PayloadSpanUtil ( is . getTopReaderContext ( ) ) ; Collection < byte [ ] > pls = psu . getPayloadsForQuery ( snq ) ; count = pls . size ( ) ; for ( byte [ ] bytes : pls ) { String s = new String ( bytes , "UTF-8" ) ; sawZero |= s . equals ( "pos:<seq2seq4repair_space>0" ) ; } assertEquals ( 5 , count ) ; assertTrue ( sawZero ) ; writer . close ( ) ; is . getIndexReader ( ) . close ( ) ; dir . close ( ) ; } }

Fix:
doc . add ( new TextField ( "content" , new StringReader ( "a<seq2seq4repair_space>a<seq2seq4repair_space>b<seq2seq4repair_space>c<seq2seq4repair_space>d<seq2seq4repair_space>e<seq2seq4repair_space>a<seq2seq4repair_space>f<seq2seq4repair_space>g<seq2seq4repair_space>h<seq2seq4repair_space>i<seq2seq4repair_space>j<seq2seq4repair_space>a<seq2seq4repair_space>b<seq2seq4repair_space>k<seq2seq4repair_space>k" ) ) ) ;