Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending arbitrary commands to redis using ioredis

Is it possible to send arbitrary commands to Redis using ioredis for Node JS?

For example, I'm using the new RediSearch module, and want to create an index using the command:

FT.CREATE test SCHEMA title TEXT WEIGHT 5.0

How would I send this command using ioredis?

like image 949
Jesse Reilly Avatar asked Aug 22 '17 02:08

Jesse Reilly


2 Answers

This will get you there, although not sure about the response encoding:

var 
    Redis = require('ioredis'),
    redis = new Redis('redis://:[yourpassword]@127.0.0.1');

redis.sendCommand(
    new Redis.Command(
        'FT.CREATE',
        ['test','SCHEMA','title','TEXT','WEIGHT','5.0'], 
        'utf-8', 
        function(err,value) {
          if (err) throw err;
          console.log(value.toString()); //-> 'OK'
        }
    )
);

If you're willing to search to node_redis, there is a pre-built RediSearch plugin that supports all the RediSearch commands. (Disclosure: I wrote it)

like image 81
stockholmux Avatar answered Nov 14 '22 02:11

stockholmux


Alternatively, these variants will work as well:

redis.call('M.CUSTOMCMD', ['arg1', 'arg2', 'arg3'], 
function(err, value) { /* ... */ });

// if you need batch custom/module commands
redis.multi([
  ['call', 'M.CUSTOMCMD', 'arg1', 'arg2', 'arg3'],
  ['call', 'M.OTHERCMD', 'arg-a', 'arg-b', 'arg-c', 'arg-d']
])
.exec(function(err, value) { /* ... */ });
like image 37
Zack Avatar answered Nov 14 '22 02:11

Zack