Given the following dataset:
-- Create random table
CREATE TABLE sample (
id serial primary key,
content jsonb
);
-- Insert sample rows
INSERT INTO sample (content)
VALUES
('{"text": ["Lorem ipsum dolor sit amet","consectetur adipiscing elit","sed do eiusmod tempor incididunt","ut labore et dolore magna aliqua"]}'),
('{"text": ["Ut enim ad minim veniam","quis nostrud exercitation ullamco laboris","nisi ut aliquip ex ea commodo consequat","Duis aute irure dolor in reprehenderit","voluptate velit esse cillum dolore"]}'),
('{"text": ["eu fugiat nulla pariatur","Excepteur sint occaecat cupidatat","non proident, sunt in culpa qui","officia deserunt mollit anim id est laborum"]}')
;
content
is a JSONB column that contains a key text
which is a Json array of texts.
I would like to be able to search with ILIKE
or a similar function within this text using an index.
Functionally this works and this is what I would like to do:
WITH ctr AS (
SELECT id, jsonb_array_elements_text((content->>'text')::jsonb) as mytext
FROM sample
)
SELECT id
FROM ctr
WHERE mytext ILIKE '%qui%';
id
----
2
2
3
(3 rows)
Is there a solution to have the same output using an index? I am looking to query a very large dataset.
It looks like it's not possible to use a trigram index (ERROR: operator class "gist_trgm_ops" does not accept data type jsonb
)