Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: Text Search, combining multiple JSONB tsvectors

In Postgres 10 you can full-text search JSONB data structures:

SELECT to_tsvector('english', '{"key":"value1"}'::jsonb);
>>> [tsvector]
>>> 'value1':1

Notice how tsvector only indexed the values of the JSON object (smart)

Now I want to combine the tsvector of multiple JSONB fields... I can do...

SELECT to_tsvector('{"key":"value1"}'::JSONB::TEXT || '{"key2":"value2"}'::JSONB::TEXT );
>>> [tsvector]
>>> 'key':1 'key2':3 'value1':2 'value2':4

Notice how because I've casted to TEXT I'm getting the keys and the values....

I want to combine the output of multiple tsvector() objects - how can I do this?

like image 210
Ben DeMott Avatar asked Sep 16 '26 06:09

Ben DeMott


1 Answers

Note that jsonb values can be concatenated:

SELECT '{"key":"value1"}'::jsonb || '{"key2":"value2"}'::jsonb AS new_jsonb

              new_jsonb              
-------------------------------------
 {"key": "value1", "key2": "value2"}
(1 row) 

You do not need to cast them to text:

SELECT to_tsvector('{"key":"value1"}'::jsonb || '{"key2":"value2"}'::jsonb );

      to_tsvector      
-----------------------
 'value1':1 'value2':3
(1 row)
like image 55
klin Avatar answered Sep 18 '26 06:09

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!