LIKE query on elements of flat jsonb array

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:29:24

问题


I have a Postgres table posts with a column of type jsonb which is basically a flat array of tags.

What i need to do is to somehow run a LIKE query on that tags column elements so that i can find a posts which has a tags beginning with some partial string.

Is such thing possible in Postgres? I'm constantly finding super complex examples and no one is ever describing such basic and simple scenario.

My current code works fine for checking if there are posts having specific tags:

select * from posts where tags @> '"TAG"'

and I'm looking for a way of running something among the lines of

select * from posts where tags @> '"%TAG%"'

回答1:


SELECT *
FROM   posts p
WHERE  EXISTS (
   SELECT FROM jsonb_array_elements_text(p.tags) tag
   WHERE  tag LIKE '%TAG%'
   );

Related, with explanation:

  • Search a JSON array for an object containing a value matching a pattern

This cannot use any indexes. So slow for big tables. A normalized design, like Laurenz already suggested would be superior - with a trigram index:

  • PostgreSQL LIKE query performance variations

For just prefix matching (LIKE 'TAG%', no leading wildcard), you could make it work with a full text index:

CREATE INDEX posts_tags_fts_gin_idx ON posts USING GIN (to_tsvector('simple', tags));

And a matching query:

SELECT *
FROM   posts p
WHERE  to_tsvector('simple', tags)  @@ 'TAG:*'::tsquery

Or use the english dictionary instead of simple (or whatever fits your case) if you want stemming for natural English language.

to_tsvector(json(b)) requires Postgres 10 or later.

Related:

  • Get partial match from GIN indexed TSVECTOR column
  • Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL


来源:https://stackoverflow.com/questions/50101530/like-query-on-elements-of-flat-jsonb-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!