Converts application text into a tsvector containing normalized lexemes and their original positions. Pass a text-search configuration explicitly or let PostgreSQL use default_text_search_config, which is english in this setup. section 6.2.1
Use the generated lexemes in one of three ways:
- Generate them during every search query, then discard them; this avoids storage but repeats processing. section 6.2.2
- Store them in a tsvector column and index that column with GIN or GiST for faster searches. section 6.2.2
- Build a GIN or GiST index directly from generated lexemes, avoiding a stored copy when the configuration can be specified. section 6.2.2
Convert a movie description
SELECT * FROM to_tsvector( 'The explorers must save the fragile peace between Earth and the aliens.'); The result includes lexemes such as 'explor', 'fragil', and 'alien', while removing stop words. Positions remain attached: 'explor':2 and 'alien':12. Concatenate a title and description when both fields should be searchable.
Store searchable movie text
ALTER TABLE omdb.movies ADD COLUMN lexemes tsvector GENERATED ALWAYS AS ( to_tsvector('english', coalesce(name, '') || ' ' || coalesce(description, '')) ) STORED; This keeps title and description lexemes available in a generated column for later indexing and querying.
