This is a continuation:
Matching pattern for jsonb key / value
I have a table as follows
CREATE TABLE "PreStage".transaction ( transaction_id serial NOT NULL, transaction jsonb CONSTRAINT pk_transaction PRIMARY KEY (transaction_id) );
The content in the jsonb column of the transaction looks like
{"ADDR": "abcd", "CITY": "abcd", "PROV": "", "ADDR2": "", "ADDR3": "","CNSNT": "Research-NA", "CNTRY": "NL", "EMAIL": "@.com", "PHONE": "12345", "HCO_NM": "HELLO", "UNQ_ID": "", "PSTL_CD": "1234", "HCP_SR_NM": "", "HCP_FST_NM": "", "HCP_MID_NM": ""}
I need a search query, for example:
SELECT transaction AS data FROM "PreStage".transaction WHERE transaction->>'HCP_FST_NM' ILIKE '%neer%';
But I have to give the user the flexibility to find any key / value on the fly.
The answer to the previous question suggested creating an index as:
CREATE INDEX idxgin ON "PreStage".transaction USING gin ((transaction->>'HCP_FST_NM') gin_trgm_ops);
Which works, but I also wanted to index other keys. So he tried something like:
CREATE INDEX idxgin ON "PreStage".transaction USING gin ((transaction->>'HCP_FST_NM'),(transaction->>'HCP_LST_NM') gin_trgm_ops)
What does not work. What is the best approach to indexing here, or do I need to create a separate index for each key, in which case the approach will not be common if a new key / value pair is added to the data.
pattern-matching indexing postgresql jsonb
Neeraj gupta
source share