Skip to content

Vector databases: what they are for

B2Bnetwork team · · 6 min

Technology and trends

In brief

A vector database stores texts, images, or products as vectors of numbers (embeddings) and quickly finds those closest to a query in meaning, not just by words. It is used for semantic search, recommendations, detecting similar tickets, and RAG, that is, giving a language model the right fragments of company documents. It can be a standalone product or an extension of an existing database, e.g. pgvector in PostgreSQL. The quality of the results depends more on how documents are split and on the embedding model than on the database itself.

What embeddings are and why store them

An embedding is a vector of several hundred or several thousand numbers that a machine learning model assigns to a piece of text, an image, or a product description. The model is trained so that pieces with similar meaning get vectors that lie close to each other.

As a result, the query "how to change the account number for payouts" will land on a procedure titled "Updating details for benefit transfers," even though they share no words. Full-text search will not find it, because it searches by words and their inflected forms.

Closeness is measured with a distance function, most often:

MeasureWhat it measuresWhen
Cosine distancethe angle between vectorsmost often for text embeddings
Inner productthe angle and the length of vectorswhen the model returns normalized vectors or the documentation recommends it
Euclidean distance (L2)distance in spaceimages, numerical data, some models

The measure is chosen to fit the embedding model, according to its documentation, not to fit the database.

How search in a vector database works

A vector database finds a query's nearest neighbors in a set of millions of vectors, usually approximately, because an exact comparison with every vector would be too slow. Approximate nearest neighbor (ANN) indexes are used for this.

The most popular is HNSW (Hierarchical Navigable Small World), described by Malkov and Yashunin. It builds a multi-level graph in which a search starts at a sparse top layer and descends into denser and denser ones, closing in on the query. Another approach, IVF, divides the space into clusters and searches only those closest to the query.

Approximation means a trade-off: index parameters are set between speed, memory use, and completeness of results (recall). In RAG at a bank or an office, losing the right fragment is worse than a few extra milliseconds, so parameters are chosen on a test set, not taken from the defaults.

An important element is filtering by metadata. A branch employee should not get a fragment from a document they have no permissions for, and the result should come from the current version of the procedure. So the database has to combine vector search with conditions such as `dzial = 'kredyty' AND obowiazuje = true`.

What vector databases are used for

Vector databases are used wherever you need to find "similar" rather than "identical." The most common uses:

  • RAG, that is, search for language models. An AI Worker or an assistant gets the few most relevant fragments of company documents to answer with, instead of the whole repository. We describe the whole flow in the article RAG: how AI uses company documents.
  • Semantic search in a knowledge base, regulations, procedures, and technical documentation.
  • Recommendations in a shop: products similar to the one being viewed or to purchase history.
  • Similar tickets in customer support and service: a new ticket immediately shows three similar ones that are already resolved.
  • Duplicate detection in customer, supplier, and material master records.
  • Image search, e.g. photos of product defects similar to a new complaint.

A separate database or an extension of an existing one

Not every company needs a new database product. The choice depends on scale, requirements, and what already runs in the company.

ApproachWhen it makes senseWhat to watch for
An extension of a relational database, e.g. pgvector in PostgreSQLup to millions of vectors, data and permissions are already in that database, the team knows PostgreSQLtuning indexes and memory with larger sets
A search engine with vector support (e.g. full-text engines with a vector index)hybrid search is needed: words and meaning togethermaintaining an additional cluster
A specialized vector databasevery large sets, high load, complex filtersanother system to secure, back up, and monitor

pgvector supports HNSW and IVFFlat indexes and, among others, Euclidean distance, cosine distance, and inner product. An example query:

SELECT id, title
FROM chunks
WHERE department = 'loans' AND is_current
ORDER BY embedding <=> $1   -- cosine distance from the query vector
LIMIT 5;

In practice hybrid search works well: full-text results (e.g. a statute article number, a product code) are combined with vector results. Embeddings handle exact identifiers poorly, and word-based search handles synonyms and paraphrase poorly.

What determines the quality of results

Quality is determined not by the database but by what goes into it. The most important decisions:

  1. Splitting documents into fragments. Fragments that are too large blur the meaning, ones that are too small lose context. It is good to split by structure: chapter, paragraph, procedure step, with the section title in the metadata.
  2. The embedding model. It must handle Polish and industry vocabulary well. Changing the model means recomputing all the vectors.
  3. Freshness. When a procedure changes, the old fragment has to be removed or marked, otherwise the model will answer from a version that is no longer in force.
  4. Permissions. A permission filter in the query, not in the interface. More in the article on company data and language models.
  5. Measurement. A set of test questions with the expected documents and measures such as accuracy in the top five results. Without it, every parameter change is guesswork.

Weak search is one of the main causes of wrong model answers, described in the article on hallucinations in language models.

DYNAMINDS Labs, the R&D division of B2Bnetwork, deploys AI Workers that use company documents through vector search. B2Bnetwork engineers also build data platforms and search engines; the scope of services is described in the What we do section.

Questions and answers

Do you need a vector database for RAG?

You need a search mechanism that will find the right fragments of documents. Most often that is vector search, but it does not have to be a separate product: for smaller sets an extension of an existing database, e.g. pgvector in PostgreSQL, is enough. Combining vector search with full-text search also gives good results.

How does vector search differ from full-text search?

Full-text search looks for words and their inflected forms, so it finds exact names, numbers, and codes well. Vector search compares meaning, so it will find a document described in other words, but it handles identifiers less well. In company systems, a combination of both methods works best.

Is data in a vector database secure?

Embeddings are created from the content of documents, so they have to be protected like the documents themselves: encryption, backups, access control, and a permission filter on every query. It also matters where embeddings are computed: an external model service means the content of documents goes to its provider, which has to be factored into the risk assessment.

How do you check whether vector search works well?

Prepare a set of several dozen or several hundred real questions with the documents that should appear in the results. Measure how often the right document is in the top five results and at which position. Repeat the measurement after every change of embedding model, document splitting, or index parameters.

What it looks like in a project

Considering a new technology for your system? Talk to an engineer.

Let's talk

Read more: Technology and trends