Semantic Search, Vector Search and Knowledge Bases

How knowledge bases, document retrieval, semantic search and vector search fit together in RAG.

Last Updated:

Module 1 · Lesson 3 of 10 · Beginner

Semantic Search, Vector Search and Knowledge Bases

How knowledge bases, document retrieval, semantic search and vector search fit together in RAG.

The "R" in RAG is retrieval: finding the right pieces of your knowledge for a question. This lesson explains the four terms you will meet constantly: knowledge base, document retrieval, semantic search and vector search, and builds a tiny semantic search engine so you can see them work together.

What you will learn

  • What a knowledge base is in a RAG system
  • How document retrieval works
  • The difference between keyword search and semantic search
  • How vector search implements semantic search

Prerequisites

Knowledge base

A knowledge base is the collection of information a RAG system can draw on: documents, web pages, tickets, database records, transcripts. In practice it has two forms:

  • the source content, in the systems where it lives (a wiki, a shared drive, a database), and
  • the search index built from it: cleaned text split into chunks, with embeddings and metadata, stored in a vector database or search engine.

The quality of a RAG system is capped by its knowledge base. If a fact is missing, outdated or unreadable (for example in a scanned PDF that was never processed), no model can answer correctly from it.

Document retrieval

Document retrieval is the task of returning the items in the knowledge base most relevant to a query, ranked by relevance. In RAG the "documents" retrieved are usually chunks: passages of a few hundred words, so that the model receives focused evidence (Why Chunking Matters).

Keyword search vs semantic search

For the query 'forgot my password', keyword search misses 'Steps to change your login credentials' while semantic search finds it.
Figure 1. Keyword search matches words; semantic search matches meaning.
  • Keyword (lexical) search ranks documents by the words they share with the query, weighted by how rare those words are (for example BM25). It is precise for exact terms such as product codes and names, but misses synonyms and paraphrases.
  • Semantic search ranks documents by meaning. "Forgot my password" matches "change your login credentials" even though they share no important words.

Vector search: how semantic search is implemented

Vector search is the technique behind semantic search:

  1. An embedding model converts every chunk into a vector (a list of numbers) that represents its meaning.
  2. The query is converted into a vector with the same model.
  3. The search returns the chunks whose vectors are closest to the query vector, usually by cosine similarity.

A vector database stores the vectors and finds nearest neighbours quickly, even among millions (What is a Vector Database?).

Try it: a tiny semantic search engine

tiny_search.py
import numpy as np
from rag_helpers import embed       # local embedding model, see Python Environment Setup

knowledge_base = [
    "Reset your password from the login page using the 'Forgot password' link.",
    "Steps to change your login credentials in the account settings.",
    "The office is closed on public holidays.",
    "Invoices are emailed on the first working day of each month.",
]
doc_vectors = embed(knowledge_base)               # index: one vector per document

def keyword_search(query: str) -> list[str]:
    words = set(query.lower().split())
    return [d for d in knowledge_base if words & set(d.lower().replace(".", "").split())]

def semantic_search(query: str, k: int = 2) -> list[tuple[float, str]]:
    scores = doc_vectors @ embed(query, query=True)[0]
    return [(round(float(scores[i]), 3), knowledge_base[i]) for i in np.argsort(-scores)[:k]]

query = "forgot my password"
print("Keyword :", keyword_search(query))
print("Semantic:", semantic_search(query))
Keyword : ["Reset your password from the login page using the 'Forgot password' link."]
Semantic: [(0.82, "Reset your password from the login page ..."), (0.71, 'Steps to change your login credentials in the account settings.')]

The naive keyword search finds only the document that contains the exact words. Semantic search ranks both relevant documents at the top. Your scores may differ slightly.

Which one should you use?

Both. Semantic search handles paraphrases; keyword search handles exact identifiers such as "error XR-4471". Production RAG systems usually run both and merge the results, which is called hybrid search.

Common mistakes

  • Embedding queries and documents with different models: the vectors are not comparable.
  • Indexing whole long documents as one vector: meaning gets blurred.
  • Assuming a high similarity score means the answer is present: always check retrieved text.

Key takeaways

  • The knowledge base is the content a RAG system can retrieve from; its quality limits answer quality.
  • Document retrieval returns the most relevant chunks for a query.
  • Semantic search matches meaning; keyword search matches words; hybrid search combines both.
  • Vector search implements semantic search by comparing embeddings.

Frequently asked questions

Is semantic search the same as vector search?

Almost. Semantic search is the goal (search by meaning); vector search is the usual technique for achieving it.

Do I need a vector database for semantic search?

Not for small collections: a NumPy matrix works for tens of thousands of chunks. A vector database helps with scale, persistence, filtering and updates.

Practice exercises

  1. Add ten documents of your own and try queries that use synonyms.
  2. Find a query where keyword search beats semantic search (hint: product codes).

Related tutorials