Does anyone know how to create an index for JSON data in PostgreSQL 9.2?
Sample data:
[ {"key" : "k1", "value" : "v1"}, {"key" : "k2", "value" : "v2"} ]
Tell me, do I want to index all the keys, how to do this?
Thanks.
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 full-text search. More details here: http://www.postgresql.org/docs/9.0/static/textsearch-indexes.html
There are currently no built-in functions for directly indexing JSON. But you can do this with a function-based index, where the function is written in JavaScript.
See this blog post for more details: http://people.planetpostgresql.org/andrew/index.php?/archives/249-Using-PLV8-to-index-JSON.html
There is another blog entry that 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
This question is a bit old, but I think the answer chosen is actually not perfect. To index json (property values inside json text), we can use expression indices with PLV8 (suggested by @a_horse_with_no_name).
Craig Kershtein does an excellent job explaining / demonstrating:
http://www.craigkerstiens.com/2013/05/29/postgres-indexes-expression-or-functional-indexes/
Source: https://habr.com/ru/post/926176/More articles:Regular Expression - regexWriting Real-Time Applications with NodeJS - javascriptPython notification - pythonC ++ 11 way to write a template to select a larger integer type? - c ++Bubble / Capture Event - Where Does It Start / End? - javascriptPython and Matplotlib and annotations with mouse pointer - pythonhow to access the value of the counter counter in the gearbox? - javaHow to work with Android devices that do not comply with EXIF orientation data? - androidWhy class object (root) does not encounter multiple inheritance - c #How to pass additional arguments - c #All Articles