{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L444-L480","documentation":"ApplyOrdering in Cosmos SelectExpression throws when an ordering expression is a scoring function (IsScoringFunction, e.g. VectorDistance or FullTextScore) and IsAscending is false. Cosmos DB scoring orderings must be ascending; descending a score reverses its semantic and is rejected. The user must call OrderBy, not OrderByDescending, for scoring functions.","triggerScenarios":"Queryable.OrderByDescending(EF.Functions.VectorDistance(...)) or OrderByDescending(EF.Functions.FullTextScore(...)).","commonSituations":"Attempting to retrieve least-relevant results by descending the score; copy-pasting an OrderByDescending pattern that worked for a property onto a vector/full-text score.","solutions":["Use OrderBy instead of OrderByDescending for the scoring function.","If you genuinely need inverse-ranked results, redesign the query or apply client-side reversal after materialization (without Skip/Take)."],"exampleFix":"// before\nvar q = db.Items\n    .OrderByDescending(i => EF.Functions.VectorDistance(i.Embedding, query));\n\n// after\nvar q = db.Items\n    .OrderBy(i => EF.Functions.VectorDistance(i.Embedding, query));","handlingStrategy":"validation","validationCode":"// Reject OrderByDescending over scoring functions at the query builder boundary.\nstatic bool IsScoringExpression(Expression e) =>\n    e is MethodCallExpression m && m.Method.Name is \"VectorDistance\" or \"FullTextScore\";\n\nstatic bool IsValidOrdering(MethodCallExpression call) =>\n    !(call.Method.Name == nameof(Queryable.OrderByDescending) && IsScoringExpression(((UnaryExpression)call.Arguments[1]).Operand));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use OrderBy for all scoring-function orderings in Cosmos queries.","Add a code-review/lint check that flags OrderByDescending(EF.Functions.VectorDistance|FullTextScore).","Document in your data-access layer that scoring orderings are ascending-only."],"tags":["cosmos","ordering","scoring-function","vector-search"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}