Clone
3
S3 Vector Bucket Metadata Filtering
Chris Lu edited this page 2026-11-23 16:04:41 -08:00

S3 Vector Bucket Metadata Filtering

A QueryVectors call can take a filter on the metadata stored with each vector. Only vectors whose metadata matches are candidates for the topK nearest. This page covers the filter syntax, how values match, how a filtered query runs, indexed metadata keys that make filters on chosen keys fast, and what filters cost. For the rest of the API see S3 Vectors API; for setup see S3 Vector Bucket.

s3v.query_vectors(
    vectorBucketName="media", indexName="movies",
    queryVector={"float32": [0.15, 0.85, 0.15]}, topK=10,
    filter={"$and": [{"genre": "scifi"}, {"year": {"$gte": 1978}}]},
    returnDistance=True, returnMetadata=True,
)

A filter only narrows a query. It is not access control: see S3 Vector Bucket Security.

Writing filters

Filter Matches
{"genre": "scifi"} genre equals "scifi", or is an array that holds "scifi"
{"genre": {"$eq": "scifi"}}, {"$ne": ...} equal / not equal; for an array, holds / does not hold the value
{"year": {"$gt": 1977}}, $gte, $lt, $lte numeric comparison; the operand must be a number ($gt expects a number)
{"genre": {"$in": ["drama", "western"]}}, $nin equal to (for an array, holds) one of / none of the values
{"tags": {"$exists": true}} the key is present (or absent, with false)
{"$and": [ ... ]}, {"$or": [ ... ]} all / any of the sub-filters
{"genre": "scifi", "year": 2014} several keys in one object: all must match
  • An unknown operator is rejected (unsupported operator $regex).
  • A filter holds up to 100 constraints, one per comparison on a field: {"year": 1979} is one, {"year": {"$gte": 1970, "$lt": 1980}} two, and $and / $or add none of their own.

How values match

  • Arrays match by their elements. With "tags": ["space", "classic"], {"tags": "space"}, {"tags": {"$eq": "classic"}} and {"tags": {"$in": ["prison", "space"]}} all match. $ne and $nin are their opposites, so {"tags": {"$ne": "space"}} does not match. $gt, $gte, $lt and $lte do not look inside arrays: {"scores": {"$gt": 2}} does not match "scores": [1, 2.5].
  • Types are compared strictly, inside arrays too. 1, "1", true and "true" are four different values: {"scores": 1} matches [1, 2.5] but not ["1", "2.5"], and {"flags": true} matches [true] but not ["true"]. {"a": 7} matches "a": 7 and not "a": "7", and {"a": false} matches only false.
  • A missing key matches no $eq, $in or comparison, and matches every $ne and $nin: {"a": {"$ne": "x"}} and {"a": {"$nin": ["x", 7]}} both return vectors that have no a, including vectors with no metadata at all. {"a": {"$exists": false}} returns only those. A filter on a key that no vector has returns nothing for $eq, and every vector for $ne.
  • null can be stored ("a": null), and GetVectors returns it. Such a vector counts as having the key ($exists: true) and matches $ne and $nin of a string or number. A filter cannot test for null: {"a": null} and {"a": {"$ne": null}} are rejected with unsupported filter operand <nil>.
  • An empty array ("a": []) has the key and holds nothing, so it matches $exists: true, $ne and $nin, and no $eq or $in. {"a": {"$in": []}} matches nothing.
  • Nested objects are not searched. A dotted path such as {"info.lang": "fr"} matches nothing, and {"info": {"lang": "fr"}} is rejected with unsupported operator lang. A vector whose a is an object matches $exists: true and $ne.
  • Non-filterable keys (nonFilterableMetadataKeys on CreateIndex) are stored and returned with returnMetadata, but a filter on one is rejected: metadata key "private" is not filterable.
  • Array matching needs a gateway and a weed-vector of the same release. With an older weed-vector, a number or boolean can also match a string element with the same text.

The cases in this section were checked through the S3 API on master a91def90c0.

How a filtered query runs

weed-vector turns the filter into a predicate on the Lance table's metadata column and hands it to Lance as a prefilter: the set of matching rows is worked out first, and the vector search then only scores rows in that set.

  • Conditions on an indexed key are answered from its scalar index. The rest of this section is about filters on keys without one.
  • The prefilter reads every row's metadata. In Lance's query plan for a 1% filter on 50,000 rows (master 4ac3633f1c), building the set of matching rows read the metadata of all 100 fragments, 3.0 MB, and took 34.5 of the query's 36.5 ms; the vector search took about 1 ms. Through the S3 API on master a91def90c0, a filtered query with the engine's caches off made 3 to 7 times the volume server requests of an unfiltered one (430 against 130 at 50,000 rows, 1,400 against 200 at 200,000), and with the caches warm a filtered query took 50 to 86 ms against 15 ms unfiltered at 50,000 rows, whether 10% or 1% of the rows matched.
  • Without a vector index the search is exact: every matching row is scored.
  • With a vector index (IVF) the search scores matching rows only in the partitions it probes (see S3 Vector Bucket#how-it-works for how many). Unfiltered, this is why a topK near the size of the index returns fewer vectors than exist: 4,000 vectors in 63 partitions with topK 4000 returned 1,924 to 2,071.
  • A short filtered answer falls back to an exact scan (#12802). When a filtered query through the index returns fewer than topK vectors, or a vector without a finite distance, the engine runs it again as an exact scan of the data the index search covered, which on a single engine is the whole index. On the 4,000-vector index above, a filter that 400 vectors matched returned all 400 for topK 500, and a filter that 5 matched returned all 5 with finite distances for topK 10, on 5 of 5 queries each. The scan costs another pass over the table: with the caches off, a query whose filter matched 5 rows or none made about twice the volume server requests of a 1% filter (833 against 430 at 50,000 rows, 3,000 against 1,400 at 200,000).

Recall at low match counts

The fallback only runs when the answer is short. When the probed partitions hold topK matching rows, those are returned even if better matches sit in partitions that were not probed, so recall can drop below 1 when few rows match. Measured through the S3 API on master a91def90c0, 128 dimensions, cosine, topK 10, 50 queries, recall@10 against an exact search over the matching rows:

Rows Matching rows Recall@10 (worst query) Results per query
50,000 5,000 (10%) 1.000 (1.0) 10
50,000 500 (1%) 0.992 (0.8 to 0.9) 10
50,000 5 1.000 (1.0) 5
200,000 20,000 (10%) 1.000 (1.0) 10
200,000 2,000 (1%) 1.000 (1.0) 10
200,000 20 (0.01%) 0.968 to 0.972 (0.7 to 0.8) 10
200,000 5 1.000 (1.0) 5
50,000 and 200,000 0 0

A filter on an indexed key that matches at most 256 rows is scored exactly, so its recall is 1.

Before #12802, a 20-row filter at 200,000 rows returned 7.8 results out of 10 on average, with recall 0.73, in a local benchmark of the engine's query code on master 4ac3633f1c.

Indexed metadata keys

A metadata filter normally reads every row's metadata and evaluates the JSON for each one. An indexed metadata key keeps a scalar index of the string values a filter compares, so a filter on that key reads the index instead. With the default search mode, indexing a metadata key changes how the filter is evaluated and leaves uncovered rows eligible for the query.

When to index a key

Index a key that your queries filter on with strings, for example {"author": "alice"} or {"genre": {"$in": ["drama", "comedy"]}}, and that you query often. The admin panel lists the keys an index's queries filtered on most in the last hour that are not indexed, with how many queries per hour and their average latency. An engine counts a limited number of keys per index; when it has stopped counting new ones, the panel says the list is partial.

Adding and removing a key

In the admin UI, open the vector bucket: each index has a Metadata indexes panel. + Index a key asks for the key name; a listed key's Index button fills it in. Remove stops indexing a ready key, Cancel a requested or building one.

From weed shell:

s3vectors.index.metadata -bucket embeddings -index articles
s3vectors.index.metadata -bucket embeddings -index articles -add author
s3vectors.index.metadata -bucket embeddings -index articles -remove author

Without -add or -remove the command lists each key with its state, the share of the index's rows its index covers, its segments and its size.

Adding is refused for a key the index declares non-filterable, for a ninth key (an index has at most 8 indexed keys), and while any registered vector engine is too old to fill indexed key columns on its writes.

States

State Meaning
requested Recorded on the index's table; the lance_metadata_index worker job has not added the key yet.
building The key's column exists and every write fills it; its index is being built. Queries do not use it yet.
ready Queries use the key's index.
off Being removed. Queries stopped using it at once; the job drops the index and the column.

A key stays building for at least 3 minutes after its column is added. The job waits that long so that every PutVectors that opened the table before the add has committed, then derives the key's values again for the rows written meanwhile, and only then marks the key ready.

The admin dispatches the job when you add or remove a key from the panel and a worker can run it. A key added with the shell is picked up by the job's detection. The job types start disabled, see Lance Maintenance Worker.

Writes while a key is added

So that the wait above is enough, a PutVectors call must commit within 120 s of the engine opening the index's table, whether or not any key is being added. A call that takes longer fails with InternalFailure (HTTP 500); since PutVectors overwrites by key, sending it again is safe.

What an indexed key covers

Filter on a ready key Uses the index
a string: {"author": "alice"}, {"author": {"$eq": "alice"}} yes
$in of strings: {"author": {"$in": ["alice", "bob"]}} yes
either, ANDed with other conditions yes; the other conditions still apply to the rows the index finds
numbers, booleans, $in mixing strings with other types no
$ne, $nin, $exists no
inside $or no

A string value matches a stored string and the string elements of a stored array, exactly as without the index. Filters the index does not cover read the metadata as before.

Filtering in indexed-only mode

Indexed-only queries consider only fragments covered by the vector index and by every metadata index used by the filter. Each condition must be string equality (a bare string or $eq) or $in with string values, and each key must have a ready metadata index. Combine conditions with AND. For example, with author and genre both ready:

{"$and": [{"author": "alice"}, {"genre": {"$in": ["drama", "comedy"]}}]}

An unsupported condition or a key whose index is missing or still building returns ValidationException. A ready key can still have uncovered fragments. lance_metadata_index expands metadata coverage and lance_optimize_indices expands vector coverage. Candidate counting and exact scoring stay within the fragments covered by both.

Exact scoring of few matches

Before searching, the engine counts the rows the indexed conditions match, through the index. When there are at most --exact-match-limit of them (default 256; set with the engine flag or WEED_VECTOR_EXACT_MATCH_LIMIT, 0 turns it off), it scores those rows exactly instead of searching the vector index, so a selective filter gets the exact top-k. Above the limit it searches the vector index with the indexed rows as the prefilter. Counting stops as soon as the count passes the limit.

Segments and the size cap

A key's index is kept as segments ordered by time: the newest one, the head, takes new rows. Once rows written since the last update reach min_unindexed_rows (default 50,000), the job folds them into the head, or starts a new segment once the head holds segment_rows (default 1,000,000). Default queries can still find rows outside the segments by reading the key's column. Indexed-only queries skip those rows. Compaction keeps each segment's rows apart. Both settings are on the job's form in the admin UI.

Storage cost

Each indexed key adds a hidden list column and its index to the index's table. The index measured about 2.6 bytes per row with 1,000 distinct values (2.7 MB for 1M rows).

Performance

Median latency of a filtered query through the S3 API on master a91def90c0, single engine, Go volume server, 128 dimensions, cosine, IVF index, topK 10, second of two rounds, on a shared host (load average 14 to 37 in these rounds). S3 Vector Bucket Performance#filtered-queries has the full tables, the first rounds, the commands and the setup.

Filter 50k, warm 50k, caches off 200k, warm 200k, caches off
none 15 ms 86 to 106 ms 38 to 40 ms 79 to 113 ms
10% of rows 86 ms 119 to 122 ms 235 to 432 ms 258 to 295 ms
1% 50 to 53 ms 82 to 86 ms 211 to 240 ms 309 to 452 ms
0.01% (20 rows) 425 to 532 ms 455 to 550 ms
5 rows 96 to 243 ms 136 to 359 ms 432 to 451 ms 413 to 434 ms
no rows 156 to 167 ms 139 to 171 ms 400 to 442 ms 464 to 472 ms

The ranges cover the two placements of the matching rows, scattered over the index or written together; neither was consistently faster. At 50,000 rows the 0.01% author has 5 rows and is in the "5 rows" line.

These are filters on a key without an index. In a separate run at 50,000 rows with the key indexed, filters matching 10% of the rows down to 5 rows took 12 to 23 ms, and recall@10 was 1.000 up to 256 matching rows: see S3 Vector Bucket Performance#with-indexed-metadata-keys.

Limits

The limits on metadata size, filterable metadata, nonFilterableMetadataKeys and filter constraints are in S3 Vector Bucket#limits. An index has at most 8 indexed metadata keys, each holding string values.