Is there a simple method to remove the occurrence of lexeme in a tsvector?
I've got a tsvector column in a table and I would like to be able to remove a lexeme from the tsvector.
I've started playing around with 开发者_Go百科ts_stat to rebuild a new tsvector, but it seems quite complicated.
Is there a simple way to do it?
Cast it to text, remove with regexp, and cast back to tsvector.
As of Aug 2022 (the moment when this comment was written), you can use ts_delete()
, which has the below signatures:
ts_delete(vector tsvector, lexeme text) → tsvector
ts_delete(vector tsvector, lexemes text[]) → tsvector
Example case for a single lexeme:
ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat') → 'cat':3 'rat':5A
As seen in the signature, you can also pass an array:
ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']) → 'cat':3
An interesting use case is subtracting a tsvector
from another tsvector
, which can be done by combining ts_delete()
and tsvector_to_array()
:
ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, tsvector_to_array('cat fat'::tsvector)) → 'rat':5A
精彩评论