This question is related to PostgreSQL Reverse LIKE
I have a table a
containing a json column a.matchers
. a.matchers
is an array of objects each of which has property pattern
Given an inputString
I would like to get all rows where at least one of the matchers[].pattern
's would match inputString LIKE '%{pattern}%'
So basically (as pseudoquery):
SELECT *
FROM a
WHERE
'inputStringABC' LIKE ANY('%' || json_array_elements(a.matchers)->>'pattern' || '%')
Is this possible?
p.s.: PostgreSQL version being 9.6.3
edit: more extensive status quo description:
Table a:
Column | Type | Modifiers | Storage | Stats target | Description
------------------------------+-----------------------------+-----------------------------------------------+----------+--------------+-------------
uuid | uuid | not null default uuid_generate_v4() | plain | |
matchers | json | | extended | |
JSON structure in column matchers:
[
{
pattern: string;
}
]
Example use case:
I have a string "Some trees look nicer than others" and following rows in table a
:
uuid: "123...", matchers: [{ pattern: "trees" }]
uuid: "987...", matchers: [{ pattern: "bees" }, { pattern: "plants" }]
The query should return row with uuid "123..."
as trees
occurs in "Some trees look nicer than others"
Edit2:
final query which worked for me thanks to s-man
SELECT DISTINCT
uuid,
pattern
FROM (
SELECT
*,
jsonb_array_elements(a.matchers)->>'pattern' as pattern
FROM a
) s
WHERE
'Some trees look nicer than others' LIKE '%' || pattern || '%'