Based on this related question on SO.
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 in the original question:
CREATE INDEX tracks_artists_gin_idx ON tracks
USING GIN (json2arr(data, 'artist'));
CREATE INDEX tracks_title_gin_idx ON tracks
USING GIN (json2arr(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[] <@ (json2arr(data, 'artist'))
AND '{"TITLE"}'::text[] <@ (json2arr(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 json2arr()
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"}]')
, (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[] <@ (json2arr(data, 'artist'))
AND '{"Welcome to my Life"}'::text[] <@ (json2arr(data, 'title'))
Both records will be matched (both record 1 and 2), even though you really only wanted the first record.