javascriptredisioredis

How to use sort command with ioredis?


await ioredis.sort(
    itemsByViewsKey(),
    "BY",
    "nosort",
    "LIMIT",
    0,
    10,
    "GET",
    `${itemsKey("*")}->name`,
    `${itemsKey("*")}->views`,
    order
  );

I want to sort the above logic by nosort, then add limit with the provided offset and count and Get the name and views. This produces this error message:

error - ReplyError: ERR unknown command `sort_ro`, with args beginning with: `items:views`, `BY`, `nosort`, `LIMIT`, `0`, `10`, `GET`, `items#*->name`, `items#*->views`, `DESC`, 
    at parseError (/Users/transformhub/Desktop/VID/Redis/rbay/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/Users/transformhub/Desktop/VID/Redis/rbay/node_modules/redis-parser/lib/parser.js:302:14) {
  command: {
    name: 'sort_ro',
    args: [
      'items:views',
      'BY',
      'nosort',
      'LIMIT',
      '0',
      '10',
      'GET',
      'items#*->name',
      'items#*->views',
      'DESC'
    ]
  },
  page: '/api/itemsByViews'
}

I also tried sort_ro but I still get an error saying:

error - ReplyError: ERR unknown command `sort_ro`, with args beginning with: `items:views`, `BY`, `nosort`, `LIMIT`, `0`, `10`, `GET`, `items#*->name`, `items#*->views`, `DESC`, 
    at parseError (/Users/transformhub/Desktop/VID/Redis/rbay/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/Users/transformhub/Desktop/VID/Redis/rbay/node_modules/redis-parser/lib/parser.js:302:14) {
  command: {
    name: 'sort_ro',
    args: [
      'items:views',
      'BY',
      'nosort',
      'LIMIT',
      '0',
      '10',
      'GET',
      'items#*->name',
      'items#*->views',
      'DESC'
    ]
  },
  page: '/api/itemsByViews'
}

The is unclear on how to do this.


Solution

  • I just test this code with some sample data I created. It seems to do as you wish:

    const Redis = require("ioredis")
    const redis = new Redis()
    
    const redisDemo = async () => {
    
            // Creating the entries
            await redis.hset("items_1", "name", "orange")
            await redis.hset("items_1", "views", "34")
    
            await redis.hset("items_2", "name", "apple")
            await redis.hset("items_2", "views", "12")
    
            // Add the ids to a list
            await redis.lpush('ids', 1)
            await redis.lpush('ids', 2)
    
            // Sort using the ratings
            res = await redis.sort('ids', "BY", "nosort",
                    "LIMIT", 0, 10,
                    "GET", "items_*->name",
                    "GET", "items_*->views",
                    "desc")
            console.log(res)
    };
    
    // Call demo
    redisDemo();
    

    But to know for sure, I'd need to have some sample data (2 items like my sample data).

    The main item I noticed when testing is you need a GET for every result. You are missing one:

      'GET',
      'items#*->name',
      'items#*->views',