Hello,
I would like to move from FT.SEARCH to FT.AGGREGATE, and I don’t seem to be able to get the SORTBY syntax correct, as the results always appear to be sorted by the first SORTABLE field.
Descending sort by the LastName - so I would expect this to return 1,2,3
FT.AGGREGATE “users_hash_index_2” “*” “SORTBY” “2” “@LastName” “DESC” “APPLY” “@UserID” “AS” “UserID” “LIMIT” “0” “10”
(integer) 3
“UserID”
“2”
“UserID”
“3”
“UserID”
“1”
Based on what I am seeing it actually looks like it is returning the records in ascending or descending order of how they were entered, completely ignoring the fields I want to sort by.
I believe the general problem here is that the ‘LastName’ isn’t in brought into the aggregation pipeline before SORTBY. You need to do this via APPLY or LOAD and the sorting will be correct.
Works: FT.AGGREGATE users_hash_index_2 * APPLY @UserID as UserID APPLY @LastName as LastName SORTBY 2 @LastName asc
Works: FT.AGGREGATE users_hash_index_2 * APPLY @UserID as UserID APPLY @LastName as LastName SORTBY 2 @LastName desc
The side effect is that you’re also returning the LastName.
Also, tried Dvir’s suggestion and lowercasing doesn’t seem to help.
If that is the case it’s likely a bug. You don’t need apply for sorting by design. Of course my design itself may have changed but it should not be necessary unless for return purposes.
Adding the APPLY for all of the fields I want to include in the sort did the trick. One other thing I was missing was that the SORTBY needs to be after the APPLY otherwise it will not sort.
Hi Dvir,
I even tried changing the schema to be all lower case, but unless I use APPLY for each field I want to sort on, the SORTBY does not appear to work.
Another interesting problem I am seeing is that SORTBY needs to come after the APPLY, otherwise the SORTBY parameters will be ignored, unless this is done by design.