From a1ca4e1248cfa0ce0578d5227b52eb3a06b38729 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Tue, 30 Sep 2025 16:53:45 +0200 Subject: [PATCH] Add a test that makes sure we return the right impacts when there are no frequencies --- .../lucene90/TestLucene90PostingsFormat.java | 5 ++++ .../TestLucene912PostingsFormat.java | 5 ++++ .../lucene99/TestLucene99PostingsFormat.java | 5 ++++ .../memory/TestDirectPostingsFormat.java | 5 ++++ .../perfield/TestPerFieldPostingsFormat.java | 6 ++++ .../index/BasePostingsFormatTestCase.java | 29 +++++++++++++++++++ .../TestAssertingPostingsFormat.java | 5 ++++ 7 files changed, 60 insertions(+) diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene90/TestLucene90PostingsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene90/TestLucene90PostingsFormat.java index 7d350ca3c1a0..1ebb7ef1760c 100644 --- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene90/TestLucene90PostingsFormat.java +++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene90/TestLucene90PostingsFormat.java @@ -52,6 +52,11 @@ protected Codec getCodec() { return codec; } + @Override + protected boolean indexFakeImpacts() { + return true; + } + /** Make sure the final sub-block(s) are not skipped. */ public void testFinalBlock() throws Exception { Directory d = newDirectory(); 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 790ac9197908..a0839d92e222 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 @@ -49,6 +49,11 @@ protected Codec getCodec() { return TestUtil.alwaysPostingsFormat(new Lucene912RWPostingsFormat()); } + @Override + protected boolean indexFakeImpacts() { + return true; + } + public void testVInt15() throws IOException { byte[] bytes = new byte[5]; ByteArrayDataOutput out = new ByteArrayDataOutput(bytes); diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99PostingsFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99PostingsFormat.java index ebdaab981c22..6fb479952344 100644 --- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99PostingsFormat.java +++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene99/TestLucene99PostingsFormat.java @@ -53,6 +53,11 @@ protected Codec getCodec() { return codec; } + @Override + protected boolean indexFakeImpacts() { + return true; + } + /** Make sure the final sub-block(s) are not skipped. */ public void testFinalBlock() throws Exception { Directory d = newDirectory(); diff --git a/lucene/codecs/src/test/org/apache/lucene/codecs/memory/TestDirectPostingsFormat.java b/lucene/codecs/src/test/org/apache/lucene/codecs/memory/TestDirectPostingsFormat.java index 493ad6bbd4f9..57ac5e3789eb 100644 --- a/lucene/codecs/src/test/org/apache/lucene/codecs/memory/TestDirectPostingsFormat.java +++ b/lucene/codecs/src/test/org/apache/lucene/codecs/memory/TestDirectPostingsFormat.java @@ -34,4 +34,9 @@ protected boolean isPostingsEnumReuseImplemented() { protected Codec getCodec() { return codec; } + + @Override + protected boolean indexFakeImpacts() { + return true; + } } diff --git a/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldPostingsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldPostingsFormat.java index 52f86a924101..d9aeaa01bb26 100644 --- a/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldPostingsFormat.java +++ b/lucene/core/src/test/org/apache/lucene/codecs/perfield/TestPerFieldPostingsFormat.java @@ -39,4 +39,10 @@ public void testMergeStability() throws Exception { public void testPostingsEnumReuse() throws Exception { assumeTrue("The MockRandom PF randomizes content on the fly, so we can't check it", false); } + + @Override + protected boolean indexFakeImpacts() { + // we don't know if the codec will use fake impls or not, true is the safe value + return true; + } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java index dd737c4930e9..394ef64c1a54 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java @@ -45,6 +45,8 @@ import org.apache.lucene.index.CodecReader; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.Fields; +import org.apache.lucene.index.FreqAndNormBuffer; +import org.apache.lucene.index.ImpactsEnum; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; @@ -1798,4 +1800,31 @@ public void testDocIDRunEnd() throws Exception { } dir.close(); } + + protected boolean indexFakeImpacts() { + return false; + } + + public void testImpactsNoFreqs() throws Exception { + assumeFalse("We index fake impacts between 9.0 in 9.12", indexFakeImpacts()); + try (Directory dir = newDirectory()) { + IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())); + iwc.setCodec(getCodec()); + try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc)) { + Document doc = new Document(); + doc.add(newStringField("field", "value", Field.Store.NO)); + iw.addDocument(doc); + try (DirectoryReader ir = iw.getReader()) { + LeafReader ar = getOnlyLeafReader(ir); + TermsEnum termsEnum = ar.terms("field").iterator(); + termsEnum.seekExact(new BytesRef("value")); + ImpactsEnum impactsEnum = termsEnum.impacts(FREQS); + FreqAndNormBuffer freqAndNormBuffer = impactsEnum.getImpacts().getImpacts(0); + assertEquals(1, freqAndNormBuffer.size); + assertEquals(1, freqAndNormBuffer.freqs[0]); + assertEquals(1L, freqAndNormBuffer.norms[0]); + } + } + } + } } diff --git a/lucene/test-framework/src/test/org/apache/lucene/tests/codecs/asserting/TestAssertingPostingsFormat.java b/lucene/test-framework/src/test/org/apache/lucene/tests/codecs/asserting/TestAssertingPostingsFormat.java index 2509cc2379b3..36e042db3747 100644 --- a/lucene/test-framework/src/test/org/apache/lucene/tests/codecs/asserting/TestAssertingPostingsFormat.java +++ b/lucene/test-framework/src/test/org/apache/lucene/tests/codecs/asserting/TestAssertingPostingsFormat.java @@ -32,4 +32,9 @@ protected Codec getCodec() { protected boolean isPostingsEnumReuseImplemented() { return false; } + + @Override + protected boolean indexFakeImpacts() { + return true; + } }