You can do the seach using jsonb functions.
Lets say you have this:
item_id | body
--------+---------------------------------------------------------------
1 | [{"text":"aSdf aSdf"},{"text":"12 41f"},{"text":"1 31s sf"}]
2 | [{"text":"31fa3"},{"text":"3f43f"}]
3 | [{"text":"l8412"},{"text":"52%$3d1f"},{"text":"agasd as3"}]
4 | [{"text":"i8i23"}]
You can search for every element in your jsonb using:
SELECT * FROM (SELECT t.id,elem.*
FROM public.items t,jsonb_array_elements(t.body) AS elem)json_vals
WHERE value->>'text' ILIKE '%s%'
This will return the next result because the query uses ILIKE
:
item_id | value
--------+----------------------
1 | {"text":"aSdf aSdf"}
1 | {"text":"1 31s sf"}
3 | {"text":"agasd as3"}
If you just need the id, you can change the *
in the query for item_id
and use DISTINCT
. But with this query you can access every JSON in the record without having to cast it as text
.
NOTE: if you just query SELECT t.id,elem.* FROM public.items t,jsonb_array_elements(t.body) AS elem
you get every element in a row:
itme_id | value
--------+--------------------
1 | {"text":"aSdf aSdf"}
1 | {"text":"12 41f"}
1 | {"text":"1 31s sf"}
2 | {"text":"31fa3"}
2 | {"text":"3f43f"}
3 | {"text":"l8412"}
3 | {"text":"52%$3d1f"}
3 | {"text":"agasd as3"}
4 | {"text":"i8i23"}