使用ioredis将任意命令发送到redis

时间:2017-08-22 02:43:42

标签: node.js redis ioredis

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

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

FT.CREATE test SCHEMA title TEXT WEIGHT 5.0

如何使用ioredis发送此命令?

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:1)

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

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