1

I'm migrating from sql server to postgres. IN sql server I have created the index like this:

 CREATE FULLTEXT INDEX ON [testdb].[dbo].['table'] (test1,test2...) KEY INDEX unique_index
              WITH STOPLIST = SYSTEM

How can I created the same thing in postgres, if I'm trying to create for multiple column like this it is giving error:

CREATE INDEX ON [testdb].[dbo].['table'] USING gin (to_tsvector('english', test1)||to_tsvector('english',test2)||to_tsvector('english', test3));

Error:

ERROR: function to_tsvector(unknown, character varying, character varying) does not exist

1 Answer 1

2

In Postgres, the to_tsvector function only accepts a single column as input. But this does not mean that we cannot do a full text search using multiple columns. For instance, assuming you wanted to query using test1 and test2, rank the results, and then retrieve the top 10 matches, you could use this query:

SELECT *,
    ts_rank(
        to_tsvector('english', test1) || to_tsvector('english', test2),
        to_tsquery('english', 'your search term here')
    ) AS rnk
FROM yourTable
WHERE
    to_tsvector('english', test1) || to_tsvector('english', test2) @@
    to_tsquery('english', 'your search term here')
ORDER BY
    rnk DESC
LIMIT 10;
Sign up to request clarification or add additional context in comments.

5 Comments

thank you, how we can create index for multiple columns.
@ashok You don't...you use a workaround, such as the one my answer suggests.
stackoverflow.com/questions/66746383/… in this question, using || symbol we can create index has been mentioned, is it possible?
sorry I have updated te question, please can you check.
@TimBiegeleisen why you have test1, test2, and "your search term here", I only have a single query I want to search for, not 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.