简体   繁体   English

如何在PostgreSQL 9.2中索引JSON数据?

[英]How to index JSON data in PostgreSQL 9.2?

Does anyone know how to create index on JSON data in PostgreSQL 9.2? 有谁知道如何在PostgreSQL 9.2中创建JSON数据索引?

Example data: 示例数据:

[
  {"key" : "k1", "value" : "v1"},
  {"key" : "k2", "value" : "v2"}
]

Say if I want to index on all the keys how to do that? 如果我想在所有键上编制索引怎么做?

Thanks. 谢谢。

You are much better off using hstore for indexed fields, at least for now. 你最好不要将hstore用于索引字段,至少目前是这样。

CREATE INDEX table_name_gin_data ON table_name USING GIN(data);

You can also create GIST indexes if you are interested in fulltext search. 如果您对全文搜索感兴趣,也可以创建GIST索引。 More info here: http://www.postgresql.org/docs/9.0/static/textsearch-indexes.html 更多信息: http//www.postgresql.org/docs/9.0/static/textsearch-indexes.html

Currently there are no built-in functions to index JSON directly. 目前没有直接索引JSON的内置函数。 But you can do it with a function based index where the function is written in JavaScript. 但是你可以使用基于函数的索引来实现,其中函数是用JavaScript编写的。

See this blog post for details: http://people.planetpostgresql.org/andrew/index.php?/archives/249-Using-PLV8-to-index-JSON.html 有关详细信息,请参阅此博客文章: http//people.planetpostgresql.org/andrew/index.php?/archives / 249-Use-PLV8-to-index-JSON.html

There is another blog post from which talks about JSON and how it can be used with JavaScript: http://www.postgresonline.com/journal/archives/272-Using-PLV8-to-build-JSON-selectors.html 还有另一篇博文来讨论JSON及其如何与JavaScript一起使用: http//www.postgresonline.com/journal/archives/272-Using-PLV8-to-build-JSON-selectors.html

This question is a little old but I think the selected answer is not really the ideal one. 这个问题有点旧,但我认为选择的答案并不是理想的答案。 To index json (the property values inside json text), we can use expression indexes with PLV8 (suggested by @a_horse_with_no_name). 要索引json(json文本中的属性值),我们可以使用带有PLV8的表达式索引(由@a_horse_with_no_name建议)。

Craig Kerstein does a great job of explaining/demonstrating: Craig Kerstein在解释/演示方面表现出色:

http://www.craigkerstiens.com/2013/05/29/postgres-indexes-expression-or-functional-indexes/ http://www.craigkerstiens.com/2013/05/29/postgres-indexes-expression-or-functional-indexes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM