Reputation: 2134
Does anyone know how to create index on JSON data in PostgreSQL 9.2?
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.
Upvotes: 6
Views: 2849
Reputation: 2591
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).
Craig Kerstein does a great job of explaining/demonstrating:
http://www.craigkerstiens.com/2013/05/29/postgres-indexes-expression-or-functional-indexes/
Upvotes: 0
Reputation: 2834
You are much better off using hstore for indexed fields, at least for now.
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. More info here: http://www.postgresql.org/docs/9.0/static/textsearch-indexes.html
Upvotes: 3
Reputation:
Currently there are no built-in functions to index JSON directly. But you can do it with a function based index where the function is written in JavaScript.
See this blog post for details: http://people.planetpostgresql.org/andrew/index.php?/archives/249-Using-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
Upvotes: 2