postgresqlfull-text-searchtrigram

Postgres Full Text Search vs pattern matching (like)


When implementing search functionalities in Postgres, we have the options to use PostGres Full text Search (FTS) or pattern matching (like) and both come with indexes for optimize queries.

eg.

gin (to_tsvector('language', text)) for ts_vector

gin (text gin_trgm_ops) for pattern matching

I am wondering when we want to use Full-text Search or pattern matching in general.

Also, if we don't need language stemming, are there still values to use tsvector.


Solution

  • Full-text search is what you need if you are looking for whole words in natural language texts. It supports prefix search and searching for the parts of hyphenated words, but it does not support pattern matching or similarity search.

    Trigram indexes are useful if you search patterns or similarity. It has no support for stemming (e.g., finding wolves when you search for wolf) and stop words, and the indexes are normally larger.

    To avoid stemming and stop word removal with full-text search, you can use the simple text search configuration.