Skip to content
Cosvexa – Cosvexa home

Research · Building with AI

Vector databases and semantic search

A vector database stores embeddings, lists of numbers that represent the meaning of text, images or other data, and finds the stored vectors most similar to a query vector. That makes semantic search possible: matching by meaning rather than exact words. To stay fast at millions of vectors, it uses approximate nearest-neighbour indexes such as HNSW, IVF and product quantization.

Cosvexa team · Published

Abstract editorial illustration of a constellation of points in layered space, with a query point linked by glowing lines to its nearest neighbours

Key points

  • Semantic search compares embeddings, so it can match paraphrases that share no keywords.
  • Exact nearest-neighbour search is slow at scale; vector databases use approximate indexes that trade a little recall for speed.
  • HNSW (graph-based), IVF (cluster-based) and product quantization (compression) are the most common index techniques.
  • Real systems also need metadata filters, updates, access control and often hybrid keyword search.
  • Retrieval quality depends more on the embedding model and the chunking than on the database.

What a vector database is

An embedding is a vector, a fixed-length list of numbers, produced by a neural network so that items with similar meaning end up close together. Sentences about refund policies cluster near each other and far from sentences about rocket engines. Semantic search uses this: embed the query, then find the stored items whose vectors are nearest. Sentence-BERT (Reimers and Gurevych, 2019) is a well-known early example of a model trained to produce sentence embeddings suitable for this kind of comparison.

A vector database is a system built to store such vectors along with their source data and metadata, and to answer nearest-neighbour queries quickly, even over billions of entries. The term covers dedicated products, vector extensions to general databases (for example pgvector for PostgreSQL) and vector fields in search engines. Underneath, most rely on the same families of index algorithms, many of them implemented in open libraries such as Faiss from Meta's research group.

How it works

Measuring similarity

MetricWhat it measuresTypical use
Cosine similarityThe angle between two vectors, ignoring lengthText embeddings; the most common default
Dot productAngle and length togetherModels trained for it; equals cosine when vectors are normalized
Euclidean (L2) distanceStraight-line distance between pointsMany image and classic machine-learning embeddings

Which metric to use is set by how the embedding model was trained; the model's documentation normally says. Using the wrong one quietly lowers retrieval quality.

Why exact search does not scale

The simplest method, called brute force or flat search, compares the query with every stored vector. That is exact, and for tens of thousands of vectors it is fast enough. But cost grows linearly with the number of vectors and their dimension. Memory grows too: one million 768-dimensional vectors stored as 32-bit floats take about 3 GB before any index overhead. For large collections and low-latency applications, databases use approximate nearest-neighbour (ANN) indexes, which return most of the true nearest neighbours while examining only a small fraction of the data.

The main index families

  • HNSW (Hierarchical Navigable Small World graphs). Introduced by Malkov and Yashunin (arXiv 2016, later in IEEE TPAMI). Each vector is a node linked to some of its neighbours, with sparse upper layers for long jumps and dense lower layers for local search. A query starts at the top, greedily walks toward closer nodes and descends layer by layer. It is fast and accurate and widely used, but memory-hungry and relatively costly to build.
  • IVF (inverted file index). Vectors are grouped into clusters with k-means. At query time only the vectors in the few clusters nearest to the query are scanned. The number of clusters probed controls the speed-accuracy trade-off.
  • Product quantization (PQ). Jégou, Douze and Schmid (2011) proposed splitting each vector into sub-vectors and replacing each with the ID of its nearest centroid from a small codebook. A vector of thousands of bytes becomes a few dozen bytes, and distances can be estimated from lookup tables. PQ is often combined with IVF; see also quantization for the related idea in model weights.
  • Disk-based and GPU indexes. Graph indexes designed for SSDs let collections exceed RAM, and GPU implementations, such as those described by Johnson, Douze and Jégou (2017), speed up building and searching very large indexes.

Every ANN index has tuning knobs, such as how many graph neighbours to explore or how many clusters to probe. Turning them up raises recall, the share of true nearest neighbours returned, at the cost of latency. A sensible practice is to measure recall against brute-force results on a sample of real queries and pick the cheapest setting that meets your target.

What a database adds to an index

An index alone answers which vectors are nearest. A database adds what applications need around it: inserting, updating and deleting records; storing the original text and metadata such as source, date, language and owner; filtering by that metadata during search (only documents this user may see, only this year's contracts); hybrid search combining vector similarity with keyword scoring; replication, backups and sharding across machines; and multi-tenant isolation. Filtering is harder than it sounds, because a strict filter can remove most of the candidates an ANN index would explore, and systems differ in how well they handle that.

  1. 1

    Choose an embedding model

    Pick a model suited to your language and domain, and note its dimension and similarity metric.

  2. 2

    Chunk and embed

    Split documents into passages, embed each passage and keep its text and metadata.

  3. 3

    Build the index

    Start with flat search for small sets; move to HNSW or IVF with quantization as the collection grows.

  4. 4

    Query

    Embed the user's query with the same model, apply filters, retrieve the top results and optionally rerank them.

  5. 5

    Measure and tune

    Check recall and latency on real queries and re-embed everything if you change the embedding model.

A concrete example. A support site has 200,000 help-centre passages. A customer types my card was charged twice. Keyword search would favour passages containing charged and twice. The embedding of the query lands near passages about duplicate payments, pending authorizations and refunds for double billing, even where none of the query's words appear. The vector database, using an HNSW index, returns the twenty nearest passages in a few milliseconds, filtered to the customer's country and language. A reranker then picks the best three for the answer, as in retrieval-augmented generation.

Why it matters

Vector search is the retrieval layer behind most RAG systems, recommendation engines, duplicate and near-duplicate detection, image and audio search, and memory for chat assistants and AI agents. Because embeddings can be computed for text, images and audio in a shared space, a single index can support searches such as finding product photos that match a written description. It also makes cross-language search possible when the embedding model is multilingual.

Limits and common misconceptions

  • The database does not understand meaning; the embedding model does. A vector database returns whatever is closest in the embedding space. If the embeddings are poor for your domain, no index will fix that.
  • Semantic is not always better. Exact identifiers, error codes, part numbers and names are often found better by keyword search. Hybrid search exists because each method misses things the other finds.
  • Approximate means approximate. ANN indexes can miss true nearest neighbours. That is usually acceptable, but it should be measured, not assumed.
  • You may not need a dedicated system. For small collections, a flat index in memory or a vector extension to an existing database can be simpler and sufficient.
  • Changing models means re-indexing. Vectors from different embedding models are not comparable, so switching models requires re-embedding the whole collection.
  • Embeddings are not anonymous. Research has shown that text can sometimes be partially reconstructed from its embeddings, so vector stores holding sensitive data deserve the same protection as the source text.

Where it is going

Vector search is becoming a standard feature rather than a separate category: relational databases, document stores and search engines increasingly ship vector indexes alongside keyword search. Active work includes better filtered search, more compact quantized vectors to cut memory cost, indexes that live mostly on disk or object storage, multi-vector representations that keep one vector per token or image patch for finer matching, and tighter integration with rerankers. As models with long context windows improve, some applications retrieve less, but for large and changing collections efficient similarity search remains essential; see long context vs RAG.

Frequently asked questions

What is a vector database used for?

It stores embeddings and quickly finds the ones most similar to a query. Common uses are semantic search, retrieval for RAG chatbots, recommendations, image search and detecting near-duplicate content.

What is the difference between semantic search and keyword search?

Keyword search matches the words in the query, weighted by how rare they are. Semantic search compares embeddings, so it finds passages with the same meaning even when the wording differs. Many systems combine both in hybrid search.

What is HNSW?

HNSW, Hierarchical Navigable Small World, is a graph-based index for approximate nearest-neighbour search introduced by Malkov and Yashunin. It links each vector to nearby vectors across several layers and finds neighbours by walking the graph, giving fast and accurate search at the cost of extra memory.

Do I need a vector database for RAG?

Not necessarily. Small collections work well with an in-memory index or a vector extension to a database you already run. Dedicated systems help with very large collections, heavy filtering, frequent updates and high query volumes.

Which similarity metric should I use?

Use the metric the embedding model was trained for, usually cosine similarity or dot product for text models. When vectors are normalized to unit length, cosine and dot product give the same ranking.

References

  1. Reimers and Gurevych (2019). Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
  2. Jégou, Douze and Schmid (2011). Product Quantization for Nearest Neighbor Search
  3. Malkov and Yashunin (2016). Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs
  4. Johnson, Douze and Jégou (2017). Billion-scale similarity search with GPUs
  5. Douze et al. (2024). The Faiss library

More in this section

Start free: 50 Cosvexa-1 questions a day

Every major model in one workspace; on the Free plan Cosvexa-1 answers 50 questions a day. No card needed to start.