Skip to departures
Hpjadul
MAC END

Integrating Hybrid Search in PostgreSQL with Pinecone for Enhanced Recommendations

Departs
Crew
Richard Jones
Passengers
323

Explore how to implement hybrid search in a PostgreSQL application using Pinecone, enhancing recommendation systems for better user experiences.

Integrating Hybrid Search in PostgreSQL with Pinecone for Enhanced Recommendations

Integrating hybrid search capabilities into a PostgreSQL application using Pinecone provides developers an effective way to enhance user search experiences. This article outlines how to set up a recommendation system for a snack shop that allows customers to find products based on cravings and ingredients through optimized search functionalities.

Arjun Patel

Implementing Hybrid Search

To successfully implement hybrid search, leverage Pinecone for vector-based searches alongside PostgreSQL as the structured system of record. Both platforms can work together effectively by synchronizing data and enhancing search capabilities.

  • Pinecone: Utilized for vector search capabilities.
  • PostgreSQL: Acts as the central data repository for storing inventory and related information.
  • Hybrid Search: Utilizes both semantic vector embeddings and keyword-based search to provide more relevant results.

For the snack shop scenario, we aim to create a system where customers can express their food cravings, either by descriptive words or specific ingredients, in order to receive corresponding product recommendations. The combination of semantic search and traditional full-text search creates a robust recommendation system, which is the essence of hybrid search.

Understanding Your Dataset

The snack shop sells 1,160 different items. Each item is characterized by various attributes such as name, description, price, and stock quantity. For example, a sample description could be:

“Crispy bacon-wrapped jalapeño poppers stuffed with cream cheese and sharp cheddar. Each bite delivers a perfect balance of spicy heat and creamy richness.”

This rich data enables us to index snacks effectively using both semantic search through vector embeddings and keyword-based full-text search.

A diagram showing the flow of a hybrid search query against an index.

Utilizing Pinecone’s Documents API

In 2026, the workflow allows combining semantic search and keyword search within a single index in Pinecone. This is facilitated by the Pinecone Documents API, which helps to structure and store data effectively.

Documents in Pinecone are defined within a schema, containing essential attributes like the snack’s name and description for text search, along with an embedding that captures semantic meaning.


// Index Schema Definition
name: { type: "string", fullTextSearch: { language: "en", stemming: true, stopWords: true } },
text: { type: "string", fullTextSearch: { language: "en", stemming: true, stopWords: true } },
embedding: { type: "dense_vector", dimension: EMBED_DIMENSION, metric: "cosine" },

This approach allows handling both keyword searches, like "cheddar," and more nuanced semantic searches like "something spicy and crunchy". The integration of these methods enhances the recommendation quality.

Why Hybrid Search with Pinecone and PostgreSQL?

While Pinecone provides efficient vector search capabilities, PostgreSQL acts as the system of record for inventory management. This duo enables a reliable architecture for real-time data management:

  • Structured Data: PostgreSQL provides detailed structured data regarding inventory, enabling effective aggregations.
  • Separation of Functions: This architecture keeps search operations separate from checkout processes, enhancing overall performance and reliability.
  • Scalability: Pinecone offers straightforward scaling, accommodating varying loads without incurring significant costs.

Integration Mechanism: Pinecone Ranks, PostgreSQL Answers

The interaction model defines a clear split between searching and data retrieval:

A gif showcasing how batch and live sync work in Pinecone

// Code Segment for Searching in Pinecone
const { matches } = await index.documents.search({
  topK,
  scoreBy,
  includeFields: [],
  ...(filter ? { filter } : {}),
});

Pinecone handles the search and returns product IDs and scores, while the detailed product data like name, price, and availability is fetched from PostgreSQL. This ensures accurate presentation of product information, minimizing the risk of displaying incorrect data such as sold-out items.

Creating a Simulation for User Interaction

To visualize how this system performs under real-world conditions, a simulation of shopper interactions demonstrates the interaction between purchases and inventory management.

The `/shop` endpoint simulates a shopping day where shopper agents search for snacks and attempt to purchase items, with an emphasis on showing how stock levels change as items are sold. In this simulation, only one of each item is available to demonstrate the stock management mechanisms effectively.

Shopping simulation showcasing user queries and purchases

Ensuring Synchronization between PostgreSQL and Pinecone

A crucial element of the system's design is how synchronization is managed between PostgreSQL and Pinecone:

  • Catalog Sync: Regularly copies product details from PostgreSQL into Pinecone, ensuring that all items are represented accurately.
  • Sales Updates: Reflects changes like sold-out items in real-time, preventing users from being offered out-of-stock products.

Rebuilding the Index

PostgreSQL can rebuild the Pinecone index whenever needed based on the data available in its `snacks` table. The following operation enables this rebuilding:

// Rebuilding Pinecone Index
const page = await db
  .select({...})
  .from(snacks)
  .orderBy(asc(snacks.id))
  .limit(UPSERT_BATCH)
  .offset(offset);

await upsertToIndex(await embedSnackDocuments(page));

This process ensures Pinecone always has the correct up-to-date vector embeddings sourced directly from PostgreSQL.

Sales Updates: Two Modes

  • Batch Mode: In this approach, Pinecone does not maintain stock information. Sold-out items remain until an explicit sync occurs at the end of the trading day.
  • Live Mode: Updates the documents immediately to reflect changes in stock levels. This involves an atomic patch to change the in-stock status:
// Live Stock Update
await index.documents.update({ documents: [{ _id: id, in_stock: inStock }] });

Testing the Integration

Setting up this integration is straightforward once the foundational logic is established. The demo setup requires a Pinecone API key, a PostgreSQL table, and a preferred method for hosting these services.

For hands-on experience, users can follow the comprehensive instructions available on GitHub: postgres-snack-search repository. This example utilizes Drizzle with Supabase but can equally work with any PostgreSQL host that provides a connection string.

Syncing between PostgreSQL and Pinecone can be efficiently executed, providing a solid groundwork for hybrid search implementations. This setup exemplifies how to use both platforms to deliver a dynamic and user-responsive search experience critical for modern applications.

Next time you're looking to enhance search functionalities in your application, considering a hybrid search model with Pinecone and PostgreSQL may prove beneficial.

Source: Richard Jones · www.pinecone.io
Arrived · Hpjadul

Also departing from this gate