---
title: "Tutorial for Expression Indexes"
description: "Tutorial for creating and querying sparse expression indexes in Aerospike Database 8.1.0+."
---

# Tutorial for Expression Indexes

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

This tutorial introduces expression indexes in Aerospike Database. This tutorial assumes you are familiar with [secondary indexes](https://aerospike.com/docs/database/learn/architecture/data-storage/secondary-index/) and [expressions](https://aerospike.com/docs/develop/expressions/) in Aerospike Database.

## Overview

Expression indexes build sparse secondary indexes over computed values defined by an Aerospike expression. Instead of indexing every record or persisting derived bins, the index evaluates your expression and includes only the records that match.

In this tutorial, you will:

-   Create your first expression index
-   Run your first [query](https://aerospike.com/docs/develop/learn/queries/secondary-index/) against it

::: note
Prior to Database 8.1.0, secondary indexes were limited to raw data, or fields other than the primary key.
:::

### Why it matters

-   **Simpler application code:** push filter logic into the index; no extra bins or duplicated client filters.
-   **Reduced memory footprint:** indexes include only relevant records.
-   **Total cost of ownership:** reduced RAM and compute requirements lower infrastructure spend.
-   **Faster queries:** less to scan means lower latency.

### When to use expression indexes

Use them when you would otherwise:

-   Persist a derived value solely to make it indexable.
-   Index every record even though only a subset are needed for business logic.
-   Re-implement the same filter logic across multiple clients or languages.

## Get started

### Prerequisites

-   A running instance of Aerospike Database 8.1.0 or later. The Java SDK and Python SDK examples need Database 8.2.0 or later, which compiles their AEL.
-   Updated Client (minimum versions):
    -   [Java SDK: v1.0.0+](https://aerospike.com/docs/develop/client/sdk/install/)
    -   [Python SDK: v1.0.0+](https://aerospike.com/docs/develop/client/sdk/install/)
    -   [Rust: v2.0.0+](https://aerospike.com/docs/develop/client/rust/install/)
    -   [C#: v8.1.0+](https://aerospike.com/docs/develop/client/csharp/install/)
    -   [Go: v8.4.0+](https://aerospike.com/docs/develop/client/go/install/)
    -   [Node.js: v6.3.0+](https://aerospike.com/docs/develop/client/node/install/)
    -   [C: v7.1.0+](https://aerospike.com/docs/develop/client/c/install/)
    -   [Java: v9.1.0+](https://aerospike.com/docs/develop/client/java/install/)
    -   [Python: v17.1.0+](https://aerospike.com/docs/develop/client/python/install/)
    -   PHP: not available

Verify your server version using [asinfo](https://aerospike.com/docs/database/tools/asinfo/):

Terminal window

```bash
# Verify you’re running Aerospike 8.1.0+

asinfo -v build
```

### Create expression, index, and query

1.  Create the expression.
    
    The following example creates the “adult users in target countries” expression. For this example, let’s assume an adult is someone 18 or older, and our target countries are Australia, Canada and Botswana.
    
    For this tutorial, we’ll generate the Base64 representation of the expression in the client and then use the `asadm` command-line tool to create the index. This approach separates application logic (defining the filter) from database administration (creating the index).
    
    -   [Java SDK](#tab-panel-4766)
    -   [Python SDK](#tab-panel-4767)
    -   [Rust](#tab-panel-4768)
    -   [C#](#tab-panel-4769)
    -   [Go](#tab-panel-4770)
    -   [Node.js](#tab-panel-4771)
    -   [C](#tab-panel-4772)
    -   [Java](#tab-panel-4773)
    -   [Python](#tab-panel-4774)
    
    ```java
    import com.aerospike.client.sdk.exp.Expression;
    
    // Build an expression that indexes age only for adults in AU/CA/BW that are 18 or older
    
    // In AEL: when the customer is 18 or older and lives in a target country, their age;
    
    // otherwise `unknown`, which leaves the record out of the index
    
    String ael = "when ($.age >= 18 and ($.country == 'Australia'"
    
        + " or $.country == 'Canada' or $.country == 'Botswana')"
    
        + " => $.age, default => unknown)";
    
    // Get the base64 for asadm's `exp_base64`; the server compiles the AEL when it creates the index
    
    String b64 = Expression.fromServerCompiledFilter(ael).getBase64();
    
    System.out.println(b64);
    
    // Prints: ksyA2YJ3aGVuICgkLmFnZSA+PSAxOCBhbmQgKCQuY291bnRyeSA9PSAnQXVzdHJhbGlhJyBvciAkLmNvdW50cnkgPT0gJ0NhbmFkYScgb3IgJC5jb3VudHJ5ID09ICdCb3Rzd2FuYScpID0+ICQuYWdlLCBkZWZhdWx0ID0+IHVua25vd24p
    
    // The SDK can also create the index itself, from the AEL:
    
    // session.createIndex(DataSet.of("test", "cust_data"), "cust_index",
    
    //         IndexType.INTEGER, IndexCollectionType.DEFAULT, ael)
    
    //     .waitTillComplete();
    ```
    
    ```python
    from aerospike_sdk.exp import Exp
    
    # Build an expression that indexes age only for adults in AU/CA/BW that are 18 or older
    
    # In AEL: when the customer is 18 or older and lives in a target country, their age;
    
    # otherwise `unknown`, which leaves the record out of the index
    
    ael = (
    
        "when ($.age >= 18 and ($.country == 'Australia'"
    
        " or $.country == 'Canada' or $.country == 'Botswana')"
    
        " => $.age, default => unknown)"
    
    )
    
    # Get the base64 for asadm's `exp_base64`; the server compiles the AEL when it creates the index
    
    b64 = Exp.from_server_compiled_ael(ael).base64()
    
    print(b64)
    
    # Prints: ksyAxIJ3aGVuICgkLmFnZSA+PSAxOCBhbmQgKCQuY291bnRyeSA9PSAnQXVzdHJhbGlhJyBvciAkLmNvdW50cnkgPT0gJ0NhbmFkYScgb3IgJC5jb3VudHJ5ID09ICdCb3Rzd2FuYScpID0+ICQuYWdlLCBkZWZhdWx0ID0+IHVua25vd24p
    
    # The SDK can also create the index itself, from the AEL:
    
    # (session.index("test", "cust_data").on_expression(ael).named("cust_index")
    
    #     .integer().create().wait_till_complete_blocking())
    ```
    
    ```rust
    use aerospike::expressions::{and, cond, eq, ge, int_bin, int_val, or, string_bin, string_val, unknown};
    
    // Build an expression that indexes age only for adults in AU/CA/BW that are 18 or older
    
    let filter_exp = cond(vec![
    
        and(vec![
    
            // Is the age 18 or older?
    
            ge(int_bin("age".to_string()), int_val(18)),
    
            // Do they live in a target country?
    
            or(vec![
    
                eq(string_bin("country".to_string()), string_val("Australia".to_string())),
    
                eq(string_bin("country".to_string()), string_val("Canada".to_string())),
    
                eq(string_bin("country".to_string()), string_val("Botswana".to_string())),
    
            ]),
    
        ]),
    
        // If true, return the age of the customer to be indexed
    
        int_bin("age".to_string()),
    
        // Otherwise return "unknown" to exclude the record from the index
    
        unknown(),
    
    ]);
    
    println!("{}", filter_exp.base64()?);
    
    // Prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    ```
    
    ```c#
    // Build an expression that indexes age only for adults in AU/CA/BW
    
    Expression filterExp = Exp.Build(
    
        // The `Cond` expression is the top-level IF/THEN/ELSE
    
        Exp.Cond(
    
            // IF block: Defines the conditions for the index
    
            Exp.And(
    
                // Condition 1: Is the age 18 or older?
    
                Exp.GE(Exp.IntBin("age"), Exp.Val(18)),
    
                // Condition 2: Do they live in a target country?
    
                Exp.Or(
    
                    Exp.EQ(Exp.StringBin("country"), Exp.Val("Australia")),
    
                    Exp.EQ(Exp.StringBin("country"), Exp.Val("Canada")),
    
                    Exp.EQ(Exp.StringBin("country"), Exp.Val("Botswana"))
    
                )
    
            ),
    
            // If true, return the integer value from the 'age' bin.
    
            // This value is what gets indexed.
    
            Exp.IntBin("age"),
    
            // ELSE If false, return "unknown".
    
            // This excludes the record from the index.
    
            Exp.Unknown()
    
        )
    
    );
    
    // Print the Base64-encoded expression for use with asadm
    
    string exprBase64 = filterExp.GetBase64();
    
    Console.WriteLine("Expression Base64:");
    
    Console.WriteLine(exprBase64);
    
    // Prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    ```
    
    ```go
    // go get github.com/aerospike/aerospike-client-go/v8
    
    import (
    
      "fmt"
    
      as "github.com/aerospike/aerospike-client-go/v8"
    
    )
    
    // Build an expression that indexes age only for adults in AU/CA/BW that are 18 or older
    
    filterExp := as.ExpCond(
    
      as.ExpAnd(
    
        // Is the age 18 or older?
    
        as.ExpGreaterEq(as.ExpIntBin("age"), as.ExpIntVal(18)),
    
        // Do they live in a target country?
    
        as.ExpOr(
    
          as.ExpEq(as.ExpStringBin("country"), as.ExpStringVal("Australia")),
    
          as.ExpEq(as.ExpStringBin("country"), as.ExpStringVal("Canada")),
    
          as.ExpEq(as.ExpStringBin("country"), as.ExpStringVal("Botswana")),
    
        ),
    
      ),
    
      // If true, return the age of the customer to be indexed
    
      as.ExpIntBin("age"),
    
      // Otherwise return "unknown" to exclude the record from the index
    
      as.ExpUnknown(),
    
    )
    
    b64, err := filterExp.Base64()
    
    if err != nil {
    
      panic(err)
    
    }
    
    fmt.Println(b64)
    
    // Prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    ```
    
    ```js
    // npm install aerospike
    
    import Aerospike from "aerospike";
    
    const exp = Aerospike.exp;
    
    const filterExp = exp.cond(
    
      exp.and(
    
        exp.ge(exp.binInt("age"), exp.int(18)),
    
        exp.or(
    
          exp.eq(exp.binStr("country"), exp.str("Australia")),
    
          exp.eq(exp.binStr("country"), exp.str("Canada")),
    
          exp.eq(exp.binStr("country"), exp.str("Botswana")),
    
        ),
    
      ),
    
      exp.binInt("age"),
    
      exp.unknown(),
    
    );
    
    // Get Base64 for use with asadm `exp_base64` (requires a connected client)
    
    const client = await Aerospike.connect({ hosts: "127.0.0.1:3000" });
    
    console.log(client.expressionToBase64(filterExp));
    
    // Prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    
    await client.close();
    ```
    
    ```c
    as_exp_build(filter_exp,
    
        as_exp_cond(
    
            as_exp_and(
    
                as_exp_cmp_ge(
    
                    as_exp_bin_int("age"),
    
                    as_exp_int(18)),
    
                as_exp_or(
    
                    as_exp_cmp_eq(as_exp_bin_str("country"), as_exp_str("Australia")),
    
                    as_exp_cmp_eq(as_exp_bin_str("country"), as_exp_str("Canada")),
    
                    as_exp_cmp_eq(as_exp_bin_str("country"), as_exp_str("Botswana"))
    
                    )),
    
            // If true, return the value in the age bin
    
            as_exp_bin_int("age"),
    
            // return unknown to exclude value from index
    
            as_exp_unknown()
    
        ));
    
    char* b64 = as_exp_to_base64(filter_exp);
    
    printf("Expression base64 = %s\n", b64);
    
    // Prints:
    
    // Expression base64 = lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    
    as_exp_destroy_base64(b64);
    
    as_exp_destroy(filter_exp);
    ```
    
    ```java
    // Build an expression that indexes age only for adults in AU/CA/BW that are 18 or older
    
    Expression filterExp = Exp.build(
    
            Exp.cond(
    
                Exp.and(
    
                    Exp.ge(// Is the age 18 or older?
    
                            Exp.intBin("age"),
    
                            Exp.val(18)
    
                    ),
    
                    Exp.or( // Do they live in a target country?
    
                            Exp.eq(Exp.stringBin("country"), Exp.val("Australia")),
    
                            Exp.eq(Exp.stringBin("country"), Exp.val("Canada")),
    
                            Exp.eq(Exp.stringBin("country"), Exp.val("Botswana"))
    
                    )
    
                ),
    
                Exp.intBin("age"), // If true, return the age of the customer to be indexed
    
                Exp.unknown() // returns "unknown" to exclude the record from the index
    
            )
    
        );
    
    System.out.println(filterExp.getBase64());
    
    // Prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    ```
    
    ```python
    # pip install aerospike
    
    import aerospike
    
    from aerospike_helpers import expressions as exp
    
    target_countries = ["Australia", "Canada", "Botswana"]
    
    # Build the "OR" condition for the country check
    
    country_is_target = exp.Or(
    
        *[exp.Eq(exp.StrBin("country"), c) for c in target_countries]
    
    )
    
    # Compile the final expression
    
    filter_exp = exp.Cond(
    
        # IF block: Both conditions must be true
    
        exp.And(
    
            exp.GE(exp.IntBin("age"), 18),
    
            country_is_target
    
        ),
    
        # Return the value of the 'age' bin for indexing
    
        exp.IntBin("age"),
    
        # ELSE Return "unknown" to exclude the record from the index
    
        exp.Unknown()
    
    ).compile()
    
    # Get Base64 for use with asadm `exp_base64`
    
    client = aerospike.client({"hosts": [("127.0.0.1", 3000)]}).connect()
    
    expr_b64 = client.get_expression_base64(filter_exp)
    
    print(expr_b64)
    
    # prints: lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA
    
    client.close()
    ```
    
    The client libraries print the same base64 string. The Java SDK and Python SDK print their AEL’s instead, which the server compiles to the same expression when it creates the index.
    
2.  Create the expression index.
    
    Use [`asadm`](https://aerospike.com/docs/database/tools/asadm) or [`sindex-create`](https://aerospike.com/docs/database/reference/info/#sindex-create) through `asinfo` to create an integer secondary index on the expression. Both accept the `integer` key type in Database 8.2.0 and later. Use `asadm` for interactive work and `asinfo` for automation. Copy the full Base64 string from the previous step’s output and use it in one of the following commands.
    
    asadm
    
    ```bash
    asadm -e "enable; manage sindex create integer cust_index ns test set cust_data exp_base64 lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA"
    ```
    
    asinfo
    
    ```bash
    asinfo -v 'sindex-create:namespace=test;set=cust_data;indexname=cust_index;indextype=default;exp=lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEA;type=integer'
    ```
    
    ::: note
    Prior to Database 8.2.0, use `numeric` in place of `integer`.
    :::
    
3.  Insert some test data.
    
    -   [Java SDK](#tab-panel-4775)
    -   [Python SDK](#tab-panel-4776)
    -   [Rust](#tab-panel-4777)
    -   [C#](#tab-panel-4778)
    -   [Go](#tab-panel-4779)
    -   [Node.js](#tab-panel-4780)
    -   [C](#tab-panel-4781)
    -   [Java](#tab-panel-4782)
    -   [Python](#tab-panel-4783)
    
    ```java
    DataSet custData = DataSet.of("test", "cust_data");
    
    // Insert sample customers to demonstrate which records get indexed
    
    session.upsert(custData)
    
        .bins("name", "age", "country")
    
        .id(1).values("Tim", 31, "Australia")
    
        .id(2).values("Bob", 47, "Canada")
    
        .id(3).values("Jo", 15, "USA")          // not indexed
    
        .id(4).values("Steven", 23, "Botswana")
    
        .id(5).values("Susan", 32, "Canada")
    
        .id(6).values("Jess", 17, "Botswana")   // not indexed
    
        .id(7).values("Sam", 18, "USA")         // not indexed
    
        .id(8).values("Alex", 47, "Canada")
    
        .id(9).values("Pam", 56, "Australia")
    
        .id(10).values("Vivek", 12, "India")    // not indexed
    
        .id(11).values("Kiril", 22, "Sweden")   // not indexed
    
        .id(12).values("Bill", 23, "UK")        // not indexed
    
        .execute();
    ```
    
    ```python
    from aerospike_sdk import DataSet
    
    cust_data = DataSet.of("test", "cust_data")
    
    # Insert sample customers to demonstrate which records get indexed
    
    (
    
        session.upsert(cust_data)
    
        .bins("name", "age", "country")
    
        .row(1, "Tim", 31, "Australia")
    
        .row(2, "Bob", 47, "Canada")
    
        .row(3, "Jo", 15, "USA")            # not indexed
    
        .row(4, "Steven", 23, "Botswana")
    
        .row(5, "Susan", 32, "Canada")
    
        .row(6, "Jess", 17, "Botswana")     # not indexed
    
        .row(7, "Sam", 18, "USA")           # not indexed
    
        .row(8, "Alex", 47, "Canada")
    
        .row(9, "Pam", 56, "Australia")
    
        .row(10, "Vivek", 12, "India")      # not indexed
    
        .row(11, "Kiril", 22, "Sweden")     # not indexed
    
        .row(12, "Bill", 23, "UK")          # not indexed
    
        .execute()
    
    )
    ```
    
    ```rust
    use aerospike::{as_bin, as_key, WritePolicy};
    
    // Insert sample customers to demonstrate which records get indexed
    
    let customers = [
    
        (1, "Tim", 31, "Australia"),
    
        (2, "Bob", 47, "Canada"),
    
        (3, "Jo", 15, "USA"),           // not indexed
    
        (4, "Steven", 23, "Botswana"),
    
        (5, "Susan", 32, "Canada"),
    
        (6, "Jess", 17, "Botswana"),    // not indexed
    
        (7, "Sam", 18, "USA"),          // not indexed
    
        (8, "Alex", 47, "Canada"),
    
        (9, "Pam", 56, "Australia"),
    
        (10, "Vivek", 12, "India"),     // not indexed
    
        (11, "Kiril", 22, "Sweden"),    // not indexed
    
        (12, "Bill", 23, "UK"),         // not indexed
    
    ];
    
    for (id, name, age, country) in customers {
    
        let key = as_key!("test", "cust_data", id);
    
        let bins = [as_bin!("name", name), as_bin!("age", age), as_bin!("country", country)];
    
        client.put(&WritePolicy::default(), &key, &bins).await?;
    
    }
    ```
    
    ```c#
    // Define the namespace and set for the records
    
    const string NAMESPACE = "test";
    
    const string SET = "cust_data";
    
    // A helper method to insert a customer record
    
    public void Insert(int key, string name, int age, string country)
    
    {
    
        client.Put(null,
    
            new Key(NAMESPACE, SET, key),
    
            new Bin("name", name),
    
            new Bin("age", age),
    
            new Bin("country", country));
    
    }
    
    // Insert sample customers to demonstrate which records get indexed
    
    public void InsertData()
    
    {
    
        Insert(1, "Tim", 31, "Australia");
    
        Insert(2, "Bob", 47, "Canada");
    
        Insert(3, "Jo", 15, "USA"); // not indexed
    
        Insert(4, "Steven", 23, "Botswana");
    
        Insert(5, "Susan", 32, "Canada");
    
        Insert(6, "Jess", 17, "Botswana"); // not indexed
    
        Insert(7, "Sam", 18, "USA"); // not indexed
    
        Insert(8, "Alex", 47, "Canada");
    
        Insert(9, "Pam", 56, "Australia");
    
        Insert(10, "Vivek", 12, "India"); // not indexed
    
        Insert(11, "Kiril", 22, "Sweden"); // not indexed
    
        Insert(12, "Bill", 23, "UK"); // not indexed
    
    }
    ```
    
    ```go
    import (
    
      "fmt"
    
      as "github.com/aerospike/aerospike-client-go/v8"
    
    )
    
    // Define the namespace and set for the records
    
    const (
    
      NAMESPACE = "test"
    
      SET       = "cust_data"
    
    )
    
    // A helper to insert a customer record
    
    func insert(client *as.Client, id int, name string, age int, country string) {
    
      key, _ := as.NewKey(NAMESPACE, SET, id)
    
      if err := client.PutBins(nil, key,
    
        as.NewBin("name", name),
    
        as.NewBin("age", age),
    
        as.NewBin("country", country)); err != nil {
    
        panic(err)
    
      }
    
    }
    
    // Insert sample customers to demonstrate which records get indexed
    
    func doInsertData(client *as.Client) {
    
      insert(client, 1, "Tim", 31, "Australia")
    
      insert(client, 2, "Bob", 47, "Canada")
    
      insert(client, 3, "Jo", 15, "USA") // not indexed
    
      insert(client, 4, "Steven", 23, "Botswana")
    
      insert(client, 5, "Susan", 32, "Canada")
    
      insert(client, 6, "Jess", 17, "Botswana") // not indexed
    
      insert(client, 7, "Sam", 18, "USA")       // not indexed
    
      insert(client, 8, "Alex", 47, "Canada")
    
      insert(client, 9, "Pam", 56, "Australia")
    
      insert(client, 10, "Vivek", 12, "India")  // not indexed
    
      insert(client, 11, "Kiril", 22, "Sweden") // not indexed
    
      insert(client, 12, "Bill", 23, "UK")      // not indexed
    
    }
    
    // Connect and load the data
    
    client, err := as.NewClient("127.0.0.1", 3000)
    
    if err != nil {
    
      panic(err)
    
    }
    
    defer client.Close()
    
    doInsertData(client)
    
    fmt.Println("Insert data: done")
    ```
    
    ```js
    import Aerospike from "aerospike";
    
    const key = Aerospike.Key;
    
    // Define the namespace and set for the records
    
    const NAMESPACE = "test";
    
    const SET = "cust_data";
    
    const client = await Aerospike.connect({
    
      hosts: "127.0.0.1:3000",
    
    });
    
    // Sample customer data
    
    const customers = [
    
      { key: 1, name: "Tim", age: 31, country: "Australia" },
    
      { key: 2, name: "Bob", age: 47, country: "Canada" },
    
      { key: 3, name: "Jo", age: 15, country: "USA" }, // not indexed
    
      { key: 4, name: "Steven", age: 23, country: "Botswana" },
    
      { key: 5, name: "Susan", age: 32, country: "Canada" },
    
      { key: 6, name: "Jess", age: 17, country: "Botswana" }, // not indexed
    
      { key: 7, name: "Sam", age: 18, country: "USA" }, // not indexed
    
      { key: 8, name: "Alex", age: 47, country: "Canada" },
    
      { key: 9, name: "Pam", age: 56, country: "Australia" },
    
      { key: 10, name: "Vivek", age: 12, country: "India" }, // not indexed
    
      { key: 11, name: "Kiril", age: 22, country: "Sweden" }, // not indexed
    
      { key: 12, name: "Bill", age: 23, country: "UK" }, // not indexed
    
    ];
    
    // Insert sample customers to demonstrate which records get indexed
    
    const promises = customers.map((customer) => {
    
      return client.put(new key(NAMESPACE, SET, customer.key), {
    
        name: customer.name,
    
        age: customer.age,
    
        country: customer.country,
    
      });
    
    });
    
    await Promise.all(promises);
    
    await client.close();
    ```
    
    ```c
    // Insert a customer record
    
    static void
    
    insert(aerospike* as, int key, const char* name, int age, const char* country)
    
    {
    
        as_error err;
    
        as_key int_key;
    
        as_key_init_int64(&int_key, "test", "cust_data", key);
    
        as_record rec;
    
        as_record_inita(&rec, 3);
    
        as_record_set_str(&rec, "name", name);
    
        as_record_set_int64(&rec, "age", age);
    
        as_record_set_str(&rec, "country", country);
    
        if (aerospike_key_put(as, &err, NULL, &int_key, &rec) != AEROSPIKE_OK) {
    
            printf("aerospike_key_put() returned %d - %s\n", err.code, err.message);
    
        }
    
        as_record_destroy(&rec);
    
    }
    
    // Insert sample customers to demonstrate which records get indexed
    
    static void
    
    do_insert_data(aerospike* as)
    
    {
    
        insert(as, 1, "Tim", 31, "Australia");
    
        insert(as, 2, "Bob", 47, "Canada");
    
        insert(as, 3, "Jo", 15, "USA"); // not indexed
    
        insert(as, 4, "Steven", 23, "Botswana");
    
        insert(as, 5, "Susan", 32, "Canada");
    
        insert(as, 6, "Jess", 17, "Botswana"); // not indexed
    
        insert(as, 7, "Sam", 18, "USA"); // not indexed
    
        insert(as, 8, "Alex", 47, "Canada");
    
        insert(as, 9, "Pam", 56, "Australia");
    
        insert(as, 10, "Vivek", 12, "India"); // not indexed
    
        insert(as, 11, "Kiril", 22, "Sweden"); // not indexed
    
        insert(as, 12, "Bill", 23, "UK"); // not indexed
    
    }
    ```
    
    ```java
    // Define the namespace and set for the records
    
    final String NAMESPACE = "test";
    
    final String SET = "cust_data";
    
    // A helper method to insert a customer record
    
    public void insert(int key, String name, int age, String country) {
    
        client.put(null,
    
                new Key(NAMESPACE, SET, key),
    
                new Bin("name", name),
    
                new Bin("age", age),
    
                new Bin("country", country));
    
    }
    
    // Insert sample customers to demonstrate which records get indexed
    
    public void insertData() {
    
        insert(1, "Tim", 31, "Australia");
    
        insert(2, "Bob", 47, "Canada");
    
        insert(3, "Jo", 15, "USA"); // not indexed
    
        insert(4, "Steven", 23, "Botswana");
    
        insert(5, "Susan", 32, "Canada");
    
        insert(6, "Jess", 17, "Botswana"); // not indexed
    
        insert(7, "Sam", 18, "USA"); // not indexed
    
        insert(8, "Alex", 47, "Canada");
    
        insert(9, "Pam", 56, "Australia");
    
        insert(10, "Vivek", 12, "India"); // not indexed
    
        insert(11, "Kiril", 22, "Sweden"); // not indexed
    
        insert(12, "Bill", 23, "UK"); // not indexed
    
    }
    ```
    
    ```python
    import aerospike
    
    # Sample customer data
    
    customers = [
    
        (1, "Tim", 31, "Australia"),
    
        (2, "Bob", 47, "Canada"),
    
        (3, "Jo", 15, "USA"),           # not indexed
    
        (4, "Steven", 23, "Botswana"),
    
        (5, "Susan", 32, "Canada"),
    
        (6, "Jess", 17, "Botswana"),    # not indexed
    
        (7, "Sam", 18, "USA"),          # not indexed
    
        (8, "Alex", 47, "Canada"),
    
        (9, "Pam", 56, "Australia"),
    
        (10, "Vivek", 12, "India"),     # not indexed
    
        (11, "Kiril", 22, "Sweden"),    # not indexed
    
        (12, "Bill", 23, "UK"),         # not indexed
    
    ]
    
    # Client configuration (replace with your actual configuration)
    
    config = {"hosts": [("127.0.0.1", 3000)]}
    
    client = aerospike.client(config).connect()
    
    # Insert sample customers to demonstrate which records get indexed
    
    for key, name, age, country in customers:
    
        client.put(
    
            ("test", "cust_data", key),
    
            {
    
                "name": name,
    
                "age": age,
    
                "country": country
    
            }
    
        )
    
    client.close()
    ```
    
4.  Validate index entries.
    
    A single-node cluster shows 6 entries in the expression index (records 1, 2, 4, 5, 8, 9). `sindex-stat` reports each node’s own entries, replicas included, so on a larger cluster the nodes’ counts add up to 6 times the replication factor.
    
    Terminal window
    
    ```bash
    # Confirm only matching records (6) were indexed
    
    asinfo -v 'sindex-stat:namespace=test;indexname=cust_index' -l
    
    entries=6
    
    ...
    ```
    
5.  Query your Expression Index.
    
    Run a query to filter for ages 5 and above. Notice the results still only shows ages 18 and above.
    
    The Java SDK and Python SDK don’t name the index. Their `where` repeats the index’s expression, and the [query optimizer](https://aerospike.com/docs/develop/learn/queries/query-optimizer/#indexable-operator-surface) serves the query from the index. Write the expression exactly as the index was defined: `$.country in [...]` builds a different expression from the `or` the index uses, and the query fails.
    
    -   [Java SDK](#tab-panel-4784)
    -   [Python SDK](#tab-panel-4785)
    -   [Rust](#tab-panel-4786)
    -   [C#](#tab-panel-4787)
    -   [Go](#tab-panel-4788)
    -   [Node.js](#tab-panel-4789)
    -   [C](#tab-panel-4790)
    -   [Java](#tab-panel-4791)
    -   [Python](#tab-panel-4792)
    
    ```java
    // Query via the expression index to fetch records for customers that are 5 and older
    
    // A `where` on the index's expression is served by the index; `forIndex` names it.
    
    String ael = "when ($.age >= 18 and ($.country == 'Australia'"
    
        + " or $.country == 'Canada' or $.country == 'Botswana')"
    
        + " => $.age, default => unknown)";
    
    try (RecordStream rs = session.query(DataSet.of("test", "cust_data"))
    
            .where(ael + " >= 5")
    
            .withHint(hint -> hint.forIndex("cust_index"))
    
            .execute()) {
    
        rs.forEach(result -> System.out.println(result.recordOrThrow().bins));
    
    }
    ```
    
    ```python
    from aerospike_sdk import DataSet, QueryHint
    
    # Query via the expression index to fetch records for customers that are 5 and older
    
    # A `where` on the index's expression is served by the index; the hint names it.
    
    ael = (
    
        "when ($.age >= 18 and ($.country == 'Australia'"
    
        " or $.country == 'Canada' or $.country == 'Botswana')"
    
        " => $.age, default => unknown)"
    
    )
    
    stream = (
    
        session.query(DataSet.of("test", "cust_data"))
    
        .where(ael + " >= 5")
    
        .with_hint(QueryHint(index_name="cust_index"))
    
        .execute()
    
    )
    
    for row in stream:
    
        print(row.record_or_raise().bins)
    
    stream.close()
    ```
    
    ```rust
    use aerospike::query::{Filter, PartitionFilter};
    
    use aerospike::{Bins, QueryPolicy, Statement};
    
    use futures::StreamExt;
    
    // Query via the expression index to fetch records for customers that are 5 and older
    
    let mut stmt = Statement::new("test", "cust_data", Bins::All);
    
    // Use `range_by_index` to query the expression index
    
    stmt.add_filter(Filter::range_by_index("cust_index", 5, i64::MAX));
    
    let rs = client.query(&QueryPolicy::default(), PartitionFilter::all(), stmt).await?;
    
    let mut stream = rs.into_stream();
    
    while let Some(record) = stream.next().await {
    
        println!("{:?}", record?.bins);
    
    }
    ```
    
    ```c#
    // Query via the expression index to fetch records for customers that are 5 and older
    
    Statement stmt = new();
    
    stmt.SetNamespace("test");
    
    stmt.SetSetName("cust_data");
    
    // Use `RangeByIndex` to query the expression index
    
    stmt.SetFilter(Filter.RangeByIndex("cust_index", 5, int.MaxValue));
    
    // Execute the query and print the results
    
    using RecordSet recordSet = client.Query(null, stmt);
    
    while (recordSet.Next())
    
    {
    
        Console.WriteLine(recordSet.Record);
    
    }
    ```
    
    ```go
    import (
    
      "fmt"
    
      "math"
    
      as "github.com/aerospike/aerospike-client-go/v8"
    
    )
    
    // Query via the expression index to fetch records for customers that are 5 and older
    
    stmt := as.NewStatement("test", "cust_data")
    
    // Use `NewRangeWithIndexNameFilter` to query the expression index
    
    stmt.SetFilter(as.NewRangeWithIndexNameFilter("cust_index", 5, math.MaxInt64))
    
    rs, err := client.Query(nil, stmt)
    
    if err != nil {
    
      panic(err)
    
    }
    
    for rec := range rs.Results() {
    
      if rec.Err != nil {
    
        fmt.Printf("error: %v\n", rec.Err)
    
        continue
    
      }
    
      fmt.Println(rec.Record.Bins)
    
    }
    ```
    
    ```js
    import Aerospike from "aerospike";
    
    const filter = Aerospike.filter;
    
    // Define the namespace and set for the records
    
    const NAMESPACE = "test";
    
    const SET = "cust_data";
    
    const client = await Aerospike.connect({
    
      hosts: "127.0.0.1:3000",
    
    });
    
    // Create a query object
    
    const query = client.query(NAMESPACE, SET);
    
    // Query via the expression index to fetch records for customers that are 5 and older
    
    query.whereWithIndexName(
    
      filter.range(null, 5, Number.MAX_SAFE_INTEGER),
    
      "cust_index",
    
    );
    
    // Execute the query and print the results
    
    const results = await query.results();
    
    for (const result of results) {
    
      console.log(result.bins);
    
    }
    
    await client.close();
    ```
    
    ```c
    static bool query_cb(const as_val*, void*);
    
    // Query via the expression index to fetch records for customers that are 5 and older
    
    static void
    
    do_query_by_age_at_least_5(aerospike* as)
    
    {
    
        as_query query;
    
        as_query_init(&query, "test", "cust_data");
    
        as_query_where_init(&query, 1);
    
        as_query_where_with_index_name(&query, "cust_index",
    
                                       as_integer_range(5, INT_MAX));
    
        as_error err;
    
        if (aerospike_query_foreach(as, &err, NULL, &query, query_cb, NULL) !=
    
                AEROSPIKE_OK) {
    
            printf("aerospike_query_foreach() returned %d - %s\n", err.code, err.message);
    
        }
    
        as_query_destroy(&query);
    
    }
    
    static bool
    
    query_cb(const as_val* val, void* udata)
    
    {
    
        if (! val) {
    
            // Query is complete.
    
            return false;
    
        }
    
        // The client calls this from several threads, so print each record in one call
    
        as_record* rec = as_record_fromval(val);
    
        printf("name=%s age=%" PRId64 " country=%s\n",
    
               as_record_get_str(rec, "name"),
    
               as_record_get_int64(rec, "age", 0),
    
               as_record_get_str(rec, "country"));
    
        return true;
    
    }
    ```
    
    ```java
    // Query via the expression index to fetch records for customers that are 5 and older
    
    Statement stmt = new Statement();
    
    stmt.setNamespace("test");
    
    stmt.setSetName("cust_data");
    
    // Use `rangeByIndex` to query the expression index
    
    stmt.setFilter(Filter.rangeByIndex("cust_index", 5, Integer.MAX_VALUE));
    
    // Execute the query and print the results
    
    try (RecordSet recordSet = client.query(null, stmt)) {
    
        while (recordSet.next()) {
    
            System.out.println(recordSet.getRecord());
    
        }
    
    }
    ```
    
    ```python
    import sys
    
    import aerospike
    
    from aerospike import predicates as p
    
    # Connect to the database
    
    config = {"hosts": [("127.0.0.1", 3000)]}
    
    client = aerospike.client(config).connect()
    
    # Query via the expression index to fetch records for customers that are 5 and older
    
    q = client.query("test", "cust_data")
    
    # Set the query to use the expression index with a range filter
    
    q.where_with_index_name("cust_index",
    
                p.range(None, aerospike.INDEX_TYPE_DEFAULT, 5, sys.maxsize))
    
    # Define a function to handle each record returned by the query
    
    def handle(record):
    
        key, meta, bins = record
    
        print(bins)
    
    # Execute the query and apply the handler to each record
    
    q.foreach(handle)
    
    client.close()
    ```
    
    Since we stored age, you can filter for records above 25 by simply modifying the query:
    
    -   [Java SDK](#tab-panel-4793)
    -   [Python SDK](#tab-panel-4794)
    -   [Rust](#tab-panel-4795)
    -   [C#](#tab-panel-4796)
    -   [Go](#tab-panel-4797)
    -   [Node.js](#tab-panel-4798)
    -   [C](#tab-panel-4799)
    -   [Java](#tab-panel-4800)
    -   [Python](#tab-panel-4801)
    
    ```java
    // Query via the expression index to fetch records for customers that are 25 and older
    
    // A `where` on the index's expression is served by the index; `forIndex` names it.
    
    String ael = "when ($.age >= 18 and ($.country == 'Australia'"
    
        + " or $.country == 'Canada' or $.country == 'Botswana')"
    
        + " => $.age, default => unknown)";
    
    try (RecordStream rs = session.query(DataSet.of("test", "cust_data"))
    
            .where(ael + " >= 25")
    
            .withHint(hint -> hint.forIndex("cust_index"))
    
            .execute()) {
    
        rs.forEach(result -> System.out.println(result.recordOrThrow().bins));
    
    }
    ```
    
    ```python
    from aerospike_sdk import DataSet, QueryHint
    
    # Query via the expression index to fetch records for customers that are 25 and older
    
    # A `where` on the index's expression is served by the index; the hint names it.
    
    ael = (
    
        "when ($.age >= 18 and ($.country == 'Australia'"
    
        " or $.country == 'Canada' or $.country == 'Botswana')"
    
        " => $.age, default => unknown)"
    
    )
    
    stream = (
    
        session.query(DataSet.of("test", "cust_data"))
    
        .where(ael + " >= 25")
    
        .with_hint(QueryHint(index_name="cust_index"))
    
        .execute()
    
    )
    
    for row in stream:
    
        print(row.record_or_raise().bins)
    
    stream.close()
    ```
    
    ```rust
    use aerospike::query::{Filter, PartitionFilter};
    
    use aerospike::{Bins, QueryPolicy, Statement};
    
    use futures::StreamExt;
    
    // Query via the expression index to fetch records for customers that are 25 and older
    
    let mut stmt = Statement::new("test", "cust_data", Bins::All);
    
    // Use `range_by_index` to query the expression index
    
    stmt.add_filter(Filter::range_by_index("cust_index", 25, i64::MAX));
    
    let rs = client.query(&QueryPolicy::default(), PartitionFilter::all(), stmt).await?;
    
    let mut stream = rs.into_stream();
    
    while let Some(record) = stream.next().await {
    
        println!("{:?}", record?.bins);
    
    }
    ```
    
    ```c#
    // Query via the expression index to fetch records for customers that are 25 and older
    
    Statement stmt = new();
    
    stmt.SetNamespace("test");
    
    stmt.SetSetName("cust_data");
    
    // Update the range to start at 25
    
    stmt.SetFilter(Filter.RangeByIndex("cust_index", 25, int.MaxValue));
    
    // Execute the query and print the results
    
    using RecordSet recordSet = client.Query(null, stmt);
    
    while (recordSet.Next())
    
    {
    
        Console.WriteLine(recordSet.Record);
    
    }
    ```
    
    ```go
    // Requires: import as "github.com/aerospike/aerospike-client-go/v8"
    
    // Query via the expression index to fetch records for customers that are 25 and older
    
    stmt := as.NewStatement("test", "cust_data")
    
    // Update the range to start at 25
    
    stmt.SetFilter(as.NewRangeWithIndexNameFilter("cust_index", 25, math.MaxInt64))
    
    rs, err := client.Query(nil, stmt)
    
    if err != nil {
    
      panic(err)
    
    }
    
    for rec := range rs.Results() {
    
      if rec.Err != nil {
    
        fmt.Printf("error: %v\n", rec.Err)
    
        continue
    
      }
    
      fmt.Println(rec.Record.Bins)
    
    }
    ```
    
    ```js
    import Aerospike from "aerospike";
    
    const filter = Aerospike.filter;
    
    // Define the namespace and set for the records
    
    const NAMESPACE = "test";
    
    const SET = "cust_data";
    
    const client = await Aerospike.connect({
    
      hosts: "127.0.0.1:3000",
    
    });
    
    // Create a query object
    
    const query = client.query(NAMESPACE, SET);
    
    // Query via the expression index to fetch records for customers that are 25 and older
    
    query.whereWithIndexName(
    
      filter.range(null, 25, Number.MAX_SAFE_INTEGER),
    
      "cust_index",
    
    );
    
    // Execute the query and print the results
    
    const results = await query.results();
    
    for (const result of results) {
    
      console.log(result.bins);
    
    }
    
    await client.close();
    ```
    
    ```c
    // Query via the expression index to fetch records for customers that are 25 and older
    
    static void
    
    do_query_by_age_at_least_25(aerospike* as)
    
    {
    
        as_query query;
    
        as_query_init(&query, "test", "cust_data");
    
        as_query_where_init(&query, 1);
    
        as_query_where_with_index_name(&query, "cust_index",
    
                                       as_integer_range(25, INT_MAX));
    
        as_error err;
    
        if (aerospike_query_foreach(as, &err, NULL, &query, query_cb, NULL) !=
    
                AEROSPIKE_OK) {
    
            printf("aerospike_query_foreach() returned %d - %s\n", err.code, err.message);
    
        }
    
        as_query_destroy(&query);
    
    }
    ```
    
    ```java
    // Query via the expression index to fetch records for customers that are 25 and older
    
    Statement stmt = new Statement();
    
    stmt.setNamespace("test");
    
    stmt.setSetName("cust_data");
    
    // Update the range to start at 25
    
    stmt.setFilter(Filter.rangeByIndex("cust_index", 25, Integer.MAX_VALUE));
    
    try (RecordSet recordSet = client.query(null, stmt)) {
    
        while (recordSet.next()) {
    
            System.out.println(recordSet.getRecord());
    
        }
    
    }
    ```
    
    ```python
    import sys
    
    import aerospike
    
    from aerospike import predicates as p
    
    # Connect to the database
    
    config = {"hosts": [("127.0.0.1", 3000)]}
    
    client = aerospike.client(config).connect()
    
    # Query via the expression index to fetch records for customers that are 25 and older
    
    q = client.query("test", "cust_data")
    
    # Create a query and apply the new range
    
    q.where_with_index_name("cust_index",
    
                p.range(None, aerospike.INDEX_TYPE_DEFAULT, 25, sys.maxsize))
    
    # Define a function to handle each record returned by the query
    
    def handle(record):
    
        key, meta, bins = record
    
        print(bins)
    
    # Execute the query and apply the handler to each record
    
    q.foreach(handle)
    
    client.close()
    ```
    
6.  That’s it. You’re done!