lua - 如何使用 Redis 将搜索文本与其他条件结合起来?

标签 lua redis

我使用 Redis 成功编写了文本搜索和其他条件的交集。为此,我使用了 Lua 脚本。问题是我不仅在阅读,而且还在从该脚本中写入值。从 Redis 3.2 开始,可以通过调用 redis.replicate_commands() 来实现这一点,但在 3.2 之前不行。

下面是我存储值的方式。

名字

> HSET product:name 'Cool product' 1
> HSET product:name 'Nice product' 2

价格

> ZADD product:price 49.90 1
> ZADD product:price 54.90 2

然后,为了获得所有匹配 'ice' 的产品,例如,我调用:

> HSCAN product:name 0 MATCH *ice*

但是,由于 HSCAN 使用游标,我必须多次调用它才能获取所有结果。这是我使用 Lua 脚本的地方:

local cursor = 0
local fields = {}
local ids = {}
local key = 'product:name'
local value = '*' .. ARGV[1] .. '*'

repeat
    local result = redis.call('HSCAN', key, cursor, 'MATCH', value)
    cursor = tonumber(result[1])
    fields = result[2]
    for i, id in ipairs(fields) do
        if i % 2 == 0 then
            ids[#ids + 1] = id
        end
    end
until cursor == 0
return ids

因为不可能将脚本的结果用于另一个调用,例如 SADD key EVAL(SHA) ...。而且,不可能在脚本中使用全局变量。我更改了字段循环内的部分以访问脚本外的 ID 列表:

if i % 2 == 0 then
    ids[#ids + 1] = id
    redis.call('SADD', KEYS[1], id)
end

我必须将 redis.replicate_commands() 添加到第一行。通过此更改,我可以从调用脚本时传递的 key 中获取所有 ID(请参阅 KEYS[1])。

最后,要获得 100 个产品 ID 的价格在 40 到 50 之间且名称包含“ice”的列表,我执行以下操作:

> ZUNIONSTORE tmp:price 1 product:price WEIGHTS 1
> ZREMRANGEBYSCORE tmp:price 0 40
> ZREMRANGEBYSCORE tmp:price 50 +INF
> EVALSHA b81c2b... 1 tmp:name ice
> ZINTERSTORE tmp:result tmp:price tmp:name
> ZCOUNT tmp:result -INF +INF
> ZRANGE tmp:result 0 100

我使用 ZCOUNT 调用来提前知道我将拥有多少结果页,执行 count/100

正如我之前所说,这与 Redis 3.2 配合得很好。但是当我尝试在 AWS 上运行代码时,它只支持 Redis 到 2.8,我无法让它工作了。我不确定如何在不使用脚本或不从脚本编写的情况下使用 HSCAN 光标进行迭代。有办法让它在 Redis 2.8 上运行吗?

一些注意事项:

  1. 我知道我可以在 Redis 之外进行部分处理(例如迭代游标或交叉匹配),但这会影响应用程序的整体性能。
  2. 我不想自己部署 Redis 实例来使用 3.2 版本。
  3. 上述标准(价格范围和名称)只是为了让事情简单化的示例。我还有其他字段和匹配类型,不仅如此。
  4. 我不确定我存储数据的方式是否是最好的方式。我愿意听取有关它的建议。

最佳答案

我在这里发现的唯一问题是将值存储在 lua scirpt 中。因此,与其将它们存储在 lua 中,不如将该值存储在 lua 外部(返回 string[] 的值)。使用 sadd (key,members[]) 将它们存储在不同调用中的集合中。然后进行交集并返回结果。

> ZUNIONSTORE tmp:price 1 product:price WEIGHTS 1
> ZREVRANGEBYSCORE tmp:price 0 40
> ZREVRANGEBYSCORE tmp:price 50 +INF
> nameSet[] = EVALSHA b81c2b... 1 ice 
> SADD tmp:name nameSet
> ZINTERSTORE tmp:result tmp:price tmp:name
> ZCOUNT tmp:result -INF +INF
> ZRANGE tmp:result 0 100

IMO 你的设计是最优化的。一个建议是尽可能使用管道,因为它会一次性处理所有内容。

希望对你有帮助

更新 lua中没有array([ ])之类的东西,必须用lua表来实现。在您的脚本中,您返回的是正确的 id,它本身是一个数组,您可以将它用作单独的调用来实现 sadd。

String [] nameSet = (String[]) evalsha b81c2b... 1 ice -> This is in java
SADD tmp:name nameSet

对应的lua脚本和你的第一个是一样的。

local cursor = 0
local fields = {}
local ids = {}
local key = 'product:name'
local value = '*' .. ARGV[1] .. '*'

repeat
    local result = redis.call('HSCAN', key, cursor, 'MATCH', value)
    cursor = tonumber(result[1])
    fields = result[2]
    for i, id in ipairs(fields) do
        if i % 2 == 0 then
            ids[#ids + 1] = id
        end
    end
until cursor == 0
return ids

关于lua - 如何使用 Redis 将搜索文本与其他条件结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231113/

相关文章:

c++ - 在 Lua 中调用 tolua++ 中可能未公开的函数

lua - lua表中最大条目数

java - Redis 值作为 byte[] 与纯字符串

node.js - Amazon ElasticCache for Redis 与 Node.js 服务器

lua - 如何从 C 结构创建 Lua 表

graphics - 在 Löve2D 中关闭抗锯齿

loops - 用于在表中创建变量的 Lua 循环

windows - cpp_redis::subscriber -> connect 导致异常:connect() 失败

lua - 多管齐下——Redis

redis - 如何在redis中设置用户名和密码?