Search code examples
redisredisearch

Are the results of FT.SEARCH ordered by their scores by default?


According to the documentation of FT.SEARCH, it has a WITHSCORES argument to get the internal scores of the documents, and a SCORER argument to specify a function for scoring the documents, such as the default TFIDF for texts (according to Scoring Documents). However, it does not explicitly mention that the scores are used to sort the documents, though the results do appear to be sorted by the scores in my experiments. Thus, I wonder whether sorting by the scores is indeed the default behavior?

FT.SEARCH also has a SORTBY argument, which sorts the results by some attribute. I wonder instead of specifying an attribute for sorting, is it possible to use it to explicitly state that I want to sort by the scores of the documents? On the other hand, if sorting the results is the default behavior, I guess it should also be possible to disable it? Then how to do that? Because it also mentioned an optimization:

Skip Sorter - applied when there is no sort of any kind. The query can return after it reaches the LIMIT requested results.


Solution

  • Yes.

    By default, FT.SEARCH sorts the results by their text score, and return the top 10. You can use the LIMIT option to set another offset or limit (default is equivalent to LIMIT 0 10). You can use an explicit SORTBY option if you want to sort by something else. FT.SEARCH always has a sorter (beside for optimizations, but logically it has) and there is no way to not have one.

    FT.AGGREGATE however does not have any kind of sorting by default. You use SORTBY explicitly by any field or query related value. On the latest 2.10 version you can also use ADDSCORES to have the text scores available and then sort by __score