SKEG · GUIDES

Payloads and filters

Store metadata, select eligible rows and understand the filtered response.

Skeg 0.8 · Development documentation

Find the two nearest Italian documents published from 2024 onward. Store each document’s language and year in its vector row’s payload, then send a filter with the search request.

Choose the payload fields

Write searchable metadata as UTF-8 key=value tokens separated by whitespace. For example, lang=it year=2025 tag=guide stores a language keyword, an integer year and a tag keyword. The script sends these bytes as the PAYLOAD argument to SKEG.VSET.

The exercise uses four documents. The cosine values below come from the example vectors and query.

RowPayloadCosine similarity
1lang=en year=2025 tag=guide1.00
2lang=it year=2023 tag=guide0.96
3lang=it year=2025 tag=guide0.80
4lang=it year=2024 tag=api0.60

Send lang = it AND year >= 2024 as one FILTER argument. It makes rows 3 and 4 eligible. An unfiltered search for two hits returns rows 1 and 2 in this exercise. Removing those hits in the application leaves no results.

flowchart TD
    accTitle: Filter documents before selecting hits
    accDescr: The language and year filter makes rows 3 and 4 eligible. Skeg returns row 3 before row 4.
    A["Rows 1, 2, 3 and 4"] -->|Apply language and year filter| B["Eligible rows 3 and 4"]
    B -->|Rank by similarity| C["Hits 3, then 4"]

Run the example

Install Skeg 0.8, and have Python 3.10 or later available. Start Skeg on a free local port 6383 with a new data directory.

data_dir=$(mktemp -d) &&
  skeg --protocol resp3 --data-dir "$data_dir" --addr 127.0.0.1:6383

Download filter_example.py into your working directory. In a second terminal, install the Python client in a virtual environment and run the script.

python3 -m venv .venv &&
  . .venv/bin/activate &&
  python -m pip install 'redis>=5' &&
  python filter_example.py

The script creates a durable index, writes the four rows and checks the unfiltered and filtered searches. This is the filtered request from the script.

expression = "lang = it AND year >= 2024"
hits = client.execute_command(
    "SKEG.VSEARCH", index, 2, 100, query,
    "WITHPAYLOAD", "FILTER", expression,
)

Expect rows 3 and 4 in that order, with the payloads shown in the table. FILTER selects eligible rows. WITHPAYLOAD adds the original payload to each hit. Either option can be used independently. See search reply formats for how to decode IDs, scores and payloads.

Adapt the filter

Use consistent field types across rows. Store years as integers to use numeric ranges. Use a keyword such as id-007 when an identifier’s leading zeroes matter. See payload encoding and types for empty and repeated fields and the handling of arbitrary bytes.

To findExpression
Italian or English documentslang IN (it, en)
Documents published in 2024 or 2025year BETWEEN 2024 AND 2025
Italian guides or API documentslang = it AND (tag = guide OR tag = api)
Indexed rows with no source fieldNOT source EXISTS

The last expression considers only rows with at least one indexed field. For example, it includes a row with lang=it and no source. When the application needs to query the state of an attribute across all documents, store an explicit state field on every row. See negation and rows without indexed fields.

Diagnose unexpected results

Read the stored payload for a missing expected ID and compare it with the expression the application sent. Check UTF-8 encoding, field names, value types and case before adjusting search effort. For the same query and filter, add EXPLAIN to inspect the search plan and candidate work. On an approximate plan, the returned hits can cover only a subset of the eligible rows.

See check filter eligibility for point reads and payload-index diagnostics, and understand a query for interpreting the profile.

Remove the example index

With redis-cli installed, drop the index on the exercise server.

redis-cli -3 -h 127.0.0.1 -p 6383 SKEG.VINDEX.DROP learn_filters

Expect OK. You can now run the script again against the same server. When finished, stop Skeg with Ctrl+C and remove the temporary data directory if you no longer need it.