You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "vigyasharma (via GitHub)" <gi...@apache.org> on 2025/06/12 20:32:13 UTC
[PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
vigyasharma opened a new pull request, #14776:
URL: https://github.com/apache/lucene/pull/14776
A `Rescorer` that uses values from provided `DoubleValuesSource` to re-score top N hits of a query.
Enables us to rescore hits from ANN vector queries using full-precision or late-interaction DoubleValuesSources (#14729 and #14708).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "dungba88 (via GitHub)" <gi...@apache.org>.
dungba88 commented on code in PR #14776:
URL: https://github.com/apache/lucene/pull/14776#discussion_r2144150610
##########
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.ArrayUtil;
+
+/** A {@link Rescorer} that uses provided DoubleValuesSource to rescore first pass hits. */
+public abstract class DoubleValuesSourceRescorer extends Rescorer {
+
+ private final DoubleValuesSource valuesSource;
+
+ public DoubleValuesSourceRescorer(DoubleValuesSource valuesSource) {
+ this.valuesSource = valuesSource;
+ }
+
+ /**
+ * Implement this in a subclass to combine the first pass scores with values from the
+ * DoubleValuesSource
+ *
+ * @param firstPassScore Score from firstPassTopDocs
+ * @param valuePresent true if DoubleValuesSource has a value for the hit from first pass
+ * @param sourceValue Value returned from DoubleValuesSource
+ */
+ protected abstract float combine(float firstPassScore, boolean valuePresent, double sourceValue);
+
+ @Override
+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)
+ throws IOException {
+ DoubleValuesSource source = valuesSource.rewrite(searcher);
+ // this will still alter scores, we clone to retain hits ordering in firstPassTopDocs
+ ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();
+ Arrays.sort(hits, (a, b) -> a.doc - b.doc);
+
+ List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
+ LeafReaderContext ctx = leaves.getFirst();
+ int currLeaf = 0;
+ int leafEnd = ctx.docBase + ctx.reader().maxDoc();
+
+ // find leaf holding this doc
+ for (ScoreDoc hit : hits) {
+ while (hit.doc >= leafEnd) {
+ if (currLeaf == leaves.size() - 1) {
+ throw new IllegalStateException(
+ "hit docId="
+ + hit.doc
+ + "greater than last searcher leaf maxDoc="
+ + leafEnd
+ + " Ensure firstPassTopDocs were produced by the searcher provided to rescore.");
+ }
+ ctx = leaves.get(++currLeaf);
+ leafEnd = ctx.docBase + ctx.reader().maxDoc();
+ }
+
+ int targetDoc = hit.doc - ctx.docBase;
+ DoubleValues values = source.getValues(ctx, null);
+ boolean scorePresent = values.advanceExact(targetDoc);
+ double secondPassScore = scorePresent ? values.doubleValue() : 0.0f;
+ hit.score = combine(hit.score, scorePresent, secondPassScore);
+ }
+
+ if (topN < hits.length) {
+ ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.scoreDocComparator);
+ ScoreDoc[] subset = new ScoreDoc[topN];
+ System.arraycopy(hits, 0, subset, 0, topN);
+ hits = subset;
+ }
+ Arrays.sort(hits, ScoreDoc.scoreDocComparator);
+
+ return new TopDocs(firstPassTopDocs.totalHits, hits);
+ }
+
+ @Override
+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
+ throws IOException {
+ Explanation first =
Review Comment:
Maybe it should be moved to when it is needed, such as L120?
##########
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.ArrayUtil;
+
+/** A {@link Rescorer} that uses provided DoubleValuesSource to rescore first pass hits. */
+public abstract class DoubleValuesSourceRescorer extends Rescorer {
+
+ private final DoubleValuesSource valuesSource;
+
+ public DoubleValuesSourceRescorer(DoubleValuesSource valuesSource) {
+ this.valuesSource = valuesSource;
+ }
+
+ /**
+ * Implement this in a subclass to combine the first pass scores with values from the
+ * DoubleValuesSource
+ *
+ * @param firstPassScore Score from firstPassTopDocs
+ * @param valuePresent true if DoubleValuesSource has a value for the hit from first pass
+ * @param sourceValue Value returned from DoubleValuesSource
+ */
+ protected abstract float combine(float firstPassScore, boolean valuePresent, double sourceValue);
+
+ @Override
+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)
+ throws IOException {
+ DoubleValuesSource source = valuesSource.rewrite(searcher);
+ // this will still alter scores, we clone to retain hits ordering in firstPassTopDocs
+ ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();
+ Arrays.sort(hits, (a, b) -> a.doc - b.doc);
+
+ List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
+ LeafReaderContext ctx = leaves.getFirst();
+ int currLeaf = 0;
+ int leafEnd = ctx.docBase + ctx.reader().maxDoc();
+
+ // find leaf holding this doc
+ for (ScoreDoc hit : hits) {
+ while (hit.doc >= leafEnd) {
+ if (currLeaf == leaves.size() - 1) {
+ throw new IllegalStateException(
+ "hit docId="
+ + hit.doc
+ + "greater than last searcher leaf maxDoc="
+ + leafEnd
+ + " Ensure firstPassTopDocs were produced by the searcher provided to rescore.");
+ }
+ ctx = leaves.get(++currLeaf);
+ leafEnd = ctx.docBase + ctx.reader().maxDoc();
+ }
+
+ int targetDoc = hit.doc - ctx.docBase;
+ DoubleValues values = source.getValues(ctx, null);
+ boolean scorePresent = values.advanceExact(targetDoc);
+ double secondPassScore = scorePresent ? values.doubleValue() : 0.0f;
+ hit.score = combine(hit.score, scorePresent, secondPassScore);
+ }
+
+ if (topN < hits.length) {
+ ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.scoreDocComparator);
+ ScoreDoc[] subset = new ScoreDoc[topN];
+ System.arraycopy(hits, 0, subset, 0, topN);
+ hits = subset;
+ }
+ Arrays.sort(hits, ScoreDoc.scoreDocComparator);
+
+ return new TopDocs(firstPassTopDocs.totalHits, hits);
+ }
+
+ @Override
+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
+ throws IOException {
+ Explanation first =
+ Explanation.match(
+ firstPassExplanation.getValue(), "first pass score", firstPassExplanation);
+
+ LeafReaderContext leafWithDoc = null;
+ for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
+ if (docID >= ctx.docBase && docID < (ctx.docBase + ctx.reader().maxDoc())) {
+ leafWithDoc = ctx;
+ break;
+ }
+ }
+ if (leafWithDoc == null) {
+ throw new IllegalArgumentException(
Review Comment:
Why we are throwing exception here instead of Explanation.noMatch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #14776:
URL: https://github.com/apache/lucene/pull/14776#issuecomment-2968083964
This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "vigyasharma (via GitHub)" <gi...@apache.org>.
vigyasharma commented on code in PR #14776:
URL: https://github.com/apache/lucene/pull/14776#discussion_r2144287000
##########
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.ArrayUtil;
+
+/** A {@link Rescorer} that uses provided DoubleValuesSource to rescore first pass hits. */
+public abstract class DoubleValuesSourceRescorer extends Rescorer {
+
+ private final DoubleValuesSource valuesSource;
+
+ public DoubleValuesSourceRescorer(DoubleValuesSource valuesSource) {
+ this.valuesSource = valuesSource;
+ }
+
+ /**
+ * Implement this in a subclass to combine the first pass scores with values from the
+ * DoubleValuesSource
+ *
+ * @param firstPassScore Score from firstPassTopDocs
+ * @param valuePresent true if DoubleValuesSource has a value for the hit from first pass
+ * @param sourceValue Value returned from DoubleValuesSource
+ */
+ protected abstract float combine(float firstPassScore, boolean valuePresent, double sourceValue);
+
+ @Override
+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)
+ throws IOException {
+ DoubleValuesSource source = valuesSource.rewrite(searcher);
+ // this will still alter scores, we clone to retain hits ordering in firstPassTopDocs
+ ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();
+ Arrays.sort(hits, (a, b) -> a.doc - b.doc);
+
+ List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
+ LeafReaderContext ctx = leaves.getFirst();
+ int currLeaf = 0;
+ int leafEnd = ctx.docBase + ctx.reader().maxDoc();
+
+ // find leaf holding this doc
+ for (ScoreDoc hit : hits) {
+ while (hit.doc >= leafEnd) {
+ if (currLeaf == leaves.size() - 1) {
+ throw new IllegalStateException(
+ "hit docId="
+ + hit.doc
+ + "greater than last searcher leaf maxDoc="
+ + leafEnd
+ + " Ensure firstPassTopDocs were produced by the searcher provided to rescore.");
+ }
+ ctx = leaves.get(++currLeaf);
+ leafEnd = ctx.docBase + ctx.reader().maxDoc();
+ }
+
+ int targetDoc = hit.doc - ctx.docBase;
+ DoubleValues values = source.getValues(ctx, null);
+ boolean scorePresent = values.advanceExact(targetDoc);
+ double secondPassScore = scorePresent ? values.doubleValue() : 0.0f;
+ hit.score = combine(hit.score, scorePresent, secondPassScore);
+ }
+
+ if (topN < hits.length) {
+ ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.scoreDocComparator);
+ ScoreDoc[] subset = new ScoreDoc[topN];
+ System.arraycopy(hits, 0, subset, 0, topN);
+ hits = subset;
+ }
+ Arrays.sort(hits, ScoreDoc.scoreDocComparator);
+
+ return new TopDocs(firstPassTopDocs.totalHits, hits);
+ }
+
+ @Override
+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
+ throws IOException {
+ Explanation first =
Review Comment:
My flow was to produce first pass explanation, then second explanation, then combined score and final explanation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "vigyasharma (via GitHub)" <gi...@apache.org>.
vigyasharma merged PR #14776:
URL: https://github.com/apache/lucene/pull/14776
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "dungba88 (via GitHub)" <gi...@apache.org>.
dungba88 commented on code in PR #14776:
URL: https://github.com/apache/lucene/pull/14776#discussion_r2144454143
##########
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.ArrayUtil;
+
+/** A {@link Rescorer} that uses provided DoubleValuesSource to rescore first pass hits. */
+public abstract class DoubleValuesSourceRescorer extends Rescorer {
+
+ private final DoubleValuesSource valuesSource;
+
+ public DoubleValuesSourceRescorer(DoubleValuesSource valuesSource) {
+ this.valuesSource = valuesSource;
+ }
+
+ /**
+ * Implement this in a subclass to combine the first pass scores with values from the
+ * DoubleValuesSource
+ *
+ * @param firstPassScore Score from firstPassTopDocs
+ * @param valuePresent true if DoubleValuesSource has a value for the hit from first pass
+ * @param sourceValue Value returned from DoubleValuesSource
+ */
+ protected abstract float combine(float firstPassScore, boolean valuePresent, double sourceValue);
+
+ @Override
+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)
+ throws IOException {
+ DoubleValuesSource source = valuesSource.rewrite(searcher);
+ // this will still alter scores, we clone to retain hits ordering in firstPassTopDocs
+ ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();
+ Arrays.sort(hits, (a, b) -> a.doc - b.doc);
+
+ List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
+ LeafReaderContext ctx = leaves.getFirst();
+ int currLeaf = 0;
+ int leafEnd = ctx.docBase + ctx.reader().maxDoc();
+
+ // find leaf holding this doc
+ for (ScoreDoc hit : hits) {
+ while (hit.doc >= leafEnd) {
+ if (currLeaf == leaves.size() - 1) {
+ throw new IllegalStateException(
+ "hit docId="
+ + hit.doc
+ + "greater than last searcher leaf maxDoc="
+ + leafEnd
+ + " Ensure firstPassTopDocs were produced by the searcher provided to rescore.");
+ }
+ ctx = leaves.get(++currLeaf);
+ leafEnd = ctx.docBase + ctx.reader().maxDoc();
+ }
+
+ int targetDoc = hit.doc - ctx.docBase;
+ DoubleValues values = source.getValues(ctx, null);
+ boolean scorePresent = values.advanceExact(targetDoc);
+ double secondPassScore = scorePresent ? values.doubleValue() : 0.0f;
+ hit.score = combine(hit.score, scorePresent, secondPassScore);
+ }
+
+ if (topN < hits.length) {
+ ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.scoreDocComparator);
+ ScoreDoc[] subset = new ScoreDoc[topN];
+ System.arraycopy(hits, 0, subset, 0, topN);
+ hits = subset;
+ }
+ Arrays.sort(hits, ScoreDoc.scoreDocComparator);
+
+ return new TopDocs(firstPassTopDocs.totalHits, hits);
+ }
+
+ @Override
+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
+ throws IOException {
+ Explanation first =
+ Explanation.match(
+ firstPassExplanation.getValue(), "first pass score", firstPassExplanation);
+
+ LeafReaderContext leafWithDoc = null;
+ for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
+ if (docID >= ctx.docBase && docID < (ctx.docBase + ctx.reader().maxDoc())) {
+ leafWithDoc = ctx;
+ break;
+ }
+ }
+ if (leafWithDoc == null) {
+ throw new IllegalArgumentException(
Review Comment:
Gotcha, thanks for the explanation. Maybe we can add some nuance you mentioned above to the error message, if the only possible case is a different IndexSearcher was passed? Anyhow, no strong opinion here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "vigyasharma (via GitHub)" <gi...@apache.org>.
vigyasharma commented on code in PR #14776:
URL: https://github.com/apache/lucene/pull/14776#discussion_r2144284445
##########
lucene/core/src/java/org/apache/lucene/search/DoubleValuesSourceRescorer.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.ArrayUtil;
+
+/** A {@link Rescorer} that uses provided DoubleValuesSource to rescore first pass hits. */
+public abstract class DoubleValuesSourceRescorer extends Rescorer {
+
+ private final DoubleValuesSource valuesSource;
+
+ public DoubleValuesSourceRescorer(DoubleValuesSource valuesSource) {
+ this.valuesSource = valuesSource;
+ }
+
+ /**
+ * Implement this in a subclass to combine the first pass scores with values from the
+ * DoubleValuesSource
+ *
+ * @param firstPassScore Score from firstPassTopDocs
+ * @param valuePresent true if DoubleValuesSource has a value for the hit from first pass
+ * @param sourceValue Value returned from DoubleValuesSource
+ */
+ protected abstract float combine(float firstPassScore, boolean valuePresent, double sourceValue);
+
+ @Override
+ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int topN)
+ throws IOException {
+ DoubleValuesSource source = valuesSource.rewrite(searcher);
+ // this will still alter scores, we clone to retain hits ordering in firstPassTopDocs
+ ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();
+ Arrays.sort(hits, (a, b) -> a.doc - b.doc);
+
+ List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
+ LeafReaderContext ctx = leaves.getFirst();
+ int currLeaf = 0;
+ int leafEnd = ctx.docBase + ctx.reader().maxDoc();
+
+ // find leaf holding this doc
+ for (ScoreDoc hit : hits) {
+ while (hit.doc >= leafEnd) {
+ if (currLeaf == leaves.size() - 1) {
+ throw new IllegalStateException(
+ "hit docId="
+ + hit.doc
+ + "greater than last searcher leaf maxDoc="
+ + leafEnd
+ + " Ensure firstPassTopDocs were produced by the searcher provided to rescore.");
+ }
+ ctx = leaves.get(++currLeaf);
+ leafEnd = ctx.docBase + ctx.reader().maxDoc();
+ }
+
+ int targetDoc = hit.doc - ctx.docBase;
+ DoubleValues values = source.getValues(ctx, null);
+ boolean scorePresent = values.advanceExact(targetDoc);
+ double secondPassScore = scorePresent ? values.doubleValue() : 0.0f;
+ hit.score = combine(hit.score, scorePresent, secondPassScore);
+ }
+
+ if (topN < hits.length) {
+ ArrayUtil.select(hits, 0, hits.length, topN, ScoreDoc.scoreDocComparator);
+ ScoreDoc[] subset = new ScoreDoc[topN];
+ System.arraycopy(hits, 0, subset, 0, topN);
+ hits = subset;
+ }
+ Arrays.sort(hits, ScoreDoc.scoreDocComparator);
+
+ return new TopDocs(firstPassTopDocs.totalHits, hits);
+ }
+
+ @Override
+ public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
+ throws IOException {
+ Explanation first =
+ Explanation.match(
+ firstPassExplanation.getValue(), "first pass score", firstPassExplanation);
+
+ LeafReaderContext leafWithDoc = null;
+ for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
+ if (docID >= ctx.docBase && docID < (ctx.docBase + ctx.reader().maxDoc())) {
+ leafWithDoc = ctx;
+ break;
+ }
+ }
+ if (leafWithDoc == null) {
+ throw new IllegalArgumentException(
Review Comment:
Yeah, this is a tricky one. Since this explain API can take any docId, I can see the argument of surfacing a "noMatch" here. However, my thinking is that `Rescorer#explain` API is meant to explain how the _Rescorer_ would score this docId for given searcher, when `rescore()` is called.
If we hit null here, it means that the docId was not matched by the provided searcher in the first-pass itself. This is likely because a different searcher was passed than the one that produced provided docId, which is an error that we should surface.
Suppose we say `noMatch` for first pass, and the docId does have a value in DoubleValuesSource. Then the explain API will return a score for the docId, However, the actual rescore API will never return a score for this (searcher, docId) combination because they don't match.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add a rescorer that uses DoubleValuesSource values to re-score first pass hits [lucene]
Posted by "romseygeek (via GitHub)" <gi...@apache.org>.
romseygeek commented on code in PR #14776:
URL: https://github.com/apache/lucene/pull/14776#discussion_r2145919558
##########
lucene/core/src/java/org/apache/lucene/search/ScoreDoc.java:
##########
@@ -51,4 +52,16 @@ public ScoreDoc(int doc, float score, int shardIndex) {
public String toString() {
return "doc=" + doc + " score=" + score + " shardIndex=" + shardIndex;
}
+
+ /** Utility comparator that sorts by score descending, then by docId ascending */
+ public static final Comparator<ScoreDoc> scoreDocComparator =
Review Comment:
Nit: constants are usually in uppercase, so I'd rename this to just `COMPARATOR` as it will always be accessed like `ScoreDoc.COMPARATOR`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org