SERVER-125724 Error when only combination.expression specified (#53590)

GitOrigin-RevId: 0d956accb2cb14693dd4a11a50ef68f4992021fd
This commit is contained in:
Adithi Raghavan 2026-05-13 09:46:19 -04:00 committed by MongoDB Bot
parent 5c10467529
commit 0289f65aed
2 changed files with 76 additions and 9 deletions

View File

@ -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

View File

@ -41,6 +41,8 @@
#include "mongo/db/pipeline/expression_context.h"
#include "mongo/db/pipeline/pipeline.h"
#include <string>
#include <boost/smart_ptr/intrusive_ptr.hpp>
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<IDLAnyType> 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()));