Sunday, July 12, 2026

How to Ingest pdfs into the OpenSearch for Semantic searching?

 Ingesting PDF documents for embedding-based search (often called Semantic Search or Vector Search) in Amazon OpenSearch is a fundamentally different process than standard log or text ingestion.

While standard ingestion involves sending raw text or JSON fields to be indexed by keyword, embedding-based ingestion requires transforming unstructured PDF content into high-dimensional vector embeddings before storing them.

How Embedding-Based Ingestion Differs

  • Standard Ingestion: You send text directly. OpenSearch uses inverted indexes (like a book's index) to perform keyword matching.

  • Embedding-Based Ingestion: You must first extract text from the PDF, send that text to an AI model (like Amazon Bedrock or a local model) to generate numerical vectors, and then store those vectors in an OpenSearch knn (k-nearest neighbors) index.

Steps for Embedding-Based Ingestion in OpenSearch

1. Preprocessing and Text Extraction

PDFs are binary files. OpenSearch cannot read them directly. You must first extract the text:

  • Use a library (like PyPDF2, LangChain, or Amazon Textract) to convert the PDF content into clean, readable text strings.

  • Chunking: Since AI models have input limits, you must split long PDF text into smaller, overlapping "chunks" (e.g., 500 tokens each).

2. Vectorization (The Embedding Step)

You transform your text chunks into vectors.

  • Send your chunks to an embedding model (e.g., amazon.titan-embed-text-v1 via Amazon Bedrock).

  • The model returns a list of floating-point numbers (e.g., 1024 dimensions) representing the semantic meaning of that chunk.

3. Configuring an OpenSearch knn Index

Before you store the vectors, your index must be configured to support Vector Search.

  • You create an index with a knn_vector field type.

  • You must define an engine (e.g., nmslib, faiss) and a method (e.g., hnsw for high performance) to allow for efficient similarity searching.

4. Ingestion Pipeline / Integration

You have two primary ways to connect this:

  • The Ingestion Pipeline (Recommended): You can use an OpenSearch Ingestion Pipeline to automatically call your embedding model (like Bedrock) while the document is being indexed. This keeps the vectorization logic inside the OpenSearch ecosystem.

  • Application-Side Processing: Your application extracts the text, calls the Bedrock API to get the embedding, and then sends the final JSON document (containing both the raw text and the vector) to the OpenSearch Bulk API.

Summary: Why is it different?

FeatureStandard Document IngestionEmbedding-Based Ingestion
Data FormatJSON / Raw LogsJSON + Vector Array
ProcessingTokenization (Keyword splitting)ML Inference (Model generation)
Index TypeInverted IndexKNN Index (knn_vector field)
Search MethodBM25 / Keyword MatchVector Similarity (Cosine/Euclidean)

Key Architectural Note: The "In-Cluster" Approach

Amazon OpenSearch Service now supports "ML Commons," which allows you to integrate your OpenSearch domain directly with Amazon Bedrock. This is highly efficient because:

  1. You define an Ingestion Pipeline inside OpenSearch.

  2. You send the raw PDF text chunks to that pipeline.

  3. OpenSearch automatically calls Bedrock to generate the embeddings.

  4. OpenSearch indexes the vectors for you, abstracting away much of the manual vector handling in your application code.

No comments:

Post a Comment