Home > Mobile >  followed-by operator not possible with tsvector constructed from jsonb due to gaps in word numbering
followed-by operator not possible with tsvector constructed from jsonb due to gaps in word numbering

Time:01-21

Input:

select to_tsvector('simple', '["one","two","three"]'::jsonb)

returns

'one':1 'three':5 'two':3

Expected:

'one':1 'three':3 'two':2

Which words have index 2 and 4? Is this a bug? How do I use it in a valid way?

With those gaps in the index numbering any search using a followed-by operator (ex: 'one' <-> 'zwo') is doomed to fail.

How I can repair that using immutable existing functions? The jsonb originates from a call to jsonb_path_query_array() which could be changed to jsonb_path_query() but I have no clue how to consolidate a set of jsonb values into a single tsvector.

CodePudding user response:

I'd say that a phrase query across multiple JSON array elements is, er, weird.

You could work around that by casting the JSON to text:

select to_tsvector('simple', '["one","two","three"]'::jsonb::text);

        to_tsvector        
═══════════════════════════
 'one':1 'three':3 'two':2
(1 row)

That type cast is IMMUTABLE.

  • Related