You should use a query fragment to wrap the JSON access:
# Assuming that the variable input = ["tag1"]
select o.id::text, o.name, o.geom, o.tags, o.address, o.open
from organizations o, jsonb_array_elements(tags) as t
and fragment("(?->>?)", t, "text") in ^input
I have used fragments in the past but as you can see from organizations o, jsonb_array_elements(tags) as t
How can you make this part of Ecto.Query, I can do from o in Organization but how should I add the json_array_elements fucntion from postgresql?
A fragment lets you execute a SQL fragment directly into the database, maybe you could try wrapping the jsonb_array_elements function call into another fragment.
select o.id::text, o.name, o.geom, o.tags, o.address, o.open
from organizations o, fragment("jsonb_array_elements(?)", tags) as t
and fragment("(?->>?)", t, "text") in ^input
Iâm not sure if that would work, but the docs show similar examples using other functions like lower.
It doesnât work if I use in ($1) but If I change the query to
select o.id::text, o.name, o.geom, o.tags, o.address, o.open
from organizations o, jsonb_array_elements(tags) as t
where t->>'text' = any($1)
will work I would like to know if there is a way to make work in the future and would be really nice to have instead of numbers named params like ..where t->>'text' = any($tags)", [tags: ["tag1"]], thanks for the help.