---
title: "Primary index"
description: "Guide to querying all records in an Aerospike namespace and set using the Rust client's primary index."
---

# Primary index

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

The Aerospike Rust client lets you query all records in a specified namespace and set.

## Querying records

This example counts the number of records in the query result:

```rust
use aerospike::{Bins, Client, ClientPolicy, Error, QueryPolicy, Statement};

use aerospike::query::PartitionFilter;

use futures::stream::StreamExt;

#[tokio::main]

async fn main() -> Result<(), Error> {

    let hosts = std::env::var("AEROSPIKE_HOSTS")

        .unwrap_or_else(|_| "localhost:3000".to_string());

    let client = Client::new(&ClientPolicy::default(), &hosts).await?;

    let policy = QueryPolicy::default();

    let stmt = Statement::new("test", "demo", Bins::None);

    let rs = client.query(&policy, PartitionFilter::all(), stmt).await?;

    let mut stream = rs.into_stream();

    let mut count: usize = 0;

    while let Some(result) = stream.next().await {

        match result {

            Ok(_record) => count += 1,

            Err(err) => println!("Error during scan: {}", err),

        }

    }

    println!("Records: {}", count);

    client.close().await?;

    Ok(())

}
```

> 📖 **API reference**: [`Client::new`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.new) | [`Client::query`](https://docs.rs/aerospike/latest/aerospike/struct.Client.html#method.query) | [`Statement::new`](https://docs.rs/aerospike/latest/aerospike/struct.Statement.html#method.new) | [`PartitionFilter::all`](https://docs.rs/aerospike/latest/aerospike/struct.PartitionFilter.html#method.all) | [`Recordset::into_stream`](https://docs.rs/aerospike/latest/aerospike/struct.Recordset.html#method.into_stream)

## Reading bins from query results

Each `Ok(record)` in the stream is a [`Record`](https://docs.rs/aerospike/latest/aerospike/struct.Record.html):

-   `record.bins` is an [`IndexMap<String, Value>`](https://docs.rs/aerospike/latest/aerospike/struct.IndexMap.html) (not a `HashMap`, and not an `Option`). It supports the same `.get()` method, so read a bin with `record.bins.get("name")`; unlike `HashMap`, iteration order matches the order the server returned the bins.
-   `record.key` is `Option<Key>`. Print it with `{:?}` (for example `println!("{:?}", record.key)`), not `{}`.

```rust
while let Some(result) = stream.next().await {

    match result {

        Ok(record) => {

            if let Some(name) = record.bins.get("name") {

                println!("name: {name}");

            }

        }

        Err(err) => println!("Error during query: {err}"),

    }

}
```

The query executes asynchronously. If an error occurs while executing the query, parallel query tasks to other nodes are terminated and the error propagates back through the record stream.

## Query a range of partitions

A namespace is divided into 4096 partitions, numbered 0 to 4095. `PartitionFilter::all()` queries all of them. To query only some of them, use `PartitionFilter::by_range(begin, count)`, where `begin` is the first partition ID and `count` is the number of partitions, from 1 to 4096. `PartitionFilter::by_id(id)` queries one partition.

Ranges that don’t overlap and together cover partitions 0 to 4095 return every record exactly once on a stable cluster with no concurrent writes. During migrations, node restarts, or concurrent writes, a record can appear twice or be missed, so make your processing idempotent. You can use ranges to split the processing of a set among independent workers. Each range query reads from every node that owns partitions in the range, so running ranges in parallel multiplies the load on the cluster. This example queries the two halves of the partitions one after the other and adds up the counts:

```rust
use aerospike::query::PartitionFilter;

let halves = [PartitionFilter::by_range(0, 2048), PartitionFilter::by_range(2048, 2048)];

let mut total = 0;

for partition_filter in halves {

    let stmt = Statement::new("test", "demo", Bins::None);

    let rs = client.query(&QueryPolicy::default(), partition_filter, stmt).await?;

    let mut stream = rs.into_stream();

    let mut count = 0;

    while let Some(result) = stream.next().await {

        result?;

        count += 1;

    }

    println!("{count} records");

    total += count;

}

println!("total: {total}");
```

> 📖 **API reference**: [`PartitionFilter::by_range`](https://docs.rs/aerospike/latest/aerospike/struct.PartitionFilter.html#method.by_range) | [`PartitionFilter::by_id`](https://docs.rs/aerospike/latest/aerospike/struct.PartitionFilter.html#method.by_id)