Обсуждение: How to search a string inside a json structure

Поиск
Список
Период
Сортировка

How to search a string inside a json structure

От
Sami Pietilä
Дата:
Hi,

I have a database with jsonb type of columns. Colums contain complex json structures. I would like to get all rows which contain a json where any of json's values matches to a given string (like %hello%).

How to create a postgre sql query to do this?

I guess postgre should traverse though each json structures while finding the string.

Thanks

Re: How to search a string inside a json structure

От
"Jason O'Donnell"
Дата:
Sami,

What version of postgres are you using?

There's some examples using GIN indexes for searching jsonb objects in the wiki: https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.4#JSONB_Binary_JSON_storage

Hope that helps,

On Mon, Nov 2, 2015 at 4:09 PM, Sami Pietilä <sami.pietila@gmail.com> wrote:
Hi,

I have a database with jsonb type of columns. Colums contain complex json structures. I would like to get all rows which contain a json where any of json's values matches to a given string (like %hello%).

How to create a postgre sql query to do this?

I guess postgre should traverse though each json structures while finding the string.

Thanks



--
Jason O'Donnell
Crunchy Data Solutions

Re: How to search a string inside a json structure

От
Sami Pietilä
Дата:
Hi,

Thank you for reply!

I am using version 9.4.5.

Unfortunately I could not figure out how to select rows which, for example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by search with "hello" string.

I am trying to create a query which looks values in any field in JSON, "a" and "b" in this case (without knowing the names "a" and "b").

Thanks

2015-11-03 0:56 GMT+02:00 Jason O'Donnell <odonnelljp01@gmail.com>:
Sami,

What version of postgres are you using?

There's some examples using GIN indexes for searching jsonb objects in the wiki: https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.4#JSONB_Binary_JSON_storage

Hope that helps,

Re: How to search a string inside a json structure

От
Vick Khera
Дата:

On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com> wrote:
Unfortunately I could not figure out how to select rows which, for example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by search with "hello" string.

cast the field to a text:

select * from t where myfield::text like '%hello%';

Re: How to search a string inside a json structure

От
Merlin Moncure
Дата:
On Tue, Nov 3, 2015 at 9:57 AM, Vick Khera <vivek@khera.org> wrote:
>
> On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com>
> wrote:
>>
>> Unfortunately I could not figure out how to select rows which, for
>> example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by
>> search with "hello" string.
>
> cast the field to a text:
>
> select * from t where myfield::text like '%hello%';

Performance of this will be awful.  Unfortunately, FWICT there is no
way to do partial string matches against json in a indexed way without
some serious elbow grease.  For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module.  It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin


Re: How to search a string inside a json structure

От
Sami Pietilä
Дата:
Hi,

Thank you for reply.

I think I need to do some more research of means to implement searching for json databases.

I'll look the module.

Thanks

2015-11-03 18:43 GMT+02:00 Merlin Moncure <mmoncure@gmail.com>:
On Tue, Nov 3, 2015 at 9:57 AM, Vick Khera <vivek@khera.org> wrote:
>
> On Tue, Nov 3, 2015 at 10:07 AM, Sami Pietilä <sami.pietila@gmail.com>
> wrote:
>>
>> Unfortunately I could not figure out how to select rows which, for
>> example, contain following json: '{"a":"world","c":{"b":"helloworld"}}' by
>> search with "hello" string.
>
> cast the field to a text:
>
> select * from t where myfield::text like '%hello%';

Performance of this will be awful.  Unfortunately, FWICT there is no
way to do partial string matches against json in a indexed way without
some serious elbow grease.  For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module.  It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin

Re: How to search a string inside a json structure

От
Sami Pietilä
Дата:
Hi,

I have simplified the case and tried to look from a list of json items (please see test table content). I think I have managed to unpack values in such a way that also partial matching is possible. However, the end result has two "value" named columns even if I have tried to rename them.

How can I rename the columns coming from jsonb_ functions? Or is there a better way of unpacking the list and items?

select * from test;
 id |           main          
----+--------------------------
  1 | [{"A": "b"}, {"B": "b"}]
(1 row)

select * from test, jsonb_array_elements(test.main) as m, jsonb_each(value);
 id |           main           |   value    | key | value
----+--------------------------+------------+-----+-------
  1 | [{"A": "b"}, {"B": "b"}] | {"A": "b"} | A   | "b"
  1 | [{"A": "b"}, {"B": "b"}] | {"B": "b"} | B   | "b"
(2 rows)

Thanks

Performance of this will be awful.  Unfortunately, FWICT there is no
way to do partial string matches against json in a indexed way without
some serious elbow grease.  For full key-value matching though you're
good to go.

Do index this operation, the best option today will revolve around the
pg_trgm module.  It can optimize like expressions against text.
Performance of this is a mixed bag although upcoming pg_trgm
enhancements should make it a lot faster.

merlin

Re: How to search a string inside a json structure

От
"David G. Johnston"
Дата:
On Saturday, November 7, 2015, Sami Pietilä <sami.pietila@gmail.com> wrote:
How can I rename the columns coming from jsonb_ functions? Or is there a better way of unpacking the list and items?

select * from function(...) [as] func-name_alias (col1_alias, col2_alias, etc...)

David J.