c
include <stdio.h> // For printing.
include <assert.h> // For assertions!
include <usearch/usearch.h> // For the win 🚀
Filtering with Predicates
The new USearch release exposes low-level C++ predicated-search functionality to Rust and C 99, also providing several convenience methods already present in the Python and JavaScript APIs. In Rust you may use it like this:
rust
let is_odd = |key: Key| key % 2 == 1;
let query = vec![0.2, 0.1, 0.2, 0.1, 0.3];
let results = index.filtered_search(&query, 10, is_odd).unwrap();
assert!(
results.keys.iter().all(|&key| key % 2 == 1),
"All keys must be odd"
);
The same using the C:
c
int is_odd(usearch_key_t key, void* state) {
return key % 2;
}
int main() {
...
usearch_key_t found_keys[10];
usearch_distance_t found_distances[10];
usearch_filtered_search(
index, &query[0], usearch_scalar_f32_k, 10,
&is_odd, NULL, // no state needed for this callback
&found_keys[0], &found_distances[0], &error);
User Defined Metrics
While most vector search packages concentrate on just two metrics, "Inner Product distance" and "Euclidean distance", USearch allows arbitrary user-defined metrics. This flexibility allows you to customize your search for various applications, from computing geospatial coordinates with the rare [Haversine][haversine] distance to creating custom metrics for composite embeddings from multiple AI models, like joint image-text embeddings. You could already use [Numba][numba], [Cppyy][cppyy], or [PeachPy][peachpy] to define your [custom metric even in Python](https://unum-cloud.github.io/usearch/python#user-defined-metrics-and-jit-in-python):
[numba]: https://numba.readthedocs.io/en/stable/reference/jit-compilation.html#c-callbacks
[cppyy]: https://cppyy.readthedocs.io/en/latest/
[peachpy]: https://github.com/Maratyszcza/PeachPy
py
from numba import cfunc, types, carray
from usearch.index import Index, MetricKind, MetricSignature, CompiledMetric
cfunc(types.float32(types.CPointer(types.float32), types.CPointer(types.float32)))
def python_inner_product(a, b):
a_array = carray(a, ndim)
b_array = carray(b, ndim)
c = 0.0
for i in range(ndim):
c += a_array[i] * b_array[i]
return 1 - c
metric = CompiledMetric(pointer=python_inner_product.address, kind=MetricKind.IP, signature=MetricSignature.ArrayArray)
index = Index(ndim=ndim, metric=metric, dtype=np.float32)
Similar effect was much easier to achieve in the C++ layer, and is now also exposed to Rust and C.
c
simsimd_distance_t callback(void const* a, void const* b, void* state) {
// Your custom metric implementation here
}
int main() {
...
void callback_state = NULL;
usearch_change_metric(index, callback, callback_state, usearch_metric_unknown_k, &error);
Let's say you are implementing a weighted distance function to search through joint embeddings of images and textual descriptions of some products in a catalog, taking some [UForm](https://github.com/unum-cloud/uform) or CLIP-like multimodal embedding models. Implementing that in Rust using [SimSIMD](https://github.com/ashvardanian/simsimd) for unimodal slices may look like:
rust
use simsimd::SpatialSimilarity;
let image_dimensions: usize = 768;
let text_dimensions: usize = 512;