LogoDuyệtSr. Data Engineer
HomeAboutPhotosInsightsCV

Footer

Logo

Resources

  • Rust Tiếng Việt
  • /archives
  • /series
  • /tags
  • Status

me@duyet.net

  • About
  • LinkedIn
  • Resume
  • Projects

© 2026 duyet.net | Sr. Data Engineer | 2026-02-27

Postgres Full Text Search

Note: This post is over 5 years old. The information may be outdated.

Postgres has built-in functions to handle Full Text Search queries. This is like a "search engine" within Postgres.

to_tsvector()

Converts your data into searchable "tokens". to_tsvector() stands for "to text search vector". For example:

select to_tsvector("the green eggs and ham")
-- Returns 'egg':2 'green':1 'ham':4


select to_tsvector("the green egg and ham")
-- Returns 'egg':2 'green':1 'ham':4

Collectively these tokens are called a document which Postgres can use for comparisons, where every token is a lexeme (unit of lexical meaning). The stopwords (and, or, the, ...) are conveniently omitted. As you can see, the eggs will be normalized as a lexeme in English: egg.

to_tsquery()

to_tsquery(), which accepts a list of words that will be checked against the normalized vector we created with to_tsvector().

The @@ operator to check if tsquery matches tsvector, it's returns true (t) if matched, otherwise returns false (f).

Let's see some queries below:

select to_tsvector("the green eggs and ham") @@ to_tsquery("egg")
?column?
--------
t
select to_tsvector("the green eggs and ham") @@ to_tsquery("eggs")
?column?
--------
t

Use & for AND in the search query:

select to_tsvector("the green eggs and ham") @@ to_tsquery("eggs & red")
?column?
--------
f

Use | for OR in the search query:

select to_tsvector("the green eggs and ham") @@ to_tsquery("eggs | red")
?column?
--------
t

Use the proximity symbol <-> for searching for terms that are a certain "distance" apart. For example, search the phase green egg, green is followed immediately by a match for egg.

select to_tsvector("the green eggs and ham") @@ to_tsquery("green <-> egg")
?column?
--------
t

For example, search for the phase egg [1 word] ham, find egg and ham within 2 words of each other:

select to_tsvector("the green eggs and ham") @@ to_tsquery("egg <1> ham")
?column?
--------
t

Use the negation symbol ! to find phrases which don't contain a search term. For example, search for the phase that have egg but not ham:

select to_tsvector("the green eggs and ham") @@ to_tsquery("egg & !ham")
?column?
--------
f

References

  • https://supabase.io/docs/guides/database/full-text-search
  • https://www.compose.com/articles/mastering-postgresql-tools-full-text-search-and-phrase-search/
  • https://www.postgresql.org/docs/9.5/textsearch.html
Jul 4, 2021·5 years ago
|Data|
DataData EngineeringDatabase
|Edit|

Related Posts

Good reasons to use ClickHouse

More than 200+ companies are using ClickHouse today. With many features support, it's equally powerful for both Analytics and Big Data service backend.

Aug 29, 2021·5 years ago
Read more

DuckDB

In this post, I want to explore the features and capabilities of DuckDB, an open-source, in-process SQL OLAP database management system written in C++11 that has been gaining popularity recently. According to what people have said, DuckDB is designed to be easy to use and flexible, allowing you to run complex queries on relational datasets using either local, file-based DuckDB instances or the cloud service MotherDuck.

Sep 3, 2023·2 years ago
Read more

Airflow control the parallelism and concurrency (draw)

How to control parallelism and concurrency

Jul 16, 2023·3 years ago
Read more

Running Spark in GitHub Actions

This post provides a quick and easy guide on how to run Apache Spark in GitHub Actions for testing purposes

May 7, 2023·3 years ago
Read more
On this page
  • to_tsquery()
  • References
On this page
  • to_tsquery()
  • References