Your first search
Start a server, store three help-centre articles and retrieve their titles.
Skeg 0.8 · Development documentation
You will start a local server, store three documents and search for the two closest to a query. By the end, you will have retrieved document titles from Skeg using the IDs returned by vector search.
Use a Skeg 0.8 executable from the installation guide,
Python 3 with venv support, and two terminals. Check the executable with
skeg --version before starting.
1. Start a local server
In the first terminal, create a temporary data directory and start Skeg. Keep this terminal open while running the example.
learn_data=$(mktemp -d)
printf 'Exercise data: %s\n' "$learn_data"
skeg --data-dir "$learn_data" --addr 127.0.0.1:6379 --protocol resp3
The startup output should show the loopback address and zero indexes.
The address 127.0.0.1 keeps the exercise local to your machine. To accept
connections from other machines, first configure
authentication and follow the
deployment guide.
2. Prepare a Python client
Open another terminal and use a fresh working directory for the Python script and its virtual environment:
learn_work=$(mktemp -d) &&
cd "$learn_work" &&
python3 -m venv .venv &&
. .venv/bin/activate &&
python -m pip install 'redis>=5'
The Python redis package sends commands using the RESP protocol supported by
Skeg. You are connecting to the Skeg process started above.
3. Give the documents IDs and vectors
Imagine a help centre with these articles:
| ID | Title | Vector |
|---|---|---|
| 1 | Reset your password | (1, 0, 0, 0) |
| 2 | Download an invoice | (0, 1, 0, 0) |
| 3 | Recover account access | (0.8, 0.6, 0, 0) |
In a real application an embedding model turns document text into vectors. Here we assign the coordinates ourselves so you can follow the result without installing a model. All vectors and queries must have the same number of coordinates as the index: four in this exercise.
Save the following as first_search.py, or download
the complete script:
import os
import struct
import redis
port = int(os.environ.get("SKEG_LEARN_PORT", "6379"))
client = redis.Redis(
host="127.0.0.1", port=port, protocol=3, decode_responses=False
)
index = "learn_search"
def vector(values):
return struct.pack("<" + "f" * len(values), *values)
client.execute_command(
"SKEG.VINDEX.CREATE", index, 4, "KIND", "tq2", "BACKEND", "disk",
"PERSISTENCE", "durable", "IFNOTEXISTS",
)
articles = [
(1, "Reset your password", [1.0, 0.0, 0.0, 0.0]),
(2, "Download an invoice", [0.0, 1.0, 0.0, 0.0]),
(3, "Recover account access", [0.8, 0.6, 0.0, 0.0]),
]
for row_id, title, coordinates in articles:
client.set(f"article:{row_id}:title", title)
client.execute_command("SKEG.VSET", index, row_id, vector(coordinates))
query = vector([1.0, 0.0, 0.0, 0.0])
hits = client.execute_command("SKEG.VSEARCH", index, 2, 100, query)
for offset in range(0, len(hits), 2):
row_id, score = hits[offset:offset + 2]
title = client.get(f"article:{int(row_id)}:title")
print(f"{int(row_id)} {float(score):.3f} {title.decode('utf-8')}")
The script stores two things for each article. SET stores its title under a
key such as article:1:title. SKEG.VSET stores its vector under the numeric
ID 1 in the learn_search index. The shared ID lets your application fetch
the title after a search.
The vector helper encodes the coordinates as little-endian 32-bit floats,
the byte format the command accepts. tq2 selects the compact search
representation and disk selects the disk-backed index. PERSISTENCE durable
keeps the index across stops. See the other creation options in the
API reference.
4. Run the search and read the result
Run the script:
python first_search.py
Each printed line contains a row ID, its similarity score and its article title. The search response contains ID–score pairs. The final loop fetches each title with a key-value lookup. Scores are printed to three decimal places.
1 1.000 Reset your password
3 0.800 Recover account access
The query points along the first coordinate. For the vectors we chose, the exact cosine scores are:
| Article | Exact score |
|---|---|
| Reset your password | 1 |
| Recover account access | 0.8 |
| Download an invoice | 0 |
The expected result is row 1, Reset your password, followed by row 3, Recover account access. How vector search works walks through these numbers and explains why an approximate index can miss a neighbour.
In the command, 2 asks for at most two results. 100 controls the breadth of
graph search. They are different choices: asking for more results and spending
more work finding them are separate operations.
5. Change the query
Change the query line to:
query = vector([0.0, 1.0, 0.0, 0.0])
This points along the second coordinate. The invoice article now has the highest exact score, followed by the account-access article.
You can rerun the full script with the changed query. IFNOTEXISTS accepts the
existing index only when its configuration matches, and writing the same row
IDs replaces those rows. An application normally creates its index once and
then issues many searches. See index management for
configuration mismatches and index identity.
If something goes wrong
| Symptom | What to check |
|---|---|
| Connection refused | Keep the server running in the first terminal and check port 6379. |
| Address already in use | Another process owns that port. Change the server address to 127.0.0.1:6380 and run SKEG_LEARN_PORT=6380 python first_search.py. |
| Index configuration mismatch | Use a fresh exercise directory or inspect the existing index; IFNOTEXISTS does not change its settings. |
| Dimension error | Each stored vector and query must have four coordinates. |
| Authentication required | You connected to a configured server rather than the local unauthenticated example. Follow its login procedure. |
Finish the exercise
Stop the server with Ctrl+C in the first terminal. To repeat the exercise with the saved rows, start Skeg again with the same data directory. To discard the exercise, remove only the temporary directory printed at startup after the server has stopped. The Python script and virtual environment are in the separate working directory.
Next, read search requests and results, then add payloads and filters to request only eligible articles. When evaluating a larger dataset, choose search effort to measure the balance between missing neighbours and spending more time searching.