我正在使用带有 RedisJSON 模块和 RediSearch 的 Redis,并且我有一些条目,如下所示。
JSON.SET uuid_1 $ '{ "type": "game1", "players": ["player1", "player2"] }'
JSON.SET uuid_2 $ '{ "type": "game1", "players": ["player3", "player2"] }'
我想要实现的是某种查询,当此类数组包含一些目标元素时,它会返回 uuid 和整个数组。
例如,当我查询时:
player1
-> [uuid_1
, ["玩家", ["玩家1", "玩家2"] ]player2
-> [uuid_2
, ["玩家", ["玩家3", "玩家2"]].我还查看了this答案,但我无法在我的案例中完全重现它。
我能够得到的是首先创建索引:
FT.CREATE idx ON JSON SCHEMA '$.players[*]' AS players TAG $.type AS gameType TEXT
然后只检索 uuid:
127.0.0.1:6379> FT.SEARCH idx @players:{player2} NOCONTENT
1) (integer) 2
2) "uuid_1"
3) "uuid_2"
或整个钥匙
127.0.0.1:6379> FT.SEARCH idx @players:{player2}
1) (integer) 2
2) "uuid_1"
3) 1) "$"
2) "{\"type\":\"game1\",\"players\":[\"player1\",\"player2\"]}"
4) "uuid_2"
5) 1) "$"
2) "{\"type\":\"game1\",\"players\":[\"player3\",\"player2\"]}"
不幸的是,如果我只想检索
player
属性,似乎是不完整的,它只返回第一个元素:
127.0.0.1:6379> FT.SEARCH idx @players:{player2} RETURN 1 players
1) (integer) 2
2) "uuid_1"
3) 1) "players"
2) "player1"
4) "uuid_2"
5) 1) "players"
2) "player"
127.0.0.1:6379> FT.SEARCH idx @players:{player1} RETURN 1 players
1) (integer) 1
2) "uuid_1"
3) 1) "players"
2) "player1"
这是早期搜索方言中的一个错误,并未本着保持向后兼容性的精神予以纠正。您需要告诉搜索至少使用 DIALECT 3 才能恢复整个数组。
127.0.0.1:6379> FT.SEARCH idx @players:{player2} RETURN 1 players DIALECT 3
1) (integer) 2
2) "uuid_1"
3) 1) "players"
2) "[\"player1\",\"player2\"]"
4) "uuid_2"
5) 1) "players"
2) "[\"player3\",\"player2\"]"
127.0.0.1:6379> FT.SEARCH idx @players:{player1} RETURN 1 players DIALECT 3
1) (integer) 1
2) "uuid_1"
3) 1) "players"
2) "[\"player1\",\"player2\"]"
您可以在启动 Redis 时设置默认方言,或者通过在 Redis 运行时设置配置选项来设置:
$ redis-server --loadmodule ./redisearch.so DEFAULT_DIALECT 3
FT.CONFIG SET DEFAULT_DIALECT 3
此外,还有一个方便的花花公子网页,其中包含您可能想知道的所有基本方言信息。