Search code examples
sortinghashredissetredis-cli

sort set in redis with hash in the collection


We have created 3 hash in redis using the REPL redis-cli in this way:

hmset redishop:items:Articulo1 artist "Martin Wessely" price 12.99 name "Handcrafted Trees Mug" 
hmset redishop:items:Articulo2 artist "Martin Wessely" price 13.99 name "Handcrafted Trees Mug"
hmset redishop:items:Articulo3 artist "Martin Wessely" price 14.99 name "Handcrafted Trees Mug"

I check the structures are created OK in redis and these are there:

hgetall redishop:items:Articulo3

Now we add the hash in a set in this way:

sadd redishop:list-all redishop:items:Articulo3
sadd redishop:list-all redishop:items:Articulo2
sadd redishop:list-all redishop:items:Articulo1

Now we are playing with the command SORT:

SORT redishop:list-all BY redishop:items:*->price
SORT redishop:list-all BY redishop:items:*->price GET redishop:items:*->price
SORT redishop:list-all BY redishop:items:*->price GET # GET redishop:items:*->price

We never get results, the hash in the set are with value null and I dont understand why?

by other hand if we create the hash and set in this other way:

multi
hmset redishop:items:Articulo1 artist "Martin Wessely" price 12.99 name "Handcrafted Trees Mug" 
sadd redishop:list-all Articulo1
hmset redishop:items3:Articulo2 artist "Martin Wessely" price 13.99 name "Handcrafted Trees Mug"
sadd redishop:list-all Articulo2
hmset redishop:items3:Articulo3 artist "Martin Wessely" price 14.99 name "Handcrafted Trees Mug"
sadd redishop:list-all Articulo3
exec

In this way the command SORT works perfectly and the hash are inserted in the set, But I dont understand why in base of redis documentation:

  1. The command multi only Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC.

  2. When I create the hash with the key key:key:key is indifferent if I use : or , or - and the most important in redis we are not creating a tree of structures according the documentation: https://redis.io/topics/data-types-intro

They tell you is better or a good way include : or dots, but They don tell you he is creating a structures tree. And then I dont understadn why when you add the hash in the set if type Articulo1 instead of redishop:items:Articulo1 is Ok but in the oher case is wrong???? in fact when you type hgetall Articulo1 you receive a null but When you type hgetall redishop:items:Articulo1 you get all the fels an values.. it is so much strange.

  1. exec only executes all the sentences, for these reason should be the same make it with multi or without multi.

Please Any help or explanation on the subject would be of great help. Thanks in advance.


Solution

  • Now we are playing with the command SORT

    Beware of SORT's time complexity and memory requirements, I usually recommend against using it.

    We never get results, the hash in the set are with value null and I dont understand why?

    The problem lies with how you call SORT and specify the GET and BY clauses. Since your Set's members are the complete (Hash) key names, here's how you should do it with your example data:

    127.0.0.1:6379> SORT redishop:list-all BY *->price
    1) "redishop:items:Articulo1"
    2) "redishop:items:Articulo2"
    3) "redishop:items:Articulo3"
    127.0.0.1:6379> SORT redishop:list-all BY *->price GET *->price
    1) "12.99"
    2) "13.99"
    3) "14.99"
    

    In this way the command SORT works perfectly

    In this case you're populating the Set with only the "id" part of the key names, so the GET and BY clauses map to actual data. To clarify, this has nothing to do with the use (or lack) of MULTI blocks.