How to create LIKE operator search in Redis cache using nodejs?

依然范特西╮ 提交于 2019-12-25 04:14:18

问题


I have a question, is it possible to create a LIKE operator search in Redis? Similar to relational (mysql/oracle) database.

I have complex json:

   {"_id" : ObjectId("581c8b8854fdcd1ff8c944e0"),
"Objectcode" : "xxxxx",
"Objecttype" : "xxxx",
"docid" : "581c8b8554fdcd1ff8c93d10",
"description" : "Tags based search .... ",
"metaTags" : [ 
    "tag1", 
    "tag2", 
    "tag3", 
    "tag5", 
    "tag6", 
    "tag7", 
    "tag8", 
    "tag9", 
    "tag10"
],
"__v" : 0

}

and i want to search on array of metaTags how can i do it?

Thanks


回答1:


You can use MATCH commands to search data.

Eg: scan 0 MATCH *11*

Refer: http://redis.io/commands/scan




回答2:


You can use Redis *SCAN commands http://redis.io/commands/scan, depending of your type of data to filter by a pattern:

  • SCAN iterates the set of keys in the currently selected Redis database.
  • SSCAN iterates elements of Sets types.
  • HSCAN iterates fields of Hash types and their associated values.
  • ZSCAN iterates elements of Sorted Set types and their associated scores.

Never use KEYS in app code, because it may ruin performance.

The two major nodejs redis client libraries node_redis and ioredis support it, with some syntax sugar:

const keys = [];
const redis = new Redis(); // ioredis
redis.mset('foo1', 1, 'foo2', 1, 'foo3', 1, 'foo4', 1, 'foo10', 1, () => {
  const stream = redis.scanStream();
  stream.on('data', data => {
    keys = keys.concat(data);
  });
  stream.on('end', () => {
    assert.equal(keys.sort(), ['foo1', 'foo10', 'foo2', 'foo3', 'foo4']);
  });
});


来源:https://stackoverflow.com/questions/40761383/how-to-create-like-operator-search-in-redis-cache-using-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!