Search code examples
postgresqlfull-text-searchrankingtsvector

Why in PostgreSQL setweight doesn't set weight for array_to_tsvector?


In my table I need to create a tsvector out of an array of text and then setweight it accordingly. But it doesn't seem to work:

select
    setweight(to_tsvector('simple', 'test vector of multiple values'), 'A') as "simple",
    setweight(to_tsvector('simple', array_to_string(array['test', 'vector', 'of', 'multiple', 'values'], ' ')), 'A') as "string array",
    setweight(array_to_tsvector('{test, vector, of, multiple, values}'::text[]), 'A') as "text array",
    setweight(array_to_tsvector(array['test', 'vector', 'of', 'multiple', 'values']), 'A') as "array"

This gives me:

simple string array text array array
'multiple':4A 'of':3A 'test':1A 'values':5A 'vector':2A 'multiple':4A 'of':3A 'test':1A 'values':5A 'vector':2A 'multiple' 'of' 'test' 'values' 'vector' 'multiple' 'of' 'test' 'values' 'vector'

Notice the absence of weights in last 2 columns. Tried to google something about it but the only thing I found was this topic without meaningful comments.

Again, I don't need to index or search an array and array_to_string workaround seems a bit ugly to me too (just like the author of the mentioned topic).

What am I missing about array_to_tsvector? Or setweight perhaps?


Solution

  • From the docs:

    Note that weight labels apply to positions, not lexemes. If the input vector has been stripped of positions then setweight does nothing.

    I don't know what the best work-around is, as I don't know what you are doing. Setting weights is pointless if you don't do anything with them.