Tutorial for Expression Indexes
This tutorial introduces expression indexes in Aerospike Database. This tutorial assumes you are familiar with secondary indexes and 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 against it
- Compare expression indexes against traditional secondary indexes
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
- Updated Client (minimum versions):
- Java: v9.1.0+
- Python: v17.1.0+
- C: v7.1.0+
- C#: v8.1.0+
- Go: v8.4.0+
- Node.js: v6.3.0+
- Rust: not available
- PHP: not available
Verify your server version using asinfo:
# Verify you’re running Aerospike 8.1.0+ asinfo -v buildCreate expression, index, and query
-
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
asadmcommand-line tool to create the index. This approach separates application logic (defining the filter) from database administration (creating the index).// Build an expression that indexes age only for adults in AU/CA/BW that are 18 or olderExpression filterExp = Exp.build(Exp.cond(Exp.and(Exp.ge(// Is the age 18 or older?Exp.intBin("age"),Exp.val(18)),ListExp.getByValue( // Do they live in a target country?ListReturnType.EXISTS,Exp.stringBin("country"),Exp.val(List.of("Australia", "Canada", "Botswana")))),Exp.intBin("age"), // If true, return the age of the customer to be indexedExp.unknown() // returns "unknown" to exclude the record from the index));System.out.println(filterExp.getBase64());// Prints: lHuTEJMEk1ECo2FnZRKVfwEAkxYNk1EDp2NvdW50cnmSfpOqA0F1c3RyYWxpYacDQ2FuYWRhqQNCb3Rzd2FuYZNRAqNhZ2WRAA==# pip install aerospikeimport aerospikefrom aerospike_helpers import expressions as exptarget_countries = ["Australia", "Canada", "Botswana"]# Build the "OR" condition for the country checkcountry_is_target = exp.Or(*[exp.Eq(exp.StrBin("country"), c) for c in target_countries])# Compile the final expressionfilter_exp = exp.Cond(# IF block: Both conditions must be trueexp.And(exp.GE(exp.IntBin("age"), 18),country_is_target),# Return the value of the 'age' bin for indexingexp.IntBin("age"),# ELSE Return "unknown" to exclude the record from the indexexp.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: lHuTEJMEk1ECo2FnZRKVfwEAkxYNk1EDp2NvdW50cnmSfpOqA0F1c3RyYWxpYacDQ2FuYWRhqQNCb3Rzd2FuYZNRAqNhZ2WRAA==client.close()// Build an expression that indexes age only for adults in AU/CA/BWExpression filterExp = Exp.Build(// The `Cond` expression is the top-level IF/THEN/ELSEExp.Cond(// IF block: Defines the conditions for the indexExp.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?ListExp.GetByValue(ListReturnType.EXISTS, // Check for existenceExp.StringBin("country"),Exp.Val(new[] { "Australia", "Canada", "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 asadmstring exprBase64 = filterExp.GetBase64();Console.WriteLine("Expression Base64:");Console.WriteLine(exprBase64);// Prints: lHuTEJMEk1ECo2FnZRKVfwEAkxYNk1EDp2NvdW50cnmSfpOqA0F1c3RyYWxpYacDQ2FuYWRhqQNCb3Rzd2FuYZNRAqNhZ2WRAA==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 binas_exp_bin_int("age"),// return unknown to exclude value from indexas_exp_unknown()));LOG("Expression base64 = %s", as_exp_to_base64(filter_exp));// Prints:// Expression base64 = lHuTEJMEk1ECo2FnZRKUEZMBk1EDp2NvdW50cnmqA0F1c3RyYWxpYZMBk1EDp2NvdW50cnmnA0NhbmFkYZMBk1EDp2NvdW50cnmpA0JvdHN3YW5hk1ECo2FnZZEAAll the client examples above print similar Base64-encoded expression strings to the console.
-
Create the expression index.
Use the
asadmcommand-line tool to create a numeric secondary index on the expression. Copy the full Base64 string from the previous step’s output and use it in the command below.Terminal window asadm -e "enable; manage sindex create numeric cust_index ns test set cust_data exp_base64 lHuTEJMEk1ECo2FnZRKVfwEAkxYNk1EDp2NvdW50cnmSfpOqA0F1c3RyYWxpYacDQ2FuYWRhqQNCb3Rzd2FuYZNRAqNhZ2WRAA==" -
Insert some test data.
// Define the namespace and set for the recordsfinal String NAMESPACE = "test";final String SET = "cust_data";// A helper method to insert a customer recordpublic 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 indexedpublic void insertData() {insert(1, "Tim", 312, "Australia");insert(2, "Bob", 47, "Canada");insert(3, "Jo", 15, "USA"); // not indexedinsert(4, "Steven", 23, "Botswana");insert(5, "Susan", 32, "Canada");insert(6, "Jess", 17, "Botswana"); // not indexedinsert(7, "Sam", 18, "USA"); // not indexedinsert(8, "Alex", 47, "Canada");insert(9, "Pam", 56, "Australia");insert(10, "Vivek", 12, "India"); // not indexedinsert(11, "Kiril", 22, "Sweden"); // not indexedinsert(12, "Bill", 23, "UK"); // not indexed}import aerospike# Sample customer datacustomers = [(1, "Tim", 312, "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()# Inserts datafor key, name, age, country in customers:client.put(("test", "cust_data", key),{"name": name,"age": age,"country": country})client.close()// Define the namespace and set for the recordsconst string NAMESPACE = "test";const string SET = "cust_data";// A helper method to insert a customer recordpublic 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 indexedpublic void InsertData(){Insert(1, "Tim", 312, "Australia");Insert(2, "Bob", 47, "Canada");Insert(3, "Jo", 15, "USA"); // not indexedInsert(4, "Steven", 23, "Botswana");Insert(5, "Susan", 32, "Canada");Insert(6, "Jess", 17, "Botswana"); // not indexedInsert(7, "Sam", 18, "USA"); // not indexedInsert(8, "Alex", 47, "Canada");Insert(9, "Pam", 56, "Australia");Insert(10, "Vivek", 12, "India"); // not indexedInsert(11, "Kiril", 22, "Sweden"); // not indexedInsert(12, "Bill", 23, "UK"); // not indexed}static voiddo_insert_data(aerospike* as){insert(as, 1, "Tim", 312, "Australia");insert(as, 2, "Bob", 47, "Canada");insert(as, 3, "Jo", 15, "USA"); // not indexedinsert(as, 4, "Steven", 23, "Botswana");insert(as, 5, "Susan", 32, "Canada");insert(as, 6, "Jess", 17, "Botswana"); // not indexedinsert(as, 7, "Sam", 18, "USA"); // not indexedinsert(as, 8, "Alex", 47, "Canada");insert(as, 9, "Pam", 56, "Australia");insert(as, 10, "Vivek", 12, "India"); // not indexedinsert(as, 11, "Kiril", 22, "Sweden"); // not indexedinsert(as, 12, "Bill", 23, "UK"); // not indexed}extern char* g_namespace;extern char* g_set;static voidinsert(aerospike* as, int key, const char* name, int age, const char* country){as_error err;as_error_init(&err);as_key int_key;as_key_init_int64(&int_key, g_namespace, g_set, 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) {LOG("aerospike_key_put() returned %d - %s", err.code, err.message);example_cleanup(as);exit(-1);}as_record_destroy(&rec);} -
Validate index entries.
You should see 6 entries in the expression index (records 1, 2, 4, 5, 8, 9).
Terminal window # Confirm only matching records (6) were indexedasinfo -v 'sindex-stat:namespace=test;indexname=cust_index' -lentries=6... -
Query your Expression Index.
Run a query to filter for ages 5 and above. Notice the results still only shows ages 18 and above.
// Query via the expression index to fetch records for customers that are 5 and olderStatement stmt = new Statement();stmt.setNamespace("test");stmt.setSetName("cust_data");// Use `rangeByIndex` to query the expression indexstmt.setFilter(Filter.rangeByIndex("cust_index", 5, Integer.MAX_VALUE));// Execute the query and print the resultstry (RecordSet recordSet = client.query(null, stmt)) {while (recordSet.next()) {System.out.println(recordSet.getRecord());}}import sysimport aerospikefrom aerospike import predicates as p# Connect to the databaseconfig = {"hosts": [("127.0.0.1", 3000)]}client = aerospike.client(config).connect()# Create a query objectq = client.query("test", "cust_data")# Set the query to use the expression index with a range filterq.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 querydef handle(record):key, meta, bins = recordprint(bins)# Execute the query and apply the handler to each recordq.foreach(handle)client.close()// Query via the expression index to fetch records for customers that are 5 and olderStatement stmt = new();stmt.SetNamespace("test");stmt.SetSetName("cust_data");// Use `RangeByIndex` to query the expression indexstmt.SetFilter(Filter.RangeByIndex("cust_index", 5, int.MaxValue));// Execute the query and print the resultstry{using RecordSet recordSet = client.Query(null, stmt);while (recordSet.Next()){Console.WriteLine(recordSet.Record);}}static bool query_cb(const as_val*, void*);static bool print_bin_cb(const char*, const as_val*, void*);static voiddo_query_by_age_at_least_5(aerospike* as){as_query query;as_query_init(&query, g_namespace, g_set);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) {LOG("aerospike_query_foreach() returned %d - %s", err.code, err.message);example_cleanup(as);exit(-1);}example_cleanup(&as);return 0;}static boolquery_cb(const as_val* val, void* udata) {if (! val) {// Query is complete.return false;}as_record* rec = as_record_fromval(val);as_record_foreach(rec, print_bin_cb, NULL);printf("\n");return true;}static boolprint_bin_cb(const char* name, const as_val* val, void* udata) {printf("%s=", name);uint64_t bin_type = as_val_type(val);switch (bin_type) {case AS_STRING:printf("\"%s\", ", as_string_get(as_string_fromval(val)));return true;case AS_INTEGER:printf("%lld, ", as_integer_get(as_integer_fromval(val)));return true;default:LOG("print_bin_cb: unknown type %llu for bin named %s", bin_type, name);return false;}}Since we stored age, you can filter for records above 25 by simply modifying the query:
// Query via the expression index to fetch records for customers that are 25 and olderStatement stmt = new Statement();stmt.setNamespace("test");stmt.setSetName("cust_data");// Update the range to start at 25stmt.setFilter(Filter.rangeByIndex("cust_index", 25, Integer.MAX_VALUE));try (RecordSet recordSet = client.query(null, stmt)) {while (recordSet.next()) {System.out.println(recordSet.getRecord());}}import sysimport aerospikefrom aerospike import predicates as pconfig = {"hosts": [("127.0.0.1", 3000)]}client = aerospike.client(config).connect()# Create a query and apply the new rangeq = client.query("test", "cust_data")q.where_with_index_name("cust_index",p.range(None, aerospike.INDEX_TYPE_DEFAULT, 25, sys.maxsize))def handle(record):key, meta, bins = recordprint(bins)q.foreach(handle)client.close()// Query via the expression index to fetch records for customers that are 25 and olderStatement stmt = new();stmt.SetNamespace("test");stmt.SetSetName("cust_data");// Update the range to start at 25stmt.SetFilter(Filter.RangeByIndex("cust_index", 25, int.MaxValue));// Execute the query and print the resultstry{using RecordSet recordSet = client.Query(null, stmt);while (recordSet.Next()){Console.WriteLine(recordSet.Record);}}static voiddo_query_by_age_at_least_25(aerospike* as){as_query query;as_query_init(&query, g_namespace, g_set);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) {LOG("aerospike_query_foreach() returned %d - %s", err.code, err.message);example_cleanup(as);exit(-1);}} -
That’s it. You’re done!
Have questions or feedback? You can submit them or schedule a call with the product team using this form.