Skip to content

Correct TermOrdValComparator competitive iterator intoBitSet implementation #14523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 19, 2025
5 changes: 4 additions & 1 deletion lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ Bug Fixes
---------------------

* GITHUB#14522: Fix DISIDocIdStream::count so that it does not try to count beyond max.
(Chris Hegarty}
(Chris Hegarty)

* GITHUB#14523: Correct TermOrdValComparator competitive iterator so that it forces sparse
field iteration to be at least scoring window baseline when doing intoBitSet. (Ben Trent, Adrien Grand)

======================= Lucene 10.2.0 =======================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected final int slowAdvance(int target) throws IOException {
* @lucene.internal
*/
public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {
assert offset <= docID();
assert offset <= docID() : "offset=" + offset + " docID()=" + docID() + " upTo=" + upTo;
for (int doc = docID(); doc < upTo; doc = nextDoc()) {
bitSet.set(doc - offset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,21 @@ public int advance(int target) throws IOException {

@Override
public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {
upTo = Math.min(upTo, maxDoc);
if (upTo <= doc) {
return;
}
// Optimize the case when intersecting the competitive iterator is expensive, which is when it
// hasn't nailed down a disjunction of competitive terms yet.
if (disjunction == null) {
if (docsWithField != null) {
// we need to be absolutely sure that the iterator is at least at offset
if (docsWithField.docID() < offset) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contract from DocIdSetIterator#intoBitSet requires starting from the current doc rather than the first doc on or after offset, so it would be more correct to advance to doc here. In practice, it wouldn't make a difference today since all call sites first advance iterators to offset before calling intoBitSet, but it may not always be the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contract from DocIdSetIterator#intoBitSet requires starting from the current doc rather than the first doc on or after offset, so it would be more correct to advance to doc here.

So things might got broken when current doc is after the first doc like

CompetitiveIterator competitiveIterator = CompetitiveIterator.of(1, 2, 3, 4);
competitiveIterator.advance(2); // advance current doc to 2
competitiveIterator.update(0, 2000); // update to doc value
final int offset = 1;
if (competitiveIterator.docID() < offset) {
  competitiveIterator.advance(offset);
}
competitiveIterator.intoBitSet(upTo, bitset, offset); // we should start from 2 instead of 1.

? Nice catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

docsWithField.advance(offset);
}
docsWithField.intoBitSet(upTo, bitSet, offset);
doc = docsWithField.docID();
} else {
upTo = Math.min(upTo, maxDoc);
bitSet.set(doc - offset, upTo - offset);
doc = upTo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.search.comparators;

import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.KeywordField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BulkScorer;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortedSetSelector;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopFieldCollectorManager;
import org.apache.lucene.search.Weight;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.util.LuceneTestCase;

public class TestTermOrdValComparator extends LuceneTestCase {

public void testIntoBitSetBugIssue14517() throws IOException {
final int maxDoc = 5_000;
try (Directory dir = new ByteBuffersDirectory()) {
try (IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
// high max doc to have a high number of unique values so that the competitive iterator is
// initialized with `docsWithField` rather than specific (< 1024) terms
for (int i = 0; i < maxDoc; ++i) {
Document doc = new Document();
// make the field to be sparse, so that the iterator is initialized with `docsWithField`
if (i % 2 == 0) {
doc.add(new StringField("field", "value", Field.Store.NO));
doc.add(new KeywordField("sort", Integer.toString(i), Field.Store.NO));
}
w.addDocument(doc);
}
w.forceMerge(1);
}
try (DirectoryReader reader = DirectoryReader.open(dir)) {
LeafReaderContext context = reader.leaves().get(0);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new TermQuery(new Term("field", "value"));
Weight weight =
searcher.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, RANDOM_MULTIPLIER);
SortField sortField = KeywordField.newSortField("sort", false, SortedSetSelector.Type.MIN);
sortField.setMissingValue(SortField.STRING_LAST);
Sort sort = new Sort(sortField);
Collector collector = new TopFieldCollectorManager(sort, 10, 10).newCollector();
LeafCollector leafCollector = collector.getLeafCollector(context);
BulkScorer bulkScorer = weight.bulkScorer(context);
// split on this specific doc ID so that the current doc of the competitive iterator
// and the current doc of `docsWithField` are out of sync,
// because the competitive iterator was just updated.
bulkScorer.score(leafCollector, null, 0, 22);
bulkScorer.score(leafCollector, null, 22, maxDoc);
}
}
}
}