Skip to content

Add null check for FunctionFactorScorer - #22653

Open
rohanbadgujar20011 wants to merge 1 commit into
opensearch-project:mainfrom
rohanbadgujar20011:fix/rohan_functionscore-null-check22634]
Open

Add null check for FunctionFactorScorer#22653
rohanbadgujar20011 wants to merge 1 commit into
opensearch-project:mainfrom
rohanbadgujar20011:fix/rohan_functionscore-null-check22634]

Conversation

@rohanbadgujar20011

Copy link
Copy Markdown

Added null check for scorer before advancing document.

Description

[Describe what this change achieves]

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • Functionality includes testing.
  • API changes companion pull request created, if applicable.
  • Public documentation issue/PR created, if applicable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Added null check for scorer before advancing document.

Signed-off-by: Rohan Badgujar <80501166+rohanbadgujar20011@users.noreply.github.com>
@rohanbadgujar20011
rohanbadgujar20011 requested a review from a team as a code owner August 5, 2026 17:21
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🧪 No relevant tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ Recommended focus areas for review

Inconsistent doc parameter

After scorer.iterator().advance(doc), the returned actualDoc may differ from doc (e.g., when the scorer's iterator skips ahead). The original code asserted equality, but if the null-check path indicates the scorer may not always align, computeScore is still called with doc rather than actualDoc. If the intent of the null check is to handle scorer mismatches, computeScore should likely use actualDoc, or the explanation should return noMatch when actualDoc != doc.

int actualDoc = scorer.iterator().advance(doc);
assert actualDoc == doc;

double score = scorer.computeScore(
    doc,
    expl.getValue().floatValue()
);
Possible Compilation Error

The variable score is referenced in Explanation.match((float) score, ...) but its declaration/assignment has been moved inside a nested block scope (the added indentation suggests an extra block). If score is declared inside that inner scope, it will be out of scope where factorExplanation uses it, causing a compile error. Verify the brace structure matches the indentation shown.

    if (scorer == null) {
        return Explanation.noMatch(
            "No matching scorer for segment"
        );
    }

    int actualDoc = scorer.iterator().advance(doc);
    assert actualDoc == doc;

    double score = scorer.computeScore(
        doc,
        expl.getValue().floatValue()
    );

factorExplanation = Explanation.match(
    (float) score,

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Avoid early return breaking explanation flow

Returning Explanation.noMatch early bypasses the surrounding Explanation.match
construction that includes functionsExplanations and the score mode label, producing
an inconsistent explanation. Instead, assign a fallback factorExplanation (e.g.,
Explanation.noMatch(...)) so that the outer combining logic below still executes and
returns a properly composed explanation.

server/src/main/java/org/opensearch/common/lucene/search/function/FunctionScoreQuery.java [475-490]

 FunctionFactorScorer scorer = functionScorer(context);
-
-    if (scorer == null) {
-        return Explanation.noMatch(
-            "No matching scorer for segment"
-        );
-    }
-    
+if (scorer == null) {
+    factorExplanation = Explanation.noMatch("No matching scorer for segment", functionsExplanations);
+} else {
     int actualDoc = scorer.iterator().advance(doc);
     assert actualDoc == doc;
-    
-    double score = scorer.computeScore(
-        doc,
-        expl.getValue().floatValue()
+    double score = scorer.computeScore(doc, expl.getValue().floatValue());
+    factorExplanation = Explanation.match(
+        (float) score,
+        "function score, score mode [" + scoreMode.toString().toLowerCase(Locale.ROOT) + "]",
+        functionsExplanations
     );
+}
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies that the early return Explanation.noMatch(...) bypasses the outer combining logic that builds the full explanation with functionsExplanations and score mode, and proposes a cleaner fallback assignment. However, the score variable's declaration/scoping in the outer block also needs consideration, so the fix is directionally correct but not entirely complete.

Medium

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

❌ Gradle check result for f72d7c1: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant