我可以对Redis值执行正则表达式搜索吗?

问题描述

我尝试使用RedisSearch,但是您可以在其中执行模糊搜索,但是我需要执行如下正则表达式搜索

key: "12345"
value: { name: "Maruti"}

搜索“ aru”将得到结果“孟买”,基本上形成的正则表达式为*aru*。谁能帮助我,如何使用Redis实现它?

解决方法

可以这样做,但是我不建议这样做-性能会受到很大影响。

但是,如果必须,您可以使用RedisGears来进行正则表达式查询,如下所示:

class CommentsController < ApplicationController

    def create
        @comment = Comment.new comment_params
        @comment.account_id = current_account.id
        
        respond_to do |format|
            format.js {
                if @comment.save 
                    @comments = Comment.where(post_id: @comment.post_id)
                    render "comments/create"

                else
                    # unable to save

                end
            }
        end
    end
    def comment_params
        params.require(:comment).permit(:message,:post_id)
    end

end

以下是提高可读性的Python代码:

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)