Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ Bug Fixes
* GITHUB#17020: Reduce CachingTokenFilter memory use by not copying stale term bytes in
CharTermAttributeImpl#clone. (Eugene Rizhkov)

* GITHUB#17028: Fix lost matches and scores in JoinUtil global ordinal joins over a single segment
when concurrent search splits the segment into several slices. (Asharam)

* GITHUB#16983: UnifiedHighlighter.Builder#withFlags is no longer ignored when building the
highlighter. (Christoph Büscher)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class GlobalOrdinalsWithScoreCollectorManager implements CollectorManager<Collec
final OrdinalMap ordinalMap;
final LongBitSet collectedOrds;
// note: we only lock when collecting since the operation here is: do all the collection, and then
// become immutable when we can have any number of concurrent readers. Locks are per-block, so
// access to a single lock should generally be by one or two threads since global docids are
// handled by per-segment threads, which each get contiguous blocks of docids.
// become immutable when we can have any number of concurrent readers. Locks are per-block and
// guard the match bits, scores and occurrence counts of that block together. Several slices can
// collect concurrently, including different partitions of the same segment, so this is needed
// with or without an ordinal map.
final Lock[] locks;

// also written concurrently
Expand Down Expand Up @@ -99,6 +100,11 @@ public Void reduce(Collection<Collector> collectors) {
return null;
}

private LongValues globalOrds(LeafReaderContext context) {
// With a single segment there is no ordinal map: segment ordinals are the global ordinals.
return ordinalMap == null ? LongValues.IDENTITY : ordinalMap.getGlobalOrds(context.ord);
}

public boolean match(int globalOrd) {
if (collectedOrds.get(globalOrd)) {
if (doMinMax) {
Expand Down Expand Up @@ -128,26 +134,21 @@ public float score(int globalOrdinal) {
@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
SortedDocValues docTermOrds = DocValues.getSorted(context.reader(), field);
if (ordinalMap != null) {
LongValues segmentOrdToGlobalOrdLookup = ordinalMap.getGlobalOrds(context.ord);
return new OrdinalMapCollector(docTermOrds, segmentOrdToGlobalOrdLookup);
} else {
return new SegmentOrdinalCollector(docTermOrds);
}
return new OrdinalCollector(docTermOrds, globalOrds(context));
}

@Override
public org.apache.lucene.search.ScoreMode scoreMode() {
return org.apache.lucene.search.ScoreMode.COMPLETE;
}

final class OrdinalMapCollector implements LeafCollector {
final class OrdinalCollector implements LeafCollector {

private final SortedDocValues docTermOrds;
private final LongValues segmentOrdToGlobalOrdLookup;
private Scorable scorer;

OrdinalMapCollector(SortedDocValues docTermOrds, LongValues segmentOrdToGlobalOrdLookup) {
OrdinalCollector(SortedDocValues docTermOrds, LongValues segmentOrdToGlobalOrdLookup) {
this.docTermOrds = docTermOrds;
this.segmentOrdToGlobalOrdLookup = segmentOrdToGlobalOrdLookup;
}
Expand All @@ -156,48 +157,24 @@ final class OrdinalMapCollector implements LeafCollector {
public void collect(int doc) throws IOException {
if (docTermOrds.advanceExact(doc)) {
final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(docTermOrds.ordValue());
// The scorer is owned by this leaf collector, so score outside the lock.
final double newScore = scorer.score();
Lock lock = locks[globalOrd / arraySize];
lock.lock();
try {
collectedOrds.set(globalOrd);
double existingScore = scores.getScore(globalOrd);
double newScore = scorer.score();
doScore(globalOrd, existingScore, newScore);
if (occurrences != null) {
occurrences.increment(globalOrd);
}
collectOrd(globalOrd, newScore);
} finally {
lock.unlock();
}
}
}

@Override
public void setScorer(Scorable scorer) throws IOException {
this.scorer = scorer;
}
}

final class SegmentOrdinalCollector implements LeafCollector {

private final SortedDocValues docTermOrds;
private Scorable scorer;

SegmentOrdinalCollector(SortedDocValues docTermOrds) {
this.docTermOrds = docTermOrds;
}

@Override
public void collect(int doc) throws IOException {
if (docTermOrds.advanceExact(doc)) {
int segmentOrd = docTermOrds.ordValue();
collectedOrds.set(segmentOrd);
double existingScore = scores.getScore(segmentOrd);
double newScore = scorer.score();
doScore(segmentOrd, existingScore, newScore);
if (occurrences != null) {
occurrences.increment(segmentOrd);
}
private void collectOrd(int globalOrd, double newScore) {
collectedOrds.set(globalOrd);
double existingScore = scores.getScore(globalOrd);
doScore(globalOrd, existingScore, newScore);
if (occurrences != null) {
occurrences.increment(globalOrd);
}
}

Expand Down Expand Up @@ -240,43 +217,31 @@ final class NoScore extends GlobalOrdinalsWithScoreCollector {
@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
SortedDocValues docTermOrds = DocValues.getSorted(context.reader(), field);
if (ordinalMap != null) {
LongValues segmentOrdToGlobalOrdLookup = ordinalMap.getGlobalOrds(context.ord);
return new LeafCollector() {

@Override
public void setScorer(Scorable scorer) throws IOException {}

@Override
public void collect(int doc) throws IOException {
if (docTermOrds.advanceExact(doc)) {
final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(docTermOrds.ordValue());
Lock lock = locks[globalOrd / arraySize];
lock.lock();
try {
collectedOrds.set(globalOrd);
occurrences.increment(globalOrd);
} finally {
lock.unlock();
}
LongValues segmentOrdToGlobalOrdLookup = globalOrds(context);
return new LeafCollector() {

@Override
public void setScorer(Scorable scorer) throws IOException {}

@Override
public void collect(int doc) throws IOException {
if (docTermOrds.advanceExact(doc)) {
final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(docTermOrds.ordValue());
Lock lock = locks[globalOrd / arraySize];
lock.lock();
try {
collectOrd(globalOrd);
} finally {
lock.unlock();
}
}
};
} else {
return new LeafCollector() {
@Override
public void setScorer(Scorable scorer) throws IOException {}

@Override
public void collect(int doc) throws IOException {
if (docTermOrds.advanceExact(doc)) {
int segmentOrd = docTermOrds.ordValue();
collectedOrds.set(segmentOrd);
occurrences.increment(segmentOrd);
}
}
};
}
}

private void collectOrd(int globalOrd) {
collectedOrds.set(globalOrd);
occurrences.increment(globalOrd);
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.FilterScorer;
import org.apache.lucene.search.IndexSearcher;
Expand All @@ -85,6 +86,7 @@
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.ScorerSupplier;
import org.apache.lucene.search.SimpleCollector;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopScoreDocCollectorManager;
Expand Down Expand Up @@ -510,6 +512,76 @@ public void testRandomOrdinalsJoin() throws Exception {
context.close();
}

public void testGlobalOrdinalsJoinWithSegmentPartitions() throws Exception {
// Sometimes use more keys than one block of the collector's per-block locks.
final int numKeys;
final int docsPerKey;
if (random().nextInt(4) == 0) {
numKeys = TestUtil.nextInt(random(), 4097, 9000);
docsPerKey = TestUtil.nextInt(random(), 2, 3);
} else {
numKeys = TestUtil.nextInt(random(), 1, 64);
docsPerKey = TestUtil.nextInt(random(), 16, 128);
}
final int numDocs = numKeys * docsPerKey;
final int numPartitions = TestUtil.nextInt(random(), 2, 6);
try (Directory dir = newDirectory();
IndexWriter w =
new IndexWriter(dir, newIndexWriterConfig().setMergePolicy(newLogMergePolicy()))) {
for (int i = 0; i < numDocs; i++) {
w.addDocument(
List.of(
new SortedDocValuesField("key", new BytesRef(Integer.toString(i % numKeys))),
new NumericDocValuesField("score", TestUtil.nextInt(random(), 1, 1000))));
}
w.forceMerge(1);
ExecutorService executor =
Executors.newFixedThreadPool(4, new NamedThreadFactory("TestJoinUtil"));
try (DirectoryReader reader = DirectoryReader.open(w)) {
assertEquals(1, reader.leaves().size());
// Partitions of the single segment, searched concurrently.
IndexSearcher concurrent =
new IndexSearcher(reader, executor) {
@Override
protected LeafSlice[] slices(List<LeafReaderContext> leaves) {
return IndexSearcher.slices(
leaves, (numDocs + numPartitions - 1) / numPartitions, 1, true);
}
};
assertTrue(concurrent.getSlices().length > 1);
IndexSearcher sequential = new IndexSearcher(reader);
Query from = numericDocValuesScoreQuery("score");
Query to = MatchAllDocsQuery.INSTANCE;
for (ScoreMode mode : ScoreMode.values()) {
// Exact occurrence bounds: every key must be counted exactly docsPerKey times.
for (int min : new int[] {0, docsPerKey}) {
int max = min == 0 ? Integer.MAX_VALUE : docsPerKey;
Query expectedJoin =
JoinUtil.createJoinQuery("key", from, to, sequential, mode, null, min, max);
Query actualJoin =
JoinUtil.createJoinQuery("key", from, to, concurrent, mode, null, min, max);
TopDocs expected = sequential.search(expectedJoin, numDocs, Sort.INDEXORDER, true);
TopDocs actual = concurrent.search(actualJoin, numDocs, Sort.INDEXORDER, true);
assertEquals(numDocs, expected.scoreDocs.length);
assertEquals(mode + " min=" + min, numDocs, actual.scoreDocs.length);
for (int i = 0; i < numDocs; i++) {
assertEquals(expected.scoreDocs[i].doc, actual.scoreDocs[i].doc);
if (mode != ScoreMode.None) {
assertEquals(
mode + " min=" + min,
((FieldDoc) expected.scoreDocs[i]).score,
((FieldDoc) actual.scoreDocs[i]).score,
0f);
}
}
}
}
} finally {
TestUtil.shutdownExecutorService(executor);
}
}
}

public void testMinMaxScore() throws Exception {
String priceField = "price";
Query priceQuery = numericDocValuesScoreQuery(priceField);
Expand Down
Loading