postgresql 如何在 Postgres 9.3 中的 json 字段上创建索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17807030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:18:03  来源:igfitidea点击:

How to create index on json field in Postgres 9.3

postgresqlpostgresql-9.3

提问by rlib

In PostgreSQL 9.3 Beta 2 (?), how do I create an index on a JSON field? I tried it using the ->operator used for hstorebut got the following error:

在 PostgreSQL 9.3 Beta 2 (?) 中,如何在 JSON 字段上创建索引?我使用->用于的运算符进行hstore了尝试,但出现以下错误:

 CREATE TABLE publishers(id INT, info JSON);
 CREATE INDEX ON publishers((info->'name'));

ERROR: data type json has no default operator class for access method "btree" HINT: You must specify an operator class for the index or define a default operator class for the data type.

错误:数据类型 json 没有用于访问方法“btree”的默认运算符类 提示:您必须为索引指定运算符类或为数据类型定义默认运算符类。

回答by rlib

Found:

成立:

CREATE TABLE publishers(id INT, info JSON); 
CREATE INDEX ON publishers((info->>'name'));

As stated in the comments, the subtle difference here is ->>instead of ->. The former one returns the value as text, the latter as a JSON object.

正如评论中所述,这里的细微差别是->>而不是->。前者以文本形式返回值,后者以 JSON 对象形式返回。