{"record":{"id":"c0900f935354a1b5","repo":"dotnet/efcore","slug":"ordering-using-a-scoring-function-is-mutually-excl","errorCode":null,"errorMessage":"Ordering using a scoring function is mutually exclusive with other forms of ordering.","messagePattern":"Ordering using a scoring function is mutually exclusive with other forms of ordering\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs","lineNumber":484,"sourceCode":"        _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 } }];\n            var appendingScoringFunctionOrdering = orderingExpression.Expression is SqlFunctionExpression { IsScoringFunction: true };\n            if (appendingScoringFunctionOrdering || existingScoringFunctionOrdering)\n            {\n                throw new InvalidOperationException(\n                    appendingScoringFunctionOrdering && existingScoringFunctionOrdering\n                        ? CosmosStrings.OrderByMultipleScoringFunctionWithoutRrf(nameof(CosmosDbFunctionsExtensions.Rrf))\n                        : CosmosStrings.OrderByScoringFunctionMixedWithRegularOrderby);\n            }\n        }\n\n        if (_orderings.FirstOrDefault(o => o.Expression.Equals(orderingExpression.Expression)) == null)\n        {\n            _orderings.Add(orderingExpression);\n        }\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>","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L466-L502","documentation":"SelectExpression.AppendOrdering throws InvalidOperationException when a scoring-function ordering is mixed with a regular (non-scoring) ordering in the same query. The two ordering modes are mutually exclusive on Cosmos: you either order by one scoring function or by regular expressions, never both.","triggerScenarios":"Mixing modes, e.g. `.OrderBy(a => a.Published).OrderBy(a => EF.Functions.FullTextScore(...))` or the reverse — appending a regular ordering after a scoring one (or vice versa).","commonSituations":"Adding a deterministic tie-breaker property alongside a relevance score; porting a multi-key relational OrderBy to a full-text Cosmos query.","solutions":["Choose one ordering mode: either a single scoring function (optionally fused via RRF) OR regular property ordering — not both.","Move the secondary sort client-side after materializing the server results.","If relevance must be primary and a property secondary, accept server relevance ordering and re-sort the top-N client-side."],"exampleFix":"// before (mixed)\nvar q = ctx.Articles\n    .OrderBy(a => a.Published)\n    .OrderBy(a => EF.Functions.FullTextScore(a.Body, \"x\"));\n\n// after (scoring only; tie-break client-side)\nvar q = ctx.Articles\n    .OrderBy(a => EF.Functions.FullTextScore(a.Body, \"x\"));\n// then: results.OrderByDescending(r => r.Published) in memory","handlingStrategy":"validation","validationCode":"// Decide ordering mode up front: scoring XOR regular, never both.\nenum OrderMode { Scoring, Regular }\nstatic IQueryable<T> ApplyOrdering<T>(IQueryable<T> q, OrderMode mode) => mode switch\n{\n    OrderMode.Scoring => q.OrderBy(/* scoring fn */),\n    _ => q.OrderBy(/* property */),\n};","typeGuard":null,"tryCatchPattern":"try { var q = query.ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"mutually exclusive\"))\n{ /* drop one of the orderings or move tie-break client-side */ }","preventionTips":["Never mix scoring and regular ordering in one Cosmos query.","Do secondary sorting client-side after retrieving results."],"tags":["cosmos","full-text-search","ordering","scoring-function"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}