我正在尝试使用“新”JSONB 类型。
我有一个documents
带有properties
jsonb 字段的表,其中是一个 field publication_year
。我想查找一年范围内的所有文档记录,例如 2013-2015。 [编辑:查询一系列值是这里的主要挑战,即使我在下面使用了完全匹配的示例。所要求的方法也适用于美元范围(价格 > 20 美元和价格 < 40 美元)或时间戳范围)。]
我努力了:
create index test1 on documents using gin ((cast(properties->'announced_on_year' as integer)));
ERROR: cannot cast type jsonb to integer
也:
create index test1 on documents using gin (cast(properties->>'publication_year' as integer));
ERROR: data type integer has no default operator class for access method "gin"
HINT: You must specify an operator class for the index or define a default operator class for the data type.`
我从这篇文章http://www.postgresql.org/message-id/10736.1409063604@sss.pgh.pa.us看到这应该是可能的,但我无法弄清楚正确的语法。
当我只是做一个简单的索引时:
create index test1 on documents using gin ((properties->'publication_year'));
创建了一个索引,但我无法使用整数值查询它来获得一个范围,它说
select count(*) from documents where properties->>'publication_year' = 2015;
ERROR: operator does not exist: text = integer
LINE 1: ...*) from documents where properties->>'publication_year' = 2015;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
任何提示和提示都非常感谢。我相信其他人也会受益。TIA