{"id":"9cce9db25af69e09","repo":"go-redis/redis","slug":"ft-aggregate-asc-and-desc-are-mutually-exclusive","errorCode":null,"errorMessage":"FT.AGGREGATE: ASC and DESC are mutually exclusive","messagePattern":"FT\\.AGGREGATE: ASC and DESC are mutually exclusive","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"search_commands.go","lineNumber":766,"sourceCode":"\t\targs = append(args, step.GroupBy.Fields...)\n\t\tfor _, reducer := range step.GroupBy.Reduce {\n\t\t\targs = append(args, \"REDUCE\", reducer.Reducer.String())\n\t\t\tif reducer.Args != nil {\n\t\t\t\targs = append(args, len(reducer.Args))\n\t\t\t\targs = append(args, reducer.Args...)\n\t\t\t} else {\n\t\t\t\targs = append(args, 0)\n\t\t\t}\n\t\t\tif reducer.As != \"\" {\n\t\t\t\targs = append(args, \"AS\", reducer.As)\n\t\t\t}\n\t\t}\n\tcase step.SortBy != nil:\n\t\targs = append(args, \"SORTBY\")\n\t\tsortByOptions := []interface{}{}\n\t\tfor _, sortBy := range step.SortBy.Fields {\n\t\t\tif sortBy.Asc && sortBy.Desc {\n\t\t\t\treturn args, fmt.Errorf(\"FT.AGGREGATE: ASC and DESC are mutually exclusive\")\n\t\t\t}\n\t\t\tsortByOptions = append(sortByOptions, sortBy.FieldName)\n\t\t\tif sortBy.Asc {\n\t\t\t\tsortByOptions = append(sortByOptions, \"ASC\")\n\t\t\t}\n\t\t\tif sortBy.Desc {\n\t\t\t\tsortByOptions = append(sortByOptions, \"DESC\")\n\t\t\t}\n\t\t}\n\t\targs = append(args, len(sortByOptions))\n\t\targs = append(args, sortByOptions...)\n\t\tif step.SortBy.Max > 0 {\n\t\t\targs = append(args, \"MAX\", step.SortBy.Max)\n\t\t}\n\t}\n\treturn args, nil\n}\n","sourceCodeStart":748,"sourceCodeEnd":784,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/search_commands.go#L748-L784","documentation":"Returned by appendFTAggregateStep (the Steps path) when a SortBy field inside an FTAggregateStep.SortBy.Fields entry has both Asc and Desc set to true. A SORTBY field can carry one direction; both is contradictory, so the builder refuses to serialize the step.","triggerScenarios":"FTAggregateStep{SortBy: &FTAggregateSortStep{Fields: []FTAggregateSortBy{{FieldName: \"price\", Asc: true, Desc: true}}}}. Triggered while building the SORTBY token list for a Step.","commonSituations":"Defaulting both flags true. UI toggles that allow both ASC and DESC selected. Reusing a SortBy value and flipping one flag without clearing the other.","solutions":["Set at most one of Asc or Desc per FTAggregateSortBy (leave both false for server-default ASC).","Audit the construction of step.SortBy.Fields to enforce mutual exclusion.","Map a single direction enum to Asc xor Desc before building the step."],"exampleFix":"// before\n{SortBy: &FTAggregateSortStep{Fields: []FTAggregateSortBy{{FieldName: \"price\", Asc: true, Desc: true}}}}\n// after\n{SortBy: &FTAggregateSortStep{Fields: []FTAggregateSortBy{{FieldName: \"price\", Desc: true}}}}","handlingStrategy":"validation","validationCode":"func validateStepSortDirections(s *FTAggregateSortStep) error {\n\tfor i, f := range s.Fields {\n\t\tif f.Asc && f.Desc {\n\t\t\treturn fmt.Errorf(\"SortBy.Fields[%d]: Asc and Desc both set\", i)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":"func directionUnique(f FTAggregateSortBy) bool { return !(f.Asc && f.Desc) }","tryCatchPattern":null,"preventionTips":["Represent direction as a single enum and map to Asc xor Desc.","When toggling one direction, clear the other.","Never default both flags to true."],"tags":["ft-aggregate","validation","sortby","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}