Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By Sarad in Tutorial
Introduction:
You might wonder what’s the big deal with Full-Text Search, you can use LIKE or
ILIKE clause any day in a query to perform a search on any given string. The
problem with traditional LIKE clause is that it has a very high-performance
penalty. That means, it takes a long time to return results as the words inside the
text are not indexed. Also, the results won’t be as expected, for e.g. you might
search rectify in a document which contains recti es – while ideally the
document should show up in the search result but it doesn’t.
Prerequisites:
https://hevodata.com/blog/postgresql-full-text-search-setup/ 1/6
10/17/2019 PostgreSQL- Setting up Full Text Search | Hevo Blog
Before getting started you need to have a working copy of PostgreSQL installed
on your favorite OS.
We will now have to create a document, which is simply just concatenating all the
elds we want to perform a search on.
SELECT
(title || ' ' || description || ' ' || creator) AS
FROM tv_series;
SELECT
(to_tsvector(title) || to_tsvector(description) ||
FROM tv_series;
https://hevodata.com/blog/postgresql-full-text-search-setup/ 2/6
10/17/2019 PostgreSQL- Setting up Full Text Search | Hevo Blog
The result we have got in return is in tsvector form, this is more suitable to
perform a full-text search. But if you notice, the result looks weirder than
anything we originally had. If you’d notice, you will nd that rst record in the
result has, in fact, reduced the word investigation to investig, also there are some
random numbers in front of each word in the result. What really happened is
that Postgres just sorted a list of distinct lexemes, which are words that have
been normalized to merge di erent variants of the same word.
Let’s start querying the data. We will be using @@ operator to perform a query
and check if a tsvector matches tsquery
SELECT to_tsvector(description) @@ 'morty' FROM tv_ser
?column?
----------
f
(1 row)
Sadly, the result is negative. To solve this we need to build a tsquery which
creates the same lexemes and using the operator @@, casts the string into a
tsquery. The following shows the di erence between casting and using the
function to_tsquery().
https://hevodata.com/blog/postgresql-full-text-search-setup/ 3/6
10/17/2019 PostgreSQL- Setting up Full Text Search | Hevo Blog
'rick' | 'rick'
(1 row)
UPDATE
tv_series
SET
document_vectors = (to_tsvector(title) || to_tsvec
For more fun we will insert a new row in the column this time with tsvector of
document.
https://hevodata.com/blog/postgresql-full-text-search-setup/ 4/6
10/17/2019 PostgreSQL- Setting up Full Text Search | Hevo Blog
VALUES
(5, 'Better Call Saul', 'Better Call Saul is an Am
We were able to successfully query all the documents which had the word
Breaking Bad in it with just the word ‘break’.
Database PostgreSQL
https://hevodata.com/blog/postgresql-full-text-search-setup/ 5/6
10/17/2019 PostgreSQL- Setting up Full Text Search | Hevo Blog
Pricing Team
https://hevodata.com/blog/postgresql-full-text-search-setup/ 6/6