{"record":{"id":"06ea8fcc7ab421b8","repo":"dotnet/efcore","slug":"ordering-based-on-scoring-function-is-not-supporte","errorCode":null,"errorMessage":"Ordering based on scoring function is not supported inside '{orderByDescending}'. Use '{orderBy}' instead.","messagePattern":"Ordering based on scoring function is not supported inside '(.+?)'\\. Use '(.+?)' instead\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs","lineNumber":462,"sourceCode":"    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public void ApplyOffset(SqlExpression sqlExpression)\n        => Offset = sqlExpression;\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public void ApplyOrdering(OrderingExpression orderingExpression)\n    {\n        if (orderingExpression is { Expression: SqlFunctionExpression { IsScoringFunction: true }, IsAscending: false })\n        {\n            throw new InvalidOperationException(\n                CosmosStrings.OrderByDescendingScoringFunction(nameof(Queryable.OrderByDescending), nameof(Queryable.OrderBy)));\n        }\n\n        _orderings.Clear();\n        _orderings.Add(orderingExpression);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public void AppendOrdering(OrderingExpression orderingExpression)\n    {\n        if (_orderings.Count > 0)\n        {\n            var existingScoringFunctionOrdering = _orderings is [{ Expression: SqlFunctionExpression { IsScoringFunction: true } }];","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L444-L480","documentation":"Cosmos Full Text Search scoring functions (e.g. FullTextScore / RRF) rank by relevance, where a higher score means a better match, so they can only appear in ascending OrderBy. ApplyOrdering throws InvalidOperationException when the ordering expression is a scoring function but IsAscending is false. Use OrderBy (ascending) instead of OrderByDescending.","triggerScenarios":"Writing `query.OrderByDescending(x => EF.Functions.FullTextScore(...))` or any descending ordering whose expression is a scoring (IsScoringFunction) SqlFunctionExpression.","commonSituations":"Assuming descending relevance is allowed; copy-pasting a relational ORDER BY pattern into a Cosmos full-text query.","solutions":["Replace OrderByDescending(scoringFunction) with OrderBy(scoringFunction) — relevance is already most-relevant-first.","If you need least-relevant first, materialize the results and reverse client-side.","Double-check that the function you call is the scoring variant; non-scoring functions are ordered normally."],"exampleFix":"// before\nvar q = ctx.Articles\n    .OrderByDescending(a => EF.Functions.FullTextScore(a.Body, \"term\"));\n\n// after\nvar q = ctx.Articles\n    .OrderBy(a => EF.Functions.FullTextScore(a.Body, \"term\"));","handlingStrategy":"validation","validationCode":"// Static helper: scoring functions must be ordered ascending.\nstatic IQueryable<T> OrderByScoring<T>(IQueryable<T> q, Expression<Func<T, double>> score)\n    => q.OrderBy(score); // never OrderByDescending","typeGuard":null,"tryCatchPattern":"try { var q = ctx.Articles.OrderByDescending(a => Scoring(a)).ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"scoring function\"))\n{ /* switch to OrderBy */ }","preventionTips":["Always order scoring functions ascending.","Wrap full-text scoring ordering in a helper that forbids descending."],"tags":["cosmos","full-text-search","ordering","scoring-function"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}