# Filtered Search

> Cross-referencing an external database during traversal beats paging through results and filtering afterwards.

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

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.

```cpp
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.

```cpp
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_t`

A progress handler read only during one call as its own state; empty hears nothing.

[types.hpp:550 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/types.hpp#L550)

```cpp
class progress_t
```

#### `predicate_t`

A predicate handler read only during one call as its own state; empty admits every key.

[types.hpp:571 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/types.hpp#L571)

```cpp
class predicate_t
```
