Redis Insights中按键值过滤JSON对象的正确语法是什么?
Redis Insight查询布尔字段无效的解决方法
我有一个存储JSON对象的Redis实例,示例JSON对象如下:
{ ... description: "" metaPublishedOnDiscord: true metaIsHot: false }
我为这些对象创建了名为NewsArticles:index的索引,希望在Redis Insights中找出所有包含metaPublishedOnDiscord: false的JSON对象。尝试查询布尔字段metaPublishedOnDiscord为true的对象时,使用@metaPublishedOnDiscord:true在NewsArticle:index上查询无效。请问正确的查询语法是什么?
编辑补充信息
我使用旧版redis-om API创建索引,代码如下:
const Connection = require('./Connection'); const { Schema, Entity } = require('redis-om'); class NewsArticle extends Entity {}; const newsArticleSchema = new Schema(NewsArticle, { url: { type: 'string' }, img: { type: 'string' }, title: { type: 'text' }, description: { type: 'text' }, content: { type: 'text' }, tags: { type: 'string[]' }, cityId: { type: 'string' }, metaScrapedAtTimestamp: { type: 'date' }, metaPublishedOnDiscord: { type: 'boolean' }, metaIsHot: { type: 'boolean' } }); module.exports = Connection.start().then(c => { const newsArticleRepository = c.fetchRepository(newsArticleSchema); newsArticleRepository.createIndex(); return newsArticleRepository; });
额外信息:索引类型为STRING,被索引的对象类型为JSON。
正确查询语法及原理
有效查询语句
由于旧版redis-om会将布尔字段序列化为字符串类型存储,查询时需要匹配带引号的字符串值:
- 查询
metaPublishedOnDiscord为true的对象:@metaPublishedOnDiscord:"true" - 查询
metaPublishedOnDiscord为false的对象:@metaPublishedOnDiscord:"false"
原因解释
旧版redis-om在创建STRING类型索引时,会把Schema中定义的boolean类型字段转换为字符串格式("true"/"false")存入Redis。你之前使用的@metaPublishedOnDiscord:true是针对原生布尔值的查询语法,自然匹配不到字符串形式的存储值,必须加上双引号指定字符串类型的查询条件。
内容的提问来源于stack exchange,提问作者violetflare
相关产品推荐
相关产品推荐

