如何在 Postgres 的 JSON 字段上创建索引? [英] How to create index on JSON field in Postgres?
问题描述
在 PostgreSQL 9.3 Beta 2 (?) 中,如何在 JSON 字段上创建索引?我使用用于 hstore
的 ->
运算符进行了尝试,但出现以下错误:
CREATE TABLE 发布者(id INT, info JSON);创建出版商索引((info->'name'));
<块引用>
错误:数据类型 json 没有用于访问方法的默认运算符类"btree" 提示:您必须为索引指定操作符类或为数据类型定义一个默认的操作符类.
找到:
CREATE TABLE 发布者(id INT, info JSON);CREATE INDEX ON Publishers((info->>'name'));
正如评论中所述,这里的细微差别是 ->>
而不是 ->
.前者以文本形式返回值,后者以 JSON 对象形式返回.
In PostgreSQL 9.3 Beta 2 (?), how do I create an index on a JSON field? I tried it using the ->
operator used for hstore
but got the following error:
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.
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.
这篇关于如何在 Postgres 的 JSON 字段上创建索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!