Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void scoreWindowScoreFirst(
docAndScoreAccBuffer, sumOfOtherClause, scorable.minCompetitiveScore, scorers.length);
}

ScorerUtil.applyRequiredClause(docAndScoreAccBuffer, iterators[i], scorables[i]);
scorers[i].applyAsRequiredClause(docAndScoreAccBuffer);
}

for (int i = 0; i < docAndScoreAccBuffer.size; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer
Arrays.fill(buffer.features, 0, size, score);
buffer.size = size;
}

@Override
public void applyAsRequiredClause(DocAndScoreAccBuffer buffer) throws IOException {
int intersectionSize = 0;
int curDoc = disi.docID();
for (int i = 0; i < buffer.size; ++i) {
int targetDoc = buffer.docs[i];
if (curDoc < targetDoc) {
curDoc = disi.advance(targetDoc);
}
if (curDoc == targetDoc) {
buffer.docs[intersectionSize] = targetDoc;
buffer.scores[intersectionSize] = buffer.scores[i];
intersectionSize++;
}
}

buffer.size = intersectionSize;
if (score != 0) {
for (int i = 0; i < intersectionSize; ++i) {
buffer.scores[i] += score;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ private void scoreInnerWindowAsConjunction(LeafCollector collector, Bits acceptD
allScorers.length);
}

DisiWrapper scorer = allScorers[i];
ScorerUtil.applyRequiredClause(docAndScoreAccBuffer, scorer.iterator, scorer.scorable);
allScorers[i].scorer.applyAsRequiredClause(docAndScoreAccBuffer);
}

scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstRequiredScorer);
Expand Down
23 changes: 23 additions & 0 deletions lucene/core/src/java/org/apache/lucene/search/Scorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,27 @@ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer
}
buffer.size = size;
}

/**
* Apply this {@link Scorer} as a required clause on the given {@link DocAndScoreAccBuffer}. This
* filters out documents from the buffer that do not match this scorer, and adds the scores of
* this {@link Scorer} to the scores.
*/
public void applyAsRequiredClause(DocAndScoreAccBuffer buffer) throws IOException {
DocIdSetIterator iterator = iterator();
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should throw an exception if this scorer exposes a twoPhaseIterator() (and add javadocs about it), since running the conjunction this way would be less efficient than doing it in a "doc first" fashion.

int intersectionSize = 0;
int curDoc = iterator.docID();
for (int i = 0; i < buffer.size; ++i) {
int targetDoc = buffer.docs[i];
if (curDoc < targetDoc) {
curDoc = iterator.advance(targetDoc);
}
if (curDoc == targetDoc) {
buffer.docs[intersectionSize] = targetDoc;
buffer.scores[intersectionSize] = buffer.scores[i] + score();
intersectionSize++;
}
}
buffer.size = intersectionSize;
}
}
39 changes: 39 additions & 0 deletions lucene/core/src/java/org/apache/lucene/search/TermScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.lucene.search.similarities.Similarity.SimScorer;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.LongsRef;

/**
Expand All @@ -41,6 +42,7 @@ public final class TermScorer extends Scorer {
private final NumericDocValues norms;
private final ImpactsDISI impactsDisi;
private final MaxScoreCache maxScoreCache;
private int[] freqs = IntsRef.EMPTY_INTS;
private long[] normValues = LongsRef.EMPTY_LONGS;

/** Construct a {@link TermScorer} that will iterate all documents. */
Expand Down Expand Up @@ -171,4 +173,41 @@ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer

bulkScorer.score(buffer.size, buffer.features, normValues, buffer.features);
}

@Override
public void applyAsRequiredClause(DocAndScoreAccBuffer buffer) throws IOException {
int size = buffer.size;
if (freqs.length < size) {
freqs = ArrayUtil.growNoCopy(freqs, size);
normValues = new long[freqs.length];
}

int intersectionSize = 0;
int curDoc = docID();
for (int i = 0; i < size; i++) {
int targetDoc = buffer.docs[i];
if (curDoc < targetDoc) {
curDoc = postingsEnum.advance(targetDoc);
}
if (curDoc == targetDoc) {
buffer.docs[intersectionSize] = targetDoc;
buffer.scores[intersectionSize] = buffer.scores[i];
freqs[intersectionSize] = postingsEnum.freq();
intersectionSize++;
}
}
buffer.size = intersectionSize;

for (int i = 0; i < intersectionSize; i++) {
if (norms == null || norms.advanceExact(buffer.docs[i]) == false) {
normValues[i] = 1L;
} else {
normValues[i] = norms.longValue();
}
}

for (int i = 0; i < intersectionSize; i++) {
buffer.scores[i] += scorer.score(freqs[i], normValues[i]);
}
}
}
Loading