fix: search termination and result set bounded by k instead of efSearch#22
Open
suykerbuyk wants to merge 3 commits into
Open
fix: search termination and result set bounded by k instead of efSearch#22suykerbuyk wants to merge 3 commits into
suykerbuyk wants to merge 3 commits into
Conversation
added 3 commits
April 11, 2026 12:26
In a binary min-heap, the last array element is not necessarily the maximum. Max() must scan the leaf nodes (indices n/2..n-1) to find the true maximum. PopLast() used Remove(Len()-1) which removed an arbitrary element instead of the worst. This caused incorrect evictions during neighbor selection and search result trimming, degrading graph quality.
…ance replenish() is called after neighbor eviction and node deletion to restore connectivity. It unconditionally used CosineDistance, ignoring the graph's configured distance function. This corrupted the graph topology for any non-Cosine metric. Thread DistanceFunc through replenish() and isolate() so the correct distance function is always used.
Two issues in layerNode.search(): 1. Termination was based on whether any neighbor improved result.Min() (the best result). Per HNSW Algorithm 5, termination should occur when the best remaining candidate is farther than result.Max() (the worst result). The old condition stopped exploration too early. 2. The result set was bounded by k (number of results to return) instead of efSearch (the exploration beam width). With k=3 and efSearch=100, only 3 candidates were tracked, causing the distance threshold to converge to a local minimum within a few iterations. Fix: bound the result set by efSearch during exploration, use the correct HNSW termination condition, and trim to k after search completes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two issues in
layerNode.search():Termination too aggressive — stopped when no neighbor beat
result.Min()(best result). Per HNSW Algorithm 5, should stop when best candidate >result.Max()(worst result). Explored too little of the graph.Result set bounded by k, not efSearch — with k=3 and efSearch=100, only 3 candidates were tracked, causing the distance threshold to converge to a local minimum within a few iterations.
Fix: bound the result set by
efSearchduring exploration, use the correct HNSW termination condition, and trim tokafter search completes.Depends on #20 and #21.
Test plan
TestGraph_SearchFindsCorrectNearest— verifies search returns true nearest neighbors with small kgo test ./...andgo vet ./...pass