How to Run Local Embeddings with Ollama for Search

Why keyword search is not enough

Keyword search only finds the words you typed. If your note says "refund window" and you search for "how do I get my money back," a keyword index shrugs. Semantic search compares meaning, not spelling, and powers most modern document search and retrieval-augmented generation (RAG).

You do not need a cloud API to try it. With Ollama's embeddings support, a laptop turns text into searchable vectors in minutes, entirely offline — from first pull to working Python search.



Source: Weaviate documentation — query, vector database, and Ollama embedding flow.

What an embedding actually is

An embedding is a list of numbers — a vector — representing a text's meaning. Ollama's documentation puts it plainly: embeddings turn text into vectors you can store, search with cosine similarity, or use in RAG pipelines, typically 384 to 1,024 dimensions depending on the model.

Texts with similar meanings get vectors pointing in similar directions. Once every note is a vector, a query becomes one too, and "closest vectors win" becomes a search engine.

Two rules matter most: use cosine similarity, and use the same model for indexing and querying — vectors from different models cannot be compared.



Source: Memgraph — documents transformed into embedding vectors and plotted in vector space.

Prerequisites

  • Ollama installed and running on your machine. If you have not set it up yet, start with our guide to running an open model locally with Ollama in 20 minutes.
  • Python 3.10 or later, plus the requests library (pip install requests).
  • About 300 MB of free disk space for the default model, and a CPU-only machine is fine — embedding models are small.

Step 1: Pull an embedding model

Chat models and embedding models are different jobs, so Ollama keeps them separate. The sensible default is nomic-embed-text: a 137-million-parameter model that produces 768-dimensional vectors from a roughly 274 MB download, according to independent Ollama embedding comparisons. Pull it with one command:

ollama pull nomic-embed-text
ollama list

The second command confirms the model is on disk. Alternatives include bge-m3 (about 1.2 GB, 1,024 dimensions, broader language coverage) and mxbai-embed-large (about 669 MB, 1,024 dimensions). Start with nomic; you can re-embed later, but you cannot mix models in one index.

Step 2: Generate your first embedding with curl

Ollama's current endpoint is POST /api/embed. It takes a model name and an input field holding either one string or an array of strings, and returns an embeddings array in the same order, as documented in the official /api/embed reference. Try it:

curl http://localhost:11434/api/embed -d '{
  "model": "nomic-embed-text",
  "input": "Ollama runs open models locally on your machine."
}'

The response holds one 768-number vector in embeddings, plus timing fields. Batch inputs when you can — one call with an array of chunks beats one request per chunk.

Note two fields: truncate defaults to true (long inputs are silently cut; set it to false to get an error instead), and dimensions requests shorter vectors on models trained for it.

Step 3: Build a tiny semantic search in Python

The script below embeds documents, embeds your question with the same model, and ranks by cosine similarity. Save it as local_search.py:

import math
import requests

OLLAMA_URL = "http://localhost:11434/api/embed"
MODEL = "nomic-embed-text"

DOCS = [
    "To request a refund, open Settings, choose Billing, and submit the form within 30 days of purchase.",
    "Ollama serves local models on http://localhost:11434 with a REST API.",
    "Back up your notes every night; the backup folder is synced to an external drive.",
]

def embed(texts):
    r = requests.post(OLLAMA_URL, json={"model": MODEL, "input": texts})
    r.raise_for_status()
    return r.json()["embeddings"]

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    na = math.sqrt(sum(x * x for x in a))
    nb = math.sqrt(sum(y * y for y in b))
    return dot / (na * nb)

doc_vectors = embed(DOCS)
query = input("Ask something: ")
query_vector = embed([query])[0]

ranked = sorted(
    zip(DOCS, [cosine(query_vector, v) for v in doc_vectors]),
    key=lambda item: item[1],
    reverse=True,
)
for text, score in ranked:
    print(f"{score:.3f}  {text}")

Run it with python local_search.py and ask "how do I get my money back?" The refund document should rank first despite sharing almost no words with your question.

The script needs no NumPy, no vector database, and no API key. For a few thousand notes, in-memory ranking is plenty; when you outgrow it, our guide to building a local RAG system with Ollama and Qdrant is the next step.

Step 4: Chunk your documents properly

Embedding quality is mostly chunking quality: a whole manual as one vector is a blurry average. Rules that work well with nomic-embed-text:

  • Aim for 200 to 400 tokens per chunk — a paragraph or two — with small overlap across boundaries.
  • Keep headings with their chunk; text that starts mid-thought embeds badly.
  • Store the source file and chunk position with each vector, so results link back to the original.
  • Re-embed everything if you switch models; a mixed index returns nonsense with great confidence.

Troubleshooting

  • Connection refused on port 11434: Ollama is not running — start it with ollama serve or the desktop app.
  • Model not found: you skipped the pull step, or mistyped the name. Run ollama pull nomic-embed-text and check ollama list.
  • Empty or identical scores: check you used the same model for query and documents, and read embeddings (plural) from /api/embed — the older /api/embeddings endpoint used a different shape.
  • Results feel random: your chunks are probably too long, or your test corpus too small. Test on real notes before switching models.

Where to go next

Once search works, natural upgrades are a persistent vector database, a small web UI, and then a chat model answering from retrieved chunks — that last step is RAG. For a custom model wrapper, see our guide to creating a custom Ollama model with a Modelfile. Semantic search used to mean a cloud bill and a data-sharing decision; now it means one 274 MB model, one endpoint, and about 40 lines of Python.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes