Search short notes by meaning with Ollama and EmbeddingGemma
Rank three fictional notes by how closely they relate to a question and save the ranking as JSON.
Last updated
Menus and supported features can change. Compare the source-check date below with your current app version.

You write “the courier brought the parcel after the promised date”, then search your notes for “shipment was late”. A word search may miss the connection. In this exercise you will rank three fictional notes by how closely their meaning relates to a short query and save the list as matches.json.
An embedding is a list of numbers representing a text input. That representation gives you a way to compare a query with notes even when their words differ. Use it to find what to read first; it does not write an answer or prove that a note is correct. The steps follow the official Ollama documentation. I checked the sorting and file-writing code with simulated numbers, not the model’s actual scores or ranking. The cover illustrates the workflow.
1. Download a local embedding model
Install and open Ollama. In PowerShell or Terminal, run ollama --version to check the installation, followed by ollama pull embeddinggemma:300m. The developer’s model listing calls for Ollama 0.11.10 or later. The first download needs internet access and disk space. The endpoint in the exercise is localhost, which means this computer. You do not need to install a separate chat model.
2. Save the practice script
Install Python and save this as find_notes.py in a fresh folder. Use UTF-8 and make sure the editor has not appended .txt. The three fictional notes are about a printer, a delayed parcel, and snacks. Keep them unchanged for your first pass so you know which results you are comparing.
import json
from math import sqrt
from pathlib import Path
from urllib.request import Request, urlopen
notes = [
("print", "The office printer needs new toner."),
("delivery", "The courier brought the parcel after the promised date."),
("snacks", "Buy snacks for Friday's team meeting.")
]
question = "My shipment was late."
inputs = [question] + [note for _, note in notes]
request = Request(
"http://localhost:11434/api/embed",
data=json.dumps({"model": "embeddinggemma:300m", "input": inputs}).encode(),
headers={"Content-Type": "application/json"}
)
with urlopen(request, timeout=180) as response:
vectors = json.load(response)["embeddings"]
if len(vectors) != len(inputs):
raise ValueError("Unexpected number of vectors")
def cosine(a, b):
if len(a) != len(b) or not a:
raise ValueError("Vector size mismatch")
norm = sqrt(sum(x*x for x in a) * sum(y*y for y in b))
if not norm:
raise ValueError("Empty vector direction")
return sum(x*y for x,y in zip(a,b)) / norm
matches = [
{"id": key, "note": note, "score": cosine(vectors[0], vector)}
for (key, note), vector in zip(notes, vectors[1:])
]
matches.sort(key=lambda row: row["score"], reverse=True)
Path("matches.json").write_text(
json.dumps({"question": question, "matches": matches}, indent=2) + "\n",
encoding="utf-8"
)
print("Saved matches.json")
3. Run it and read the top result
Change to the practice folder with cd "path to the folder". Run py -3 find_notes.py on Windows or python3 find_notes.py on macOS or Linux. The model returns an embedding for the query and for each note. The script compares their directions using cosine similarity and sorts higher scores first. A value of 0.8 does not mean an 80 percent chance that a result is correct.
Open matches.json in your text editor. With “My shipment was late”, the reference expectation is that delivery is the most relevant of the three. This is a target for your review, not a measured ranking. Change the query to We need more toner. and rerun it. Check whether print moves up. Save the first result under a different name if you want both lists side by side.
Troubleshooting
If the connection is refused, make sure the local Ollama service is running. If it cannot find the model, check the exact tag with ollama list. When the ranking looks wrong, read the original notes and try a less ambiguous query. Search exact identifiers separately: a note related to the wrong order number is still the wrong note. To extract fixed fields from a sentence instead of ranking notes, see the Ollama ticket JSON guide.