Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Range-search batching can omit closer in-range neighbors or return unsorted results, and uncaught worker exceptions can terminate the process instead of returning a C API error.
5 open issues
Sort range-search candidates before applying radius and result limits · New Propagate worker exceptions instead of terminating the process · New Include vectors exactly on the range-search boundary · New Guard null result pointers when reading empty rows · New Make reserve preserve results if allocation fails · New
What changed in this PR
Adds approximate range search to the C API, allowing C and FFI consumers to query Vamana indexes by a metric-specific threshold rather than approximating range queries with large top-K requests.
Changes:
- Adds range search with optional ID filtering and per-query limits across static and dynamic indexes.
- Centralizes search-result buffer management for top-K and range search.
- Documents the API and adds coverage for metrics, buffers, filtering, limits, and synchronization modes.
| File | Description |
|---|---|
bindings/c/tests/CMakeLists.txt |
Registers the new search-results tests. |
bindings/c/tests/c_api_search_results.cpp |
Tests result buffers and range-search behavior. |
bindings/c/tests/c_api_dynamic_index_sync.cpp |
Exercises range search in synchronization workloads. |
bindings/c/src/svs_c.cpp |
Exposes range search and adopts shared result handling. |
bindings/c/src/search_results.hpp |
Adds the internal result-buffer wrapper. |
bindings/c/src/index.hpp |
Adds the virtual range-search interface. |
bindings/c/src/index_vamana.hpp |
Declares Vamana implementations and shared sampling helper. |
bindings/c/src/index_vamana.cpp |
Implements static and dynamic range-search dispatch. |
bindings/c/src/filtered_search.hpp |
Collects range results through batch iterators. |
bindings/c/README.md |
Lists range search among supported operations. |
bindings/c/include/svs/c/svs_c.h |
Declares and documents the public API. |
bindings/c/docs/C_API_Design.md |
Adds range-search usage and contract details. |
bindings/c/CMakeLists.txt |
Registers the result-wrapper header. |
Review effort: Balanced
| svs::threads::parallel_for( | ||
| index.get_threadpool_handle(), | ||
| svs::threads::StaticPartition{queries.size()}, | ||
| search_closure | ||
| ); |
f3fd772 to
630c422
Compare
| do { | ||
| iterator.next(batch_size); | ||
| for (const auto& neighbor : iterator.results()) { | ||
| // Batches are ordered by distance: the first miss ends the range. |
There was a problem hiding this comment.
Each batch is sorted, but a later batch can return closer points, so stopping when a candidate not in range might miss points in later batches. The impact could be larger for range search, current design batch size = search window size, so a radius holds more points than window needs many batches. Maybe have the same stop rule as in #472, which my understanding is look at closest points to expand out of radius instead of iterator result and sort each row before write in result
There was a problem hiding this comment.
Unfortunately, we cannot apply #472 rule here, because iterator's search buffer is private.
Making changes in the code SVS code is out of scope for this PR.
The only we can do - try to call iterator.next() one more time to ensure if there are no more closer neigbors in next batch, but one more iteration negatively affects performance.
Anyway, this PR is made to implement functionality for range searching using batch iterator and is going to be replaced by direct VamanaIndex::range_search call soon.
P.S. The search logic has been taken from svs::runtime::VamanaIndexImpl::range_search() defined in /bindings/cpp/src/vamana_index_impl.h
|
|
||
| // A zero estimate means the sampled hit rate is below the filter rate. | ||
| if (id_filter != nullptr && | ||
| estimate_initial_batch_size( |
There was a problem hiding this comment.
Since we are fixing batch size to be search window size in range search, pre sampling is not needed, it is only used now to get batch size adaptively
630c422 to
20c7f59
Compare
7de931d to
382cde6
Compare
yuejiaointel
left a comment
There was a problem hiding this comment.
LGTM, just some minor doc change
382cde6 to
2b66cb4
Compare
Co-authored-by: yuejiaointel <108152493+yuejiaointel@users.noreply.github.com>
b109144 to
77aba3d
Compare


[C API] Add range search with optional ID filter and result limit (using batch iterator)
Summary
Adds
svs_index_search_range()to the C API. It returns, for each query, the neighbors strictly closer than a radius, with an optional ID filter and an optional per-query result limit. It works for static Vamana indexes and for dynamic indexes with every sync kind (NONE,GLOBAL,FINE_GRAIN).This PR also wraps
svs_search_results_tmanagement into an internalSearchResultsclass that top-K search and range search share.API
< radius.> radius.svs_search_results_row().max_resultscaps each row;0means no limit. A capped row holds the closest in-range neighbors, and the search for that query stops once the row is full.search_paramsmay be NULL, in which case the index defaults are used. The search window size also sets the batch size used to walk the graph.id_filterworks the same way as insvs_index_search_topk(). Iffilter_rate > 0and the observed hit rate is lower, the affected rows come back empty: every row when the pre-search sampling detects it, otherwise only the row being searched.SVS_ERROR_INVALID_ARGUMENTfor bad arguments (num_queries == 0, NULL handles or pointers, an incompatible results struct). It is also returned when caller-owned buffers are too small; in that caseout_resultsis left unchanged.SVS_ERROR_NOT_IMPLEMENTEDfor more than one query on aSVS_SYNC_KIND_FINE_GRAINindex, matching top-K.Implementation
filtered_range_search()(filtered_search.hpp) walks each query's batch iterator in parallel. It stops at the first candidate outside the radius, or when the row reachesmax_results. The distance direction comes from the index metric throughDistanceDispatcher.Index::search_range()now writes into aSearchResults&. Implementations:IndexVamana(static).DynamicIndexVamana, which takes the shared lock, soNONEandGLOBALare covered.ConcurrentIndexVamana(FINE_GRAIN), limited to one query per call. Deleted and concurrently consolidated IDs are already skipped by the batch iterators.DynamicIndexVamana::make_sample_generator()now holds the ID-sampling code used for filter-rate estimation. Filtered top-K and range search share it.SearchResults(search_results.hpp) is a non-owning wrapper oversvs_search_results_t. It provides:validate(): version andstruct_sizechecks.reserve(): grows buffers the library owns; caller-owned buffers that are too small are rejected. It allocates the new buffer before freeing the old one, so a failed allocation leaves the existing buffer in place.assign(): writes results from aQueryResultplus per-query counts, or from per-query neighbor lists.reset(): the logic behindsvs_search_results_free().It replaces the former
ensure_search_results_capacity()helper insvs_c.cpp.Documentation
svs_c.h: full description of range search, a newSVS_ERROR_INVALID_ARGUMENTcase forsvs_index_search_topk()when caller-owned buffers are too small, and the fine-grain one-query limit now listing range search.docs/C_API_Design.md: range search moved from "Future Extensions" to current capabilities, with a usage example, notes on radius, limit and buffer behaviour, and an updated function overview.README.md: mentions range search.Testing
New file
tests/c_api_search_results.cpp:svs_search_results_free()safe to call twice and on NULL;versionorstruct_sizerejected;svs_search_results_row()accessor, including out-of-range rows.filter_rateset too high (empty rows);max_results: each capped row matches the start of the uncapped row, and buffers sizednum_queries * max_resultsare enough;max_resultscombined with the ID filter.tests/c_api_dynamic_index_sync.cppnow also calls range search in the sequential test and in the concurrent reader threads.closes #425