0

I have added a table column document of tsvector type:

ALTER TABLE Schema.Table ADD COLUMN document tsvector; update Schema.Table SET document = to_tsvector(Table::text); 

LIKE does not work with tsvector type columns to search for a partial match. I would like to find 'Missing', 'Miss', 'Missplaced' etc. with a query like this (pseudo code, not working):

SELECT * FROM Schema.Table WHERE document ILIKE 'Miss%'; 

How can I search for a partial word in tsvector type column?

1

1 Answer 1

1

Full text search allows prefix matching - with index support:

SELECT * FROM schema.table WHERE document @@ to_tsquery('simple', 'Miss:*'); 

Note :* appended to the (leading!) search word.
See:

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, @Erwin, Your help was most useful for me. I came across one issue where to_tsvector("POC1_Table"::text) does not work with NULL values. (ts_vector is not created is any NULL is there in that row). Then, I used document = to_tsvector(coalesce(CAST (COL1 as text),'') || ' ' || coalesce( COL2 ,'')). Here COL1 is integer so need coalesce CAST and where COL2 is text. This will work even any value is NULL. Do you know any better way to convert integer, bigint, binary values in ts_vector?
@Mehul: Just concat_ws(' ', col1, col2). You don't even need an explicit cast for this as concat_ws() coerces automatically. See: stackoverflow.com/a/66556331/939860 and stackoverflow.com/a/12320369/939860

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.