Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

Index for finding an element in a JSON Array in PostgreSQL (with multiple JSON fields)

$
0
0

Original question of what this is based on: http://stackoverflow.com/questions/18404055/index-for-finding-an-element-in-a-json-array

This works fine if you only want simple matches. Suppose tracks have both Artist and Title field in the JSON data. So we have something like

INSERT INTO tracks (id, data)  VALUES (1, '[{"artist": "Simple Plan", "title": "Welcome to My Life"}]');

We create the index like this (similar to the original question)

CREATE INDEX tracks_artists_gin_idx ON tracks
USING GIN (json_val_arr(data, 'artist'));

CREATE INDEX tracks_title_gin_idx ON tracks
USING GIN (json_val_arr(data, 'title'));

So now we have two fields to match. As you can see, if we perform the original query (with very naive modifications) of:

SELECT *
FROM   tracks
WHERE  '{"ARTIST NAME"}'::text[] <@ (json_val_arr(data, 'artist'))
AND '{"TITLE"}'::text[] <@ (json_val_arr(data, 'title'))

This will give the wrong answer because the indices of the array of artist and title in the JSON array do not have to match for this query to match something in the JSON. What is the proper way of doing this query so we can get the exact match we need? Does json_val_arr need to be changed?

Edit: Why this is wrong

Suppose our table has records like

INSERT INTO tracks (id, data)  VALUES (1, '[{"artist": "Simple Plan", "title": "Welcome to My Life"}]');
INSERT INTO tracks (id, data)  VALUES (2, '[{"artist": "Another Artist", "title": "Welcome to My Life"}, {"artist": "Simple Plan", "title": "Perfect"}]');

If you query like

SELECT *
FROM   tracks
WHERE  '{"Simple Plan"}'::text[] <@ (json_val_arr(data, 'artist'))
AND '{"Welcome to my Life"}'::text[] <@ (json_val_arr(data, 'title'))

Both records will be matched (both record 1 and 2), even though you really only wanted the first record


Viewing all articles
Browse latest Browse all 1138

Trending Articles