jlfenaux
jlfenaux

Reputation: 3291

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 ts_stat to rebuild a new tsvector, but it seems quite complicated.

Is there a simple way to do it?

Upvotes: 3

Views: 858

Answers (2)

Mihai Crisan
Mihai Crisan

Reputation: 33

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

Upvotes: 1

user80168
user80168

Reputation:

Cast it to text, remove with regexp, and cast back to tsvector.

Upvotes: 5

Related Questions