Sling Academy
Home/PostgreSQL/PostgreSQL Full-Text Search with SQLAlchemy and Flask

PostgreSQL Full-Text Search with SQLAlchemy and Flask

Last updated: December 20, 2024

Implementing full-text search in web applications often becomes a necessity as the amount of content grows. PostgreSQL offers robust inbuilt capabilities for full-text search that integrate seamlessly with Python through libraries like SQLAlchemy and Flask. This article will guide you through setting up PostgreSQL full-text search in a Flask application using SQLAlchemy.

Setting Up the Environment

Before you start implementing full-text search, ensure that you have a PostgreSQL database set up and that your Flask application can connect to it using SQLAlchemy.

# app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/mydatabase'
db = SQLAlchemy(app)

In the code above, replace username, password, and mydatabase with your PostgreSQL credentials and database name.

Defining the Model

For this example, assume you have a simple model called Article that stores articles. Add a column for the searchable content.

from sqlalchemy.dialects.postgresql import TSVECTOR

class Article(db.Model):
    __tablename__ = 'article'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255))
    content = db.Column(db.Text)
    search_vector = db.Column(TSVECTOR)

The TSVECTOR type will store searchable text content within your PostgreSQL database.

Creating and Updating the Search Vector

Next, you’ll need to ensure that the search_vector is updated whenever articles are inserted or updated. You can achieve this through triggers in PostgreSQL.

-- SQL script to create a trigger
CREATE OR REPLACE FUNCTION article_search_vector_trigger() RETURNS TRIGGER AS $$
BEGIN
  NEW.search_vector :=
    to_tsvector('english', COALESCE(NEW.title, '') || ' ' || COALESCE(NEW.content, ''));
  RETURN NEW;
END
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_search_vector
BEFORE INSERT OR UPDATE ON article
FOR EACH ROW EXECUTE PROCEDURE article_search_vector_trigger();

This SQL script creates a function and a trigger that automatically updates the search_vector column whenever an article is inserted or updated.

To perform a full-text search, format queries to use the @@ operator comparing TSQUERY with TSVECTOR.

from sqlalchemy import func

def search_articles(search_term):
    ts_query = func.to_tsquery('english', search_term)
    return Article.query.filter(Article.search_vector.op('@@')(ts_query)).all()

This function transforms the user’s search term into a TSQUERY and then filters articles to return those matching the search condition.

Conclusion

Integrating full-text searching in Flask applications using PostgreSQL and SQLAlchemy allows for creating powerful search interfaces. It harnesses the efficiency of PostgreSQL's inbuilt full-text search mechanism, leveraging triggers for automatic updates, and using SQLAlchemy for seamless querying within Python applications.

Next Article: How to Use PostgreSQL Full-Text Search in Spring Boot Applications

Previous Article: PostgreSQL Full-Text Search with Node.js and Sequelize

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB