我可以使用 ilike 来检查 jsonb 中的 table 的内容吗?

Can I use ilike to check the contents of a table in jsonb

SELECT 
    id,
    some_jsonb_table
FROM
    public.example
where some_jsonb_table::text ilike '%example_report%'

我尝试在 TEXT 上切换 some_jsonb_table,但 ILIKE 仍然不起作用。 如何检查 table 的内容是否包含我要搜索的文本片段?

jsonb_data::text ilike '%my_search_string% 应该可以正常工作

使用这个示例table 并查询,它甚至适用于嵌套的 jsonb 对象

with a as (
select cast('{"brand": "Toyota", "color": ["red", "black"], "price": 285000}' as jsonb) jsonb_data
union  
select cast('{"brand": "Honda", "color": ["blue", "pink"], "price": 25000}' as jsonb)
)
select * from a 
where jsonb_data::text ilike '%blue%';

结果符合预期

jsonb_data
{"brand": "Honda", "color": ["blue", "pink"], "price": 25000}

您还可以使用替代运算符

with a as (
select cast('{"brand": "Toyota", "color": ["red", "black"], "price": 285000, "sold": true}' as jsonb) jsonb_data
union  
select cast('{"brand": "Honda", "color": {"good":[["blue"], "pink"]}, "price": 25000, "sold": false}' as jsonb)
)
select * from a 
where jsonb_data ->> 'color' ~* 'blue';

Good resource on jsonb in PostreSQL