SKEG · GUIDES

Search requests and results

Supply a query vector, interpret scores and retrieve application records.

Skeg 0.8 · Development documentation

A vector search takes a query embedding and returns nearby row IDs with scores. Use the IDs to retrieve the application records behind the hits. The example below continues your first search.

flowchart TD
    accTitle: From a query embedding to an article title
    accDescr: The application sends an embedding to Skeg, then uses the returned row IDs in separate GET requests for the titles.
    A["Application sends an embedding"] --> B["SKEG.VSEARCH returns IDs and scores"]
    B --> C["GET reads the title by row ID"]

In this example, a hit with ID 3 leads to GET article:3:title. Request WITHPAYLOAD when the search reply should also include the payload stored on each vector row.

Prepare a query

Use the same embedding model and preprocessing as the stored documents. Match the index dimension, and encode coordinates as little-endian float32 values. Vectors must have finite coordinates and a finite squared norm in float32. Use meaningful nonzero embeddings for cosine search.

For text queries, generate the embedding in your application before calling VSEARCH. Follow search text with local embeddings for an example.

Choose result count and search effort

SKEG.VSEARCH <index> <k> <l_search> <query>

k is the maximum number of hits to return. l_search controls graph-search effort. Zero uses the disk index’s default. Read SKEG.VINDEX.INFO for that default and SKEG.CAPABILITIES for supported limits. Oversized search requests for k or effort are clamped to those limits.

Skeg can return fewer than k hits. The index may contain fewer rows, a filter may leave fewer eligible rows, or approximate candidate selection may omit a neighbour. A request with k=0 returns no hits and still requires an existing index and a valid query vector.

For flat search and filtered plans that do not walk a graph, increasing l_search does not imply a broader scan. Inspect EXPLAIN and follow search effort when tuning a workload.

Decode pairs and payload triples

The default result is a flat array:

[id, score, id, score, ...]

WITHPAYLOAD changes the stride to three:

[id, score, payload, id, score, payload, ...]

The ID is a decimal unsigned integer carried as a bulk string. Scores are doubles on RESP3 and byte strings on RESP2. Convert them according to your client’s response types. A missing payload is null; a present-empty payload is an empty byte string. An empty result is an empty array.

For example, this illustrative response contains two hits:

["1", 1.0, "topic=account", "3", 0.8, "topic=account"]

The scores come from the hand-calculated cosine example and diagram. The payloads are illustrative. In Python with binary responses, IDs and payloads are byte strings.

Interpret scores

Search returns cosine similarity scores in descending order. Larger means closer in the embedding space. Quantized representations guide candidate selection; returned candidates are scored with stored float32 vectors.

Candidate selection can omit a closer neighbour before scoring. Read the score as cosine similarity in the chosen embedding space. If scores tie, use the row IDs to identify the hits because their order can vary.

If your application needs a minimum score, evaluate the cutoff on labelled queries and apply it to returned hits. The raw VSEARCH syntax does not offer a score-threshold argument. Removing low-scoring hits after retrieval can leave fewer than k results.

Retrieve application records

In the first-search tutorial, a title is stored at article:<id>:title. Reuse that convention to fetch content from hit IDs. This snippet continues that tutorial with its existing client, index and encoded query variables:

hits = client.execute_command("SKEG.VSEARCH", index, 2, 100, query)
if len(hits) % 2:
    raise RuntimeError("Unexpected search response shape")

for offset in range(0, len(hits), 2):
    row_id = int(hits[offset])
    score = float(hits[offset + 1])
    title = client.get(f"article:{row_id}:title")
    if title is None:
        print(row_id, score, "title missing; reconcile the application record")
    else:
        print(row_id, score, title.decode("utf-8"))

Search and KV reads are separate operations, so a title can change or disappear between them. Handle missing or changed titles according to your application’s update policy. See the data model and write outcomes.

Restrict eligible rows

Add FILTER with the entire expression as one argument:

SKEG.VSEARCH articles 10 100 <query-bytes> FILTER "lang = it AND year >= 2024"

The server uses the filter to determine eligibility before selecting hits. Filtering ten results in the application can only remove rows from those ten.

FILTER does not implicitly enable WITHPAYLOAD. Follow payloads and filters for a complete example, and the filter reference for types, missing fields and negation.

Inspect a query

EXPLAIN changes the outer response to [hits, profile_text]:

hits, profile = client.execute_command(
    "SKEG.VSEARCH", index, 2, 100, query, "EXPLAIN",
)
print(profile.decode("utf-8"))

The hit format still depends on WITHPAYLOAD. The profile describes this query and can change its wording across releases. Read it as diagnostic text. See query diagnostics.

Understand result boundaries

VSEARCH has no cursor or offset argument for walking the whole index. Repeating a query, changing effort or increasing k is not an export protocol. Use VGET for a known row ID and retain source IDs for your application’s inventory. Concurrent writes and maintenance can change results between calls.

If useful hits are missing, start with the search troubleshooting procedure. If a request is refused or its response is lost, use write outcomes for connection and retry handling, even when the failed operation is a read.