Skip to content

Commit 8e0c596

Browse files
authored
Fix returned Impacts when frequencies are not indexed (#15263)
This updates postings to always return impacts with freq=1 and norm=1 when frequencies are not indexed.
1 parent e823d38 commit 8e0c596

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lucene/CHANGES.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ http://s.apache.org/luceneversions
77

88
Bug Fixes
99
---------------------
10-
(No changes)
10+
* GITHUB#15263: Fix the returned Impact returned from Lucene103PostingsReader when frequencies
11+
are not indexed. It was returning a wrong frequency in that case affecting scoring which
12+
might led to performance issues. (Ignacio Vera)
1113

1214
======================= Lucene 10.3.0 =======================
1315

lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsReader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public final class Lucene103PostingsReader extends PostingsReaderBase {
7474
// less than 128 docs left to evaluate anyway.
7575
private static final List<Impact> DUMMY_IMPACTS =
7676
Collections.singletonList(new Impact(Integer.MAX_VALUE, 1L));
77+
// impacts when there is no frequency, max frequency is 1.
78+
private static final List<Impact> IMPACTS_NO_FREQ = Collections.singletonList(new Impact(1, 1L));
7779

7880
private final IndexInput docIn;
7981
private final IndexInput posIn;
@@ -1381,8 +1383,9 @@ public List<Impact> getImpacts(int level) {
13811383
if (level == 1) {
13821384
return readImpacts(level1SerializedImpacts, level1Impacts);
13831385
}
1386+
return DUMMY_IMPACTS;
13841387
}
1385-
return DUMMY_IMPACTS;
1388+
return IMPACTS_NO_FREQ;
13861389
}
13871390

13881391
private List<Impact> readImpacts(BytesRef serialized, MutableImpactList impactsList) {

lucene/core/src/test/org/apache/lucene/codecs/lucene103/TestLucene103PostingsFormat.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
import org.apache.lucene.document.Field;
3030
import org.apache.lucene.index.DirectoryReader;
3131
import org.apache.lucene.index.Impact;
32+
import org.apache.lucene.index.ImpactsEnum;
3233
import org.apache.lucene.index.IndexWriter;
3334
import org.apache.lucene.index.IndexWriterConfig;
35+
import org.apache.lucene.index.LeafReader;
36+
import org.apache.lucene.index.PostingsEnum;
37+
import org.apache.lucene.index.TermsEnum;
3438
import org.apache.lucene.store.ByteArrayDataInput;
3539
import org.apache.lucene.store.ByteArrayDataOutput;
3640
import org.apache.lucene.store.Directory;
@@ -39,7 +43,9 @@
3943
import org.apache.lucene.store.IndexOutput;
4044
import org.apache.lucene.tests.analysis.MockAnalyzer;
4145
import org.apache.lucene.tests.index.BasePostingsFormatTestCase;
46+
import org.apache.lucene.tests.index.RandomIndexWriter;
4247
import org.apache.lucene.tests.util.TestUtil;
48+
import org.apache.lucene.util.BytesRef;
4349

4450
public class TestLucene103PostingsFormat extends BasePostingsFormatTestCase {
4551

@@ -154,4 +160,26 @@ private void doTestImpactSerialization(List<Impact> impacts) throws IOException
154160
}
155161
}
156162
}
163+
164+
public void testImpactsNoFreqs() throws Exception {
165+
try (Directory dir = newDirectory()) {
166+
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
167+
iwc.setCodec(getCodec());
168+
try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc)) {
169+
Document doc = new Document();
170+
doc.add(newStringField("field", "value", Field.Store.NO));
171+
iw.addDocument(doc);
172+
try (DirectoryReader ir = iw.getReader()) {
173+
LeafReader ar = getOnlyLeafReader(ir);
174+
TermsEnum termsEnum = ar.terms("field").iterator();
175+
termsEnum.seekExact(new BytesRef("value"));
176+
ImpactsEnum impactsEnum = termsEnum.impacts(PostingsEnum.FREQS);
177+
List<Impact> impacts = impactsEnum.getImpacts().getImpacts(0);
178+
assertEquals(1, impacts.size());
179+
assertEquals(1, impacts.get(0).freq);
180+
assertEquals(1L, impacts.get(0).norm);
181+
}
182+
}
183+
}
184+
}
157185
}

0 commit comments

Comments
 (0)