[英]Can I perform a regex search on Redis values?
I tried using RedisSearch but there you can perform a fuzzy search, but I need to perform a regex search like:我尝试使用 RedisSearch 但您可以执行模糊搜索,但我需要执行正则表达式搜索,例如:
key: "12345"
value: { name: "Maruti"}
searching "aru" will give the result "Mumbai", basically the regex formed is *aru*
.搜索“aru”将给出结果“Mumbai”,基本上形成的正则表达式是
*aru*
。 Can anyone help me out how can I achieve it using Redis?谁能帮助我如何使用 Redis 实现它?
This can be done, but I do not recommend it - performance will be greatly impacted.可以这样做,但我不建议这样做 - 性能会受到很大影响。
If you must, however, you can use RedisGears for ad-hoc regex queries like so:但是,如果必须,您可以使用RedisGears进行临时正则表达式查询,如下所示:
127.0.0.1:6379> HSET mykey name Maruti
(integer) 1
127.0.0.1:6379> HSET anotherkey name Moana
(integer) 1
127.0.0.1:6379> RG.PYEXECUTE "import re\np = re.compile('.*aru.*')\nGearsBuilder().filter(lambda x: p.match(x['value']['name'])).map(lambda x: x['key']).run()"
1) 1) "mykey"
2) (empty array)
Here's the Python code for readability:这是 Python 代码以提高可读性:
import re
p = re.compile('.*aru.*')
GearsBuilder() \
.filter(lambda x: p.match(x['value']['name'])) \
.map(lambda x: x['key']) \
.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.