HNSW filtered KNN silently drops qualifying nearest neighbors after delete/overwrite churn (embedded surrealkv, 3.2.1)
Describe the bug
HNSW filtered KNN (embedding <|K,EF|> $qvec combined with WHERE conditions) silently drops qualifying nearest neighbors that the bare KNN operator returns at the same K/EF — after the index has undergone delete/overwrite churn. In the worst case observed, a filter that qualifies only the true rank-1 nearest neighbor returns an empty result set while the row is present, committed, and returned at rank 1 by the unfiltered operator on the same connection.
The query parses and runs without error — the failure is silent, which is the worst possible mode for retrieval workloads (we hit this in a memory-recall system).
We believe this explains the "cannot reproduce" history of the related reports (#5219, closed as fixed with no linked fix commit; #7314; #6493): the drop requires prior index mutation (deletes/overwrites → element/doc id recycling + compaction) as a precondition. Bulk-write-then-query repros — which is what everyone naturally writes, including our own earlier attempt — never fire it. With churn included, our harness reliably produces silent drops (though the per-run count is nondeterministic — see the correction comment below; it tracks background-compaction timing, not the seed).
Steps to reproduce
Full deterministic harness (Rust, embedded surrealkv, surrealdb = "=3.2.1"): https://github.com/coryzibell/surrealdb-knn-probe — cargo run --release --bin probe writes the full report. Note: the store contents are seed-fixed but the drop count varies run to run with compaction timing — see the correction comment for the accurate reproducibility characterization.
Structure: a SCHEMAFULL table with a 768-dim array<float> field, DEFINE INDEX ... HNSW DIMENSION 768 DIST COSINE TYPE F32, clustered synthetic unit vectors, and an exact in-process cosine ground truth mirroring every write/delete/overwrite. A "strong drop" is counted only when a row (a) passes the filter, (b) is in the exact-cosine top-K, (c) is returned by the bare KNN at the same K=10/EF=40, and (d) is absent from the filtered KNN — i.e. ordinary approximate-recall misses are excluded by construction.
Protocols and results (3,560 filtered queries total, N ∈ 400–1500):
| protocol | filtered queries | strong drops | note |
|---|---|---|---|
| write-only, close/reopen, filtered-query-first | 960 | 0 | matches the "cannot reproduce" history |
| churn, live session (no reopen) | 720 | 1 (rank-1) | 12 rounds × (20% delete + 20% re-insert + 10% embedding overwrite) |
| churn + reopen | 960 | 1 (rank-1) | persists whether or not a bare query runs first |
| narrow filter (qualifies only true rank-1), live | 400 | 0 | |
| narrow filter, churn + reopen | 400 | 7 — all rank-1, all EMPTY result sets | sharpest detector |
All 10 strong drops involve the true nearest neighbor. surrealkv Transaction write conflict retries during churn confirm background compaction fired. Store durability was verified clean throughout (row counts reconcile after every reopen), so this is not data loss — the rows are reachable by the bare operator on the same handle.
Expected behaviour
Filtered KNN returns every qualifying neighbor that the bare KNN returns at the same K/EF. Filtering should only remove non-qualifying rows, never qualifying ones.
Root-cause candidate (from source reading — offered as a lead, not a confirmed diagnosis)
Reading v3.1.3/v3.2.0 (crates/core/src/idx/trees/hnsw/), there is an asymmetry between the two paths:
- The bare path (
layer.rssearch) admits every in-range element to the candidate set unconditionally and resolves element→doc→record after traversal, in batch, falling through to the KV on cache miss. - The filtered path (
search_with_filter→add_if_truthy) resolves element→doc-set during traversal viaVecDocs::get_docs_by_elementand drops any element whose doc-set resolves stale/None— before that element can update thef_distpruning threshold. - The doc-id→record cache (
cache.rsget_doc_id) is generation-gated against compaction, but the element→doc-set cache (cache.rsget_doc_set) — reached only by the filtered path — is not generation-gated; it is invalidated only on element removal. Element ids and doc ids are recycled (theavailablepool /next_element_id), andcheck_state's layer reload rebuilds adjacency but never clears this cache.
A stale element→doc-set entry surviving id recycling would produce exactly the observed asymmetry: bare correct, filtered silently missing the recycled element's row, only after mutation churn.
SurrealDB version
surrealdb = "=3.2.1" (crates.io), embedded surrealkv backend. Originally observed on 3.1.3 in production; reproduced on 3.2.1 with the harness above. Rust SDK, no server.
Contact / follow-up
Happy to run harness variants (different N, churn mixes, EF sweeps, candidate patches); each protocol takes minutes.
Source: surrealdb/surrealdb