使用ioredis向redis发送任意命令

Sending arbitrary commands to redis using ioredis

是否可以使用 ioredis for Node JS 向 Redis 发送任意命令?

例如,我正在使用新的 RediSearch 模块,并希望使用以下命令创建索引:

FT.CREATE test SCHEMA title TEXT WEIGHT 5.0

如何使用 ioredis 发送此命令?

这会让你到达那里,虽然不确定响应编码:

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'
        }
    )
);

如果您愿意搜索支持所有 RediSearch 命令的node_redis, there is a pre-built RediSearch plugin。 (披露:我写的)

或者,这些变体也可以工作:

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) { /* ... */ });