diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene912/TestLucene912PostingsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene912/TestLucene912PostingsFormat.java index adf8aaf9ec7..4d4eae20e5f 100644 --- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene912/TestLucene912PostingsFormat.java +++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene912/TestLucene912PostingsFormat.java @@ -16,6 +16,9 @@ */ package org.apache.lucene.backward_codecs.lucene912; +import static org.apache.lucene.tests.util.TestUtil.alwaysPostingsFormat; +import static org.apache.lucene.tests.util.TestUtil.getDefaultPostingsFormat; + import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -23,14 +26,17 @@ import java.util.List; import org.apache.lucene.backward_codecs.lucene912.Lucene912PostingsReader.MutableImpactList; import org.apache.lucene.codecs.Codec; import org.apache.lucene.codecs.CompetitiveImpactAccumulator; +import org.apache.lucene.codecs.PostingsFormat; import org.apache.lucene.codecs.lucene90.blocktree.FieldReader; import org.apache.lucene.codecs.lucene90.blocktree.Stats; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.Impact; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.TermsEnum; import org.apache.lucene.store.ByteArrayDataInput; import org.apache.lucene.store.ByteArrayDataOutput; import org.apache.lucene.store.Directory; @@ -39,13 +45,13 @@ import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.tests.analysis.MockAnalyzer; import org.apache.lucene.tests.index.BasePostingsFormatTestCase; -import org.apache.lucene.tests.util.TestUtil; +import org.apache.lucene.util.BytesRef; public class TestLucene912PostingsFormat extends BasePostingsFormatTestCase { @Override protected Codec getCodec() { - return TestUtil.alwaysPostingsFormat(new Lucene912RWPostingsFormat()); + return alwaysPostingsFormat(new Lucene912RWPostingsFormat()); } public void testVInt15() throws IOException { @@ -154,4 +160,113 @@ public class TestLucene912PostingsFormat extends BasePostingsFormatTestCase { } } } + + public void testBackwardSeek() throws Exception { + Directory dir = newDirectory(); + // Set minTermBlockSize to 2, maxTermBlockSize to 3, to generate deep subBlock. + PostingsFormat postingsFormat = getDefaultPostingsFormat(2, 3); + + IndexWriter writer = + new IndexWriter(dir, newIndexWriterConfig().setCodec(alwaysPostingsFormat(postingsFormat))); + String[] categories = + new String[] { + "regular", "request1", "request2", "request3", "request4", "rest", "teacher", "team" + }; + + for (String category : categories) { + Document doc = new Document(); + doc.add(newStringField("category", category, Field.Store.YES)); + writer.addDocument(doc); + } + + IndexReader reader = DirectoryReader.open(writer); + + TermsEnum termsEnum = getOnlyLeafReader(reader).terms("category").iterator(); + + // Test seekExact. + BytesRef target = new BytesRef("request2"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request1"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request3"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request1"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("regular"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("rest"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("regular"); + assertTrue(termsEnum.seekExact(target)); + assertEquals(termsEnum.term(), target); + + // Test seekCeil. + target = new BytesRef("request2"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request1"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request3"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request1"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("request4"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("regular"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("rest"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + target = new BytesRef("regular"); + assertEquals(TermsEnum.SeekStatus.FOUND, termsEnum.seekCeil(target)); + assertEquals(termsEnum.term(), target); + + writer.close(); + reader.close(); + dir.close(); + } }