redisspring-data-redisredis-search

RediSearch - Get the JSON objects sorted in the same way after update of any field


I have created an index on RediSearch that includes multiple fields, for example param1 and param2.

I perform the search using FT.SEARCH executing the following command.

FT.SEARCH "index" "@id:{4555bcbd\\-1d15\\-46f1\\-9acb\\-91e29c1f63f5} @param1:{ee2aa256\\-5c67\\-42cb\\-871f\\-a402fcdf2160}" "SORTBY" "param2"  DESC LIMIT 0 3000

The search produces a list of objects. When I update for example param2 and execute the above command, the list changes. The latest updated object appears at the top of the list.

I tried to use multiple sorting fields but FT.SEARCH does not support sorting by multiple fields.


Solution

  • My custom solution included adding a index field for every object I store. Then, use FT.AGGREGATE function to query and sort by multiple fields param2 and index. Because in constract to FT.SEARCH, FT.AGGREGATE supports multiple field sorting.

    FT.AGGREGATE "index" "@idRicerca:{4555bcbd\\-1d15\\-46f1\\-9acb\\-91e29c1f63f5} @bankId:{ee2aa256\\-5c67\\-42cb\\-871f\\-a402fcdf2160}" 
    LOAD "2" "@idRicerca" "@bankId" 
    LIMIT "0" "3000" 
    SORTBY "4" "@bankName" "DESC" "@index" "DESC"