Sending arbitrary commands to redis using ioredis
Asked Answered
N

2

5

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?

Nephrotomy answered 22/8, 2017 at 2:43 Comment(0)
A
9

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)

Aerophyte answered 24/8, 2017 at 20:0 Comment(1)
Cheers - I'll try this out when I'm back at the office next week. Same with the RediSearch module!Nephrotomy
C
4

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) { /* ... */ });
Crossindex answered 30/10, 2017 at 19:16 Comment(1)
Don't forget to add call which execute the module.Glume

© 2022 - 2024 — McMap. All rights reserved.