C++ · Filtered Search

Filtered Search

#include <usearch/types.hpp> 3.0.0-rc.1 · cabb6603

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/usearch/cpp/filtered-search.md' | claude -p "How would this fit my code?"

Cross-referencing an external database during traversal beats paging through results and filtering afterwards. Non-matching entries still route the traversal, so recall does not collapse at low selectivity.

auto is_odd = [](usearch_key_t key) noexcept { return key.bytes[15] % 2 == 1; };
auto [matches, status] = index.on(executor).search<float>({query, 1, 768}, 10, arena, {.predicate = {is_odd}});

auto in_stock = [&](std::span<usearch_key_t const> keys, std::span<usearch_admission_t> admissions) noexcept {
    inventory.lookup(keys, admissions); // one round trip per batch
};
auto [stocked, stocked_status] = index.on(executor).search<float>({query, 1, 768}, 10, arena, {.predicate = {in_stock}});

A predicate is a noexcept callable with a const call operator, called by the executor’s threads together, so a mutable lambda is refused at compile time and any state it reaches is guarded. One answering a single key as a bool is cheap, asked as candidates are scored. One writing an admission per key of a batch is expensive by default, asked once per round, unless us::predicate_t {handler, usearch_predicate_cost_cheap_k} says otherwise. A VRAM handle refuses a host predicate with status_code_t::device_k.

Every long verb’s config takes a progress handler, called with the units finished so far and the units the call finishes if it runs to the end. Answering usearch_cancel_k stops the call at the next increment boundary.

std::chrono::steady_clock::time_point const deadline = std::chrono::steady_clock::now() + std::chrono::seconds(30);
auto before_the_deadline = [&](std::size_t, std::size_t) noexcept {
    return std::chrono::steady_clock::now() < deadline ? usearch_continue_k : usearch_cancel_k;
};
us::status_t const refined = index.on(executor).refine(
    {.refine = {.expansion_factor = 128}, .progress = {before_the_deadline}}).status;
if (refined.failed()) assert(refined == us::status_code_t::cancelled_k && "only the deadline stops it");

The handler arrives on whichever thread advanced the work and never on two at once, which is what lets it carry a counter or a deadline without a lock of its own. Reports are denominated in whatever the verb consumes: entries for an insertion, queries for a search, a phase’s entries or islands for a refine or a reshard, draws for a sample, shards for a checkpoint, and bytes for a buffer. How often they arrive is the increment’s size rather than a guarantee, so read the counts and never the number of calls.

What a stop leaves behind is each verb’s own contract. An insert cannot be stopped: it claims every entry before its first report, ignores a cancel, and publishes the whole batch at once. A stopped search reports status_code_t::cancelled_k and leaves no matches. A stopped refine or reshard reports status_code_t::cancelled_k and leaves a graph that saves; without a plan it closes the round where it stopped, and with one it leaves the plan live for the next call. A stopped load creates no index, and a stopped save_buffer leaves no monolith.

API

Types

progress_tA progress handler read only during one call as its own state; empty hears nothing.
types.hpp:550 ↗
class progress_t
predicate_tA predicate handler read only during one call as its own state; empty admits every key.
types.hpp:571 ↗
class predicate_t