From 0289f65aedfa978d68832c88a99c3cb85bd16073 Mon Sep 17 00:00:00 2001 From: Adithi Raghavan Date: Wed, 13 May 2026 09:46:19 -0400 Subject: [PATCH] SERVER-125724 Error when only combination.expression specified (#53590) GitOrigin-RevId: 0d956accb2cb14693dd4a11a50ef68f4992021fd --- .../document_source_score_fusion_test.cpp | 66 +++++++++++++++++++ .../score_fusion_pipeline_builder.cpp | 19 +++--- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/mongo/db/pipeline/document_source_score_fusion_test.cpp b/src/mongo/db/pipeline/document_source_score_fusion_test.cpp index 6a230fa36ae..764993131e6 100644 --- a/src/mongo/db/pipeline/document_source_score_fusion_test.cpp +++ b/src/mongo/db/pipeline/document_source_score_fusion_test.cpp @@ -8902,5 +8902,71 @@ TEST_F(DocumentSourceScoreFusionTest, InternalFieldBehaviorThroughGroupAndReshap ASSERT_TRUE(results[0]["__hs_name1_score"_sd].missing()); ASSERT_TRUE(results[1]["__hs_name1_score"_sd].missing()); } + +TEST_F(DocumentSourceScoreFusionTest, + ErrorsIfNoCombinationMethodButCombinationExpressionSpecified) { + auto spec = fromjson(R"({ + $scoreFusion: { + input: { + pipelines: { + name1: [ + { + $search: { + index: "search_index", + text: { + query: "mystery", + path: "genres" + } + } + }, + { $match : { author : "dave" } } + ] + }, + normalization: "none" + }, + combination: { + expression: {$sum: ["$$name1", 5.0]} + } + } + })"); + + ASSERT_THROWS_CODE(DocumentSourceScoreFusion::createFromBson(spec.firstElement(), getExpCtx()), + AssertionException, + 10017300); +} + +TEST_F(DocumentSourceScoreFusionTest, + ErrorsIfCombinationMethodAvgButCombinationExpressionSpecified) { + auto spec = fromjson(R"({ + $scoreFusion: { + input: { + pipelines: { + name1: [ + { + $search: { + index: "search_index", + text: { + query: "mystery", + path: "genres" + } + } + }, + { $match : { author : "dave" } } + ] + }, + normalization: "none" + }, + combination: { + method: "avg", + expression: {$sum: ["$$name1", 5.0]} + } + } + })"); + + ASSERT_THROWS_CODE(DocumentSourceScoreFusion::createFromBson(spec.firstElement(), getExpCtx()), + AssertionException, + 10017300); +} + } // namespace } // namespace mongo diff --git a/src/mongo/db/pipeline/score_fusion_pipeline_builder.cpp b/src/mongo/db/pipeline/score_fusion_pipeline_builder.cpp index b138cfd913f..5a087ec9574 100644 --- a/src/mongo/db/pipeline/score_fusion_pipeline_builder.cpp +++ b/src/mongo/db/pipeline/score_fusion_pipeline_builder.cpp @@ -41,6 +41,8 @@ #include "mongo/db/pipeline/expression_context.h" #include "mongo/db/pipeline/pipeline.h" +#include + #include namespace mongo { @@ -59,16 +61,15 @@ public: // The default combination method is avg if no combination method is specified. ScoreFusionCombinationMethodEnum combinationMethod = ScoreFusionCombinationMethodEnum::kAvg; boost::optional combinationExpression = boost::none; - if (combination.has_value() && combination->getMethod().has_value()) { - combinationMethod = combination->getMethod().get(); - uassert(10017300, - "combination.expression should only be specified when combination.method " - "has the value \"expression\"", - (combinationMethod != ScoreFusionCombinationMethodEnum::kExpression && - !combination->getExpression().has_value()) || - (combinationMethod == ScoreFusionCombinationMethodEnum::kExpression && - combination->getExpression().has_value())); + if (combination.has_value()) { + combinationMethod = + combination->getMethod().value_or(ScoreFusionCombinationMethodEnum::kAvg); combinationExpression = combination->getExpression(); + uassert(10017300, + "combination.expression must be specified if and only if combination.method " + "is \"expression\"", + combinationExpression.has_value() == + (combinationMethod == ScoreFusionCombinationMethodEnum::kExpression)); uassert(10017301, "both combination.expression and combination.weights cannot be specified", !(combination->getWeights().has_value() && combinationExpression.has_value()));