SQL

Implement Basic Full-Text Search (PostgreSQL Example)

Discover how to perform basic full-text searches on text columns in your database, returning results based on relevance (PostgreSQL specific).

SELECT
    p.id,
    p.title,
    p.description,
    ts_rank(to_tsvector('english', p.title || ' ' || p.description), query) AS rank
FROM products p,
    to_tsquery('english', 'search & term') query
WHERE to_tsvector('english', p.title || ' ' || p.description) @@ query
ORDER BY rank DESC;
How it works: This PostgreSQL snippet demonstrates a basic full-text search. It converts the concatenated `title` and `description` into a `tsvector` (a special data type for full-text search) and searches for a `tsquery` ('search & term'). The `@@` operator checks for matches, and `ts_rank` calculates a relevance score, allowing results to be ordered by how well they match the search query.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs