Lesson 4 of 5 Operational and Purpose-Built Databases

Building Vector Stores and Indexes for Semantic Search

This lesson explains how vector search finds items with similar meaning by comparing embeddings, powering semantic search and retrieval augmented generation (RAG), and the pipelines data engineers build to feed it.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how embeddings and similarity measures work.
  • Identify the vector stores available on AWS.
  • Choose between HNSW and IVF indexes.
  • Build vector pipelines with Bedrock Knowledge Bases or custom jobs.

Embeddings and similarity

Embeddings are numeric vectors, produced by models such as Amazon Titan Text Embeddings on Bedrock, that represent meaning. Similar items lie close together, measured by cosine similarity, Euclidean (L2) distance or the inner (dot) product.

True or false

Embeddings

Exam skills 2.4.6

Decide whether each statement about embeddings is true.

Log in to try this activity Interactive activities are free with an account.

Vector stores on AWS

A vector store holds embeddings, numeric representations of text or images, and finds the ones most similar to a query. AWS offers several. Aurora PostgreSQL and RDS for PostgreSQL with the pgvector extension keep vectors in the same database as the relational data they describe, queried with SQL, which suits applications already using PostgreSQL. OpenSearch Service k-NN indexes and OpenSearch Serverless vector collections handle search at large scale, including hybrid search that combines keywords with vector similarity.

Amazon MemoryDB provides durable, in-memory vector search with single-digit millisecond latency. Amazon S3 Vectors stores very large numbers of vectors at low cost, for workloads that can accept slower queries. Neptune Analytics combines graph queries with vectors, and DocumentDB adds vector search to document data. The following table summarises the stores.

StoreBest for
Aurora PostgreSQL / RDS for PostgreSQL with pgvectorVectors next to relational data, queried with SQL
OpenSearch Service k-NN, OpenSearch Serverless vector collectionsSearch at scale, including hybrid keyword and vector search
Amazon MemoryDBDurable in-memory search with very low latency
Amazon S3 VectorsLow-cost storage of huge numbers of vectors
Neptune Analytics, DocumentDBGraph plus vectors; document data
Match the pairs

Vector stores

Exam skills 2.1.3

Match each requirement with the vector store that fits it.

Log in to try this activity Interactive activities are free with an account.

HNSW vs IVF indexes

Comparing a query with every stored vector is exact but too slow for millions of vectors, so stores use indexes that find close matches quickly. HNSW (hierarchical navigable small world) builds a multi-layer graph linking each vector to its neighbours; a search starts in the sparse top layer and moves closer to the query layer by layer. It gives high recall and fast queries and handles new inserts well, but uses more memory and takes longer to build.

IVF (inverted file index), called IVFFlat in pgvector, groups vectors into clusters, called lists, and searches only the lists nearest the query; the probes setting decides how many. It uses less memory and builds faster, but its accuracy depends on the lists and probes chosen, and it may need rebuilding as data changes, because the clusters were based on the original data. The following table summarises the differences.

HNSW
How it worksA multi-layer graph of vectors
Recall and speedHigh recall, fast queries
MemoryMore
Build timeSlower
Changing dataHandles inserts well
IVF (IVFFlat in pgvector)
How it worksClusters vectors into lists and searches the nearest lists (set by probes)
Recall and speedDepends on lists and probes
MemoryLess
Build timeFaster
Changing dataMay need rebuilding

Both are approximate nearest neighbour (ANN) methods that trade a little accuracy for speed; exact search compares every vector.

The following diagram shows how each index type finds near neighbours.

How the two approximate indexes search. HNSW starts in a sparse top layer of a graph and moves to closer neighbours layer by layer until it reaches the nearest vectors. IVF groups vectors into clusters (lists) and searches only the lists nearest the query; the probes setting decides how many, here two.
The layouts are schematic.
Sort into groups

HNSW or IVF?

Exam skills 2.1.8

Decide whether each statement describes HNSW or IVF.

Log in to try this activity Interactive activities are free with an account.

Building vector pipelines

Vector data can be prepared by a managed pipeline or a custom one. Amazon Bedrock Knowledge Bases automate the whole process: they read documents from a data source, split them into chunks, generate embeddings, store them in a vector store and keep the store synchronised with the source, which suits most retrieval applications.

Custom pipelines, built with AWS Glue or Lambda, chunk and embed data themselves and add metadata, such as product, date or access level, that searches can filter on. They suit data that needs unusual processing, data from sources without a connector, or pipelines that must fit into existing data engineering workflows.

The following diagram shows the indexing and query paths of a vector search pipeline for support articles.

The vector pipeline for support articles. Changed articles trigger a job that splits them into chunks, Amazon Titan Text Embeddings creates a vector for each chunk, and the vectors are stored with metadata in an HNSW index. At query time, the user's question is embedded with the same model and the nearest vectors are returned.
The question must be embedded with the same model as the articles.
Scenario questions

Managed or custom vector pipeline?

Exam skills 2.4.6 · 2.1.3

Choose how to build each vector pipeline.

Log in to try this activity Interactive activities are free with an account.
Worked example

Building a vector pipeline for search

A company wants semantic search over 2 million support articles that change daily.

StepService
Split articles into chunksA Glue or Lambda job
Create an embedding for each chunkAmazon Titan Text Embeddings through Amazon Bedrock
Store vectors with metadataOpenSearch Service (HNSW index), Aurora PostgreSQL with pgvector, or S3 Vectors for lowest cost
Keep in sync with article changesProcess only changed articles, triggered by events
QueryEmbed the user's question and find the nearest vectors

HNSW indexes give fast, accurate search at higher memory cost; IVF indexes use less memory but need training and can be less accurate. If the articles already live in Aurora, pgvector keeps vectors next to the relational data with no new database to run.

Check yourself

Where can vectors be stored alongside existing relational data?

Show answer

In Aurora or RDS for PostgreSQL with the pgvector extension.

Exam Tips

Remember for the exam

  • To store vectors alongside existing relational data, use Aurora or RDS for PostgreSQL with pgvector.
  • For the best recall and query speed choose HNSW; for lower memory use and faster builds choose IVF.
  • For durable, very low-latency in-memory vector search, use MemoryDB.
  • The lowest-cost storage for huge numbers of vectors is S3 Vectors.
  • For managed chunking, embedding and storage for RAG, use Bedrock Knowledge Bases.