简体   繁体   English

在postgresql中搜索JSON列的查询

[英]Search query on JSON column in postgresql

I have a column 'tags' in my table 'categories', which is a json type 我的表'类别'中有一个列'标签',这是一个json类型

----------------------------------------------------------------
id    name                tags
----------------------------------------------------------------
1     Electronics         ["phones","computers","watches"]
2     Fashion             ["jewelry","costumes","watches"]
3     Food                ["cakes","sweets","juices"]
4     Tools               ["keyboards","pens","drills"]
----------------------------------------------------------------

I want to fetch all categories that are having any one of the tags in the array that I match in my SQL 我想获取在我的SQL中匹配的数组中包含任何一个标记的所有类别

select * from categories where tags::jsonb ?| '{"watches","sweets"}'

I expect the result set as 我希望结果集为

------------------------------
1     Electronics 
2     Fashion
3     Food  
------------------------------

so first two categories matches for "watches" and third category for "sweets" 所以前两个类别匹配“手表”,第三类匹配“糖果”

the above query perfectly worked on my local server as thats on 9.4 but when I moved to live itis 9.2.15 there and having no jsonb, returning error, is there a similar operator for json type that returns any one of the matching elements in query? 上面的查询完全在我的本地服务器上工作,就像在9.4上那样但是当我移动到现场itis 9.2.15并且没有jsonb,返回错误时,json类型是否有类似的运算符返回查询中的任何一个匹配元素?

is the column categories.tags json or text datatype? categories.tags json或text数据类型? JSON datatype support is very limited in PostgreSQL 9.2, so although you may store JSON data in it, you'll have trouble processing it in any way other than casting to text. PostgreSQL 9.2中的JSON数据类型支持非常有限,因此尽管您可以在其中存储JSON数据,但除了转换为文本之外,您将无法以任何方式处理它。

You could use the array overlap && , instead of the jsonb ?| 您可以使用数组重叠&& ,而不是jsonb ?| operator: 运营商:

with
  categories(id,name,tags) as (
    values (1, 'Electronics', '["phones","computers","watches"]'::json),
           (2, 'Fashion', '["jewelry","costumes","watches"]'),
           (3, 'Food', '["cakes","sweets","juices"]'),
           (4, 'Tools', '["keyboards","pens","drills"]'))

select *
from categories
where regexp_replace(regexp_replace(tags::text, '^\[', '{'), '\]$', '}')::text[] && '{watches,sweets}'::text[]

I just found a simple solution for this problem 我刚刚找到了解决这个问题的简单方法

I turned the json type to text first then replaced my search expression with SIMILAR TO 我首先将json类型转换为文本,然后用SIMILAR TO替换了我的搜索表达式

here is the example sql 这是sql的示例

select * from categories where tags SIMILAR TO  ("watches"|"sweets")

" are must because they help in fetching apt matches, like when you have tags like watches , watches-new using SIMILAR to with out " like (watches|sweets) will result both, if you use with "" on a json stored on text column, it works just as you wanted it to. “是必须的,因为它们有助于获取匹配,就像你有像watches这样的标签, watches-new使用SIMILAR而不是”喜欢(watches|sweets)会产生两者,如果你在文本上存储的json上使用“”专栏,它可以按照您的意愿工作。

hope this helps some one like me. 希望这有助于像我这样的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM