redis / redis

Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps.
http://redis.io
Other
66.78k stars 23.77k forks source link

Want the data in sorted manner based on their scores from sorted set. #7477

Open rajneran-quot opened 4 years ago

rajneran-quot commented 4 years ago

ZADD add the data in sorted set with score and some value while fetching the data wanted the data to be sorted based on their scores. Tried with ZSCAN but didn't get desired result

oranagra commented 4 years ago

i think you're looking for https://redis.io/commands/zrange

rajneran-quot commented 4 years ago

i think you're looking for https://redis.io/commands/zrange

zrange gives the data in a given range but I want to get the data based on some regex that should give all the string which follow that regex with sorted score. For Example :

ZSCAN test3 0 MATCH t 1) "200" 2) 1) "lakeside-collection|Lakeside_Collection" 2) "84353" 3) "tomtom|TomTom" 4) "183" 5) "kaplan|Kaplan_Test_Prep" 6) "62493" 7) "frenchtoast|Frenchtoast" 8) "76651" 9) "hostelbookers|HostelBookers" 10) "473"

If you observe the result here the string is coming with their scores but that is not in sorted manner.

oranagra commented 4 years ago

that's right, ZRANGE can give you the entire sorted set, but with no filtering. and ZSCAN doesn't do sorting. i suppose the only thing you can do is use ZRANGE inside a Lua script which will do the filtering for you. If you also want to scan it alliteratively rather than get the whole set in one go, that would also complicate things.

rajneran-quot commented 4 years ago

that's right, ZRANGE can give you the entire sorted set, but with no filtering. and ZSCAN doesn't do sorting. i suppose the only thing you can do is use ZRANGE inside a Lua script which will do the filtering for you. If you also want to scan it alliteratively rather than get the whole set in one go, that would also complicate things.

Since wanted to implement autocompletion so fetching the data each time for different regex that would be highly costly task.

oranagra commented 4 years ago

@rajneran-quot so your LUA script can fetch just the first n results, and even do incremental calls to LRANGE and abort the "scan" when enough results were collected.

i'm guessing there are better way, maybe involving a module. @itamarhaber might be able to point to something.

rajneran7890 commented 4 years ago

@itamarhaber can you check and let me know is there any better way ?

itamarhaber commented 4 years ago

Hello @rajneran7890

As @oranagra said, I'd go with a Lua script to implement this using vanilla Redis. Alternatively, you can extend Redis with the RediSearch module and use its autocomplete feature (i.e SUGADD and SUGGET).

rajneran-quot commented 4 years ago

@itamarhaber MODULE LOAD redisearch.so OPT1 OPT2 (error) ERR unknown command MODULE, with args beginning with: LOAD, redisearch.so, OPT1, OPT2,

getting this while loading the redissearch.so any idea why so ?

itamarhaber commented 4 years ago

Redis modules were added to Redis v4 - which version are you using?

rajneran-quot commented 4 years ago

redis_version:4.0.14

itamarhaber commented 4 years ago

redis_version:4.0.14

This doesn't make sense... unless you're using a managed Redis service where this functionality has been turned off? Although if that's the case I'm not sure how you got the .so file there in the first place.