How to Implement Hybrid Search with PostgreSQL (Full Tutorial)

Dave EbbelaarAbout 5 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • RAG (Retrieval-Augmented Generation): A framework for enhancing the knowledge of large language models (LLMs) by retrieving relevant information from an external knowledge base.
  • Semantic Search: Searching for information based on the meaning and context of the query, using vector embeddings.
  • Keyword-Based Search: Searching for information based on the presence of specific keywords in the text.
  • Hybrid Search: Combining semantic search and keyword-based search to improve retrieval accuracy.
  • Re-ranking: Using an LLM to re-order the results retrieved by a search engine, based on their relevance to the query.
  • Vector Embeddings: Numerical representations of text that capture its semantic meaning.
  • PGVector: A PostgreSQL extension for storing and querying vector embeddings.
  • Timescale Vector: A client library from Timescale for performing similarity searches on a PostgreSQL database using PGVector.
  • GIN Index: A generalized inverted index in PostgreSQL, used for full-text search.
  • TS Rank CD: A PostgreSQL function for ranking search results based on keyword relevance.
  • Cohere Re-ranking Model: A large language model from Cohere used to re-rank search results.

Setting up the Docker Environment

  • The video demonstrates setting up a PostgreSQL database with the TimescaleDB extension using Docker Compose.
  • The docker-compose.yml file configures a PostgreSQL instance with necessary extensions and settings for similarity search and vector operations.
  • Command: docker compose up -d (run in detached mode)
  • The TimescaleDB image includes PGVector, enabling vector storage and similarity search within PostgreSQL.

Creating a Python Environment and Installing Dependencies

  • A new virtual environment is created using either virtualenv or conda.
  • The requirements.txt file lists the Python packages needed for the project, including:
    • datasets: For loading the CNN DailyMail dataset.
    • openai: For generating vector embeddings.
    • psycopg2: For connecting to the PostgreSQL database.
    • cohere: For re-ranking search results (optional).
  • The .env file stores sensitive information such as API keys.

Inserting Documents and Generating Embeddings

  • The insert_vectors.py script loads the CNN DailyMail dataset and inserts articles into the PostgreSQL database.
  • A subset of 1,000 articles is selected for demonstration purposes.
  • The get_embedding function uses the OpenAI API to generate vector embeddings for each article.
  • The embeddings, along with article content and metadata, are stored in a PostgreSQL table named "documents".
  • The script creates the table, adds a vector search index using PGVector, and adds a GIN index for keyword search.
  • The upsert operation inserts the data into the database.

Implementing Semantic Search

  • The search.py script demonstrates different search methods.
  • Semantic search is performed using the Timescale Vector client library.
  • The semantic_search function queries the database for articles similar to a given query, based on cosine similarity of the embeddings.
  • The function returns a list of articles ranked by their similarity score.

Implementing Keyword-Based Search

  • Keyword-based search is implemented using PostgreSQL's built-in full-text search capabilities.
  • The keyword_search function uses a SQL query with the TS Rank CD function to rank articles based on keyword relevance.
  • A GIN index is used to speed up keyword searches.
  • Keyword-based search is more restrictive than semantic search, as it only retrieves documents that contain all of the specified keywords.

Implementing Hybrid Search

  • Hybrid search combines semantic search and keyword-based search to improve retrieval accuracy.
  • The hybrid_search function first performs a keyword search, then a semantic search, and combines the results.
  • Duplicate articles are removed from the combined results.
  • Hybrid search aims to capture both the semantic meaning and the specific keywords of the query.

Implementing Re-ranking

  • Re-ranking uses an LLM to re-order the results retrieved by the hybrid search, based on their relevance to the query.
  • The rerank parameter in the hybrid_search function enables re-ranking.
  • The Cohere re-ranking model is used to re-rank the results.
  • Re-ranking can improve the quality of the search results by considering the context and meaning of the query.

Synthesizing the Response

  • The synthesizer function takes the re-ranked search results and generates a summary of the information.
  • The function uses a large language model to synthesize the response.
  • The synthesized response is presented to the user.

Key Arguments and Perspectives

  • Combining semantic search and keyword-based search can improve retrieval accuracy compared to using either method alone.
  • Re-ranking can further improve the quality of the search results by considering the context and meaning of the query.
  • Using a PostgreSQL database with PGVector for RAG offers several advantages, including:
    • Control over the entire system.
    • No reliance on external vector databases.
    • Simplified data management.
    • Flexibility to deploy the system on any platform.

Notable Quotes

  • (Not explicitly quoted, but the video emphasizes the benefits of using PostgreSQL for RAG due to its control, flexibility, and simplified data management.)

Technical Terms and Concepts

  • Cosine Similarity: A measure of the similarity between two vectors, calculated as the cosine of the angle between them.
  • TS Vector: A PostgreSQL data type for storing pre-processed text for full-text search.
  • TS Query: A PostgreSQL data type for representing a full-text search query.
  • Stop Words: Common words (e.g., "the", "a", "is") that are typically removed from text before indexing.
  • U ID: Universally Unique Identifier

Logical Connections

  • The video builds upon a previous tutorial on setting up a PostgreSQL database for RAG.
  • It starts with basic semantic search and gradually introduces more advanced techniques, such as keyword-based search, hybrid search, and re-ranking.
  • Each technique is explained in detail, with code examples and explanations of the underlying concepts.
  • The video concludes by demonstrating how to synthesize the search results into a coherent response.

Data, Research Findings, or Statistics

  • The video mentions an article from entropic stating about how they got really good results with the Cohere re-ranking method.

Synthesis/Conclusion

The video provides a comprehensive guide to building an advanced RAG pipeline using PostgreSQL, PGVector, and other open-source tools. It demonstrates how to combine semantic search, keyword-based search, and re-ranking to improve retrieval accuracy and generate high-quality responses. The video emphasizes the benefits of using PostgreSQL for RAG, including control, flexibility, and simplified data management. The techniques and code examples presented in the video can be used to build powerful RAG systems for a variety of applications.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.