Using indexes in json array in PostgreSQL

user2512324

Referring to the original stackoverflow question, I am trying to apply gin indexes to keys in objects of an array in Postgres 9.4 but I'm not getting the results as stated in the first answer.

Can you please rectify the error?

The steps I followed have been written below.

Part 1: Creating table and indexes

CREATE TABLE tracks (id serial, artists jsonb);
CREATE INDEX tracks_artists_gin_idx ON tracks USING gin (artists);
INSERT INTO tracks (id, artists) VALUES (1, '[{"name": "blink-182"}]');
INSERT INTO tracks (id, artists) VALUES (2, '[{"name": "The Dirty Heads"}, {"name": "Louis Richards"}]');

Part 2: Query

SELECT * FROM tracks WHERE artists @> '{"name": "The Dirty Heads"}';
 id | artists 
----+---------
(0 rows)

This query gives empty results.
I also tried to use jsonb_path_ops GIN indexes.

Alternative index and query:

DROP INDEX tracks_artists_gin_idx;
CREATE INDEX tracks_artistnames_gin_idx ON tracks USING  gin (artists jsonb_path_ops);
SELECT * FROM tracks WHERE artists @> '{"name": "The Dirty Heads"}';
 id | artists 
----+---------
(0 rows)
potatosalad

This specific jsonb example from the original answer was missing the array layer [] around the non-primitive object for the containment query. It has since been fixed.

The behavior documented for PostgreSQL 9.4.x jsonb Containment and Existence states:

The general principle is that the contained object must match the containing object as to structure and data contents

...

As a special exception to the general principle that the structures must match, an array may contain a primitive value

The special exception allows us to do the following:

CREATE TABLE tracks (id serial, artistnames jsonb);
CREATE INDEX tracks_artistnames_gin_idx ON tracks USING gin (artistnames);
INSERT INTO tracks (id, artists) VALUES (1, '["blink-182"]');
INSERT INTO tracks (id, artists) VALUES (2, '["The Dirty Heads", "Louis Richards"]');

We can query for containment using the general principle:

SELECT * FROM tracks WHERE artistnames @> '["The Dirty Heads"]';
 id |              artistnames              
----+---------------------------------------
  2 | ["The Dirty Heads", "Louis Richards"]
(1 row)

We can also query for containment using the special exception since the array contains primitive types:

SELECT * FROM tracks WHERE artistnames @> '"The Dirty Heads"';
 id |              artistnames              
----+---------------------------------------
  2 | ["The Dirty Heads", "Louis Richards"]
(1 row)

There are 4 primitive types that allow containment and existence queries on arrays to work:

  1. string
  2. number
  3. boolean
  4. null

Since the example you mentioned in your question is dealing with objects nested inside an array, we don't qualify for the special exception mentioned above:

CREATE TABLE tracks (id serial, artists jsonb);
CREATE INDEX tracks_artists_gin_idx ON tracks USING gin (artists);
INSERT INTO tracks (id, artists) VALUES (1, '[{"name": "blink-182"}]');
INSERT INTO tracks (id, artists) VALUES (2, '[{"name": "The Dirty Heads"}, {"name": "Louis Richards"}]');

We can query for containment using the general principle:

SELECT * FROM tracks WHERE artists @> '[{"name": "The Dirty Heads"}]';
 id |                          artists                          
----+-----------------------------------------------------------
  2 | [{"name": "The Dirty Heads"}, {"name": "Louis Richards"}]
(1 row)

Objects are not considered a primitive type, so the following query for containment does not qualify for the special exception and therefore does not work:

SELECT * FROM tracks WHERE artists @> '{"name": "The Dirty Heads"}';
 id | artists 
----+---------
(0 rows)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using indexes in json array in PostgreSQL

From Dev

How to create indexes using Hibernate for PostgreSQL

From Dev

Alphabetical sort in postgresql using indexes.

From Dev

Encode array to JSON string without array indexes

From Dev

How to transform string with objects and array indexes into json

From Dev

Returning array indexes in a Collection while using Stream

From Dev

Sort rows of a cell array using given indexes

From Dev

Get a sub array using array of indexes in one step

From Dev

how to sort (indexes) of an array to get the original array sorted from the smallest to the biggest value by using those indexes

From Dev

Understanding bitmap indexes in postgresql

From Dev

Optimizing PostgreSQL with functional indexes?

From Dev

PHP unset returns my json array with int indexes

From Dev

How to convert array to json string in javascript having indexes inside the element

From Dev

Postgresql from Json Object to array

From Dev

Existance of key in JSON array in PostgreSQL

From Dev

Convert array of records to JSON in Postgresql

From Dev

Parameterized query in Postgresql with a json array

From Dev

how to fetch json array in postgresql

From Dev

Storing JSON Array of Arrays in PostgreSql

From Dev

Query with JSON array in WHERE in Postgresql

From Dev

char array assignment using two indexes added together

From Dev

How do I deserialize an array with indexes using jackson

From Dev

Using indexOf method on array, getting all indexes and not just first

From Dev

How do I deserialize an array with indexes using jackson

From Dev

Accessing n-dimensional array in R using a function of vector of indexes

From Dev

Using push() to add values to indexes within an empty multidimensional Array

From Dev

Perform operation on elements of numpy array using indexes list

From Dev

Querying array using list in postgreSQL

From Dev

How to get JavaScript/JSON array of array in Postgresql?

Related Related

  1. 1

    Using indexes in json array in PostgreSQL

  2. 2

    How to create indexes using Hibernate for PostgreSQL

  3. 3

    Alphabetical sort in postgresql using indexes.

  4. 4

    Encode array to JSON string without array indexes

  5. 5

    How to transform string with objects and array indexes into json

  6. 6

    Returning array indexes in a Collection while using Stream

  7. 7

    Sort rows of a cell array using given indexes

  8. 8

    Get a sub array using array of indexes in one step

  9. 9

    how to sort (indexes) of an array to get the original array sorted from the smallest to the biggest value by using those indexes

  10. 10

    Understanding bitmap indexes in postgresql

  11. 11

    Optimizing PostgreSQL with functional indexes?

  12. 12

    PHP unset returns my json array with int indexes

  13. 13

    How to convert array to json string in javascript having indexes inside the element

  14. 14

    Postgresql from Json Object to array

  15. 15

    Existance of key in JSON array in PostgreSQL

  16. 16

    Convert array of records to JSON in Postgresql

  17. 17

    Parameterized query in Postgresql with a json array

  18. 18

    how to fetch json array in postgresql

  19. 19

    Storing JSON Array of Arrays in PostgreSql

  20. 20

    Query with JSON array in WHERE in Postgresql

  21. 21

    char array assignment using two indexes added together

  22. 22

    How do I deserialize an array with indexes using jackson

  23. 23

    Using indexOf method on array, getting all indexes and not just first

  24. 24

    How do I deserialize an array with indexes using jackson

  25. 25

    Accessing n-dimensional array in R using a function of vector of indexes

  26. 26

    Using push() to add values to indexes within an empty multidimensional Array

  27. 27

    Perform operation on elements of numpy array using indexes list

  28. 28

    Querying array using list in postgreSQL

  29. 29

    How to get JavaScript/JSON array of array in Postgresql?

HotTag

Archive