Skip to content

Quickstart: path expressions

This page contains a quickstart guide for path expressions.

Path expressions enable granular indexing and querying from a nested map or list collection data type (CDT) using expressions.

Prerequisites

Code Demo

All the Java code mentioned in our path expressions documentation is available in GitHub. You can clone, explore, run and modify it, or just read along.

  1. Verify the server version.

    Verify that the Aerospike server is running with the asadm command:

    Terminal window
    # connect to your Aerospike Database server on the port it is listening on
    asadm -p 3000

    Expected output similar to:

    Seed: [('127.0.0.1', 3000, None)]
    Config_file: /Users/dbuser/.aerospike/astools.conf, /etc/aerospike/astools.conf
    Aerospike Interactive Shell, version 2.12.0
    Found 1 nodes
    Online: 127.0.0.1:3000
    Admin> info

    The Build should be version >= 8.1.2.0

    Exit asadm with the command exit.

  2. Download the demo code.

    Use the following command to clone the app from GitHub:

    Terminal window
    git clone https://github.com/aerospike-examples/path-expressions-java-preview.git
    cd path-expressions-java-preview

    There are four folders:

    • bookstore/ has the code used in the overview page
    • catalog/ has the code used in this quickstart page and advanced usage
    • booking/ has the code used in the performance page
    • misc/ has miscellaneous code examples for path expression use, such as chaining mapKeysIn/andFilter pairs
  3. Build the application with Maven.

    Back at the shell command prompt, go into the folder you want to use and run the following command:

    Terminal window
    mvn compile exec:java

Catalog example

This example uses a simplified ecommerce product catalog stored as a single record in Aerospike. Products are modeled as a map and organized under a single catalog map bin, keyed by product IDs (e.g. 10000001, 50000009).

{
"inventory": {
"10000001": {
"category": "clothing",
"featured": true,
"name": "Classic T-Shirt",
"description": "A lightweight cotton T-shirt perfect for everyday wear.",
"variants": {
"2001": { "size": "S", "price": 25, "quantity": 100 },
"2002": { "size": "M", "price": 25, "quantity": 0 },
"2003": { "size": "L", "price": 27, "quantity": 50 }
}
},
"10000002": {
"category": "clothing",
"featured": false,
"name": "Casual Polo Shirt",
"description": "A soft polo shirt suitable for work or leisure.",
"variants": {
"2004": { "size": "M", "price": 30, "quantity": 20 },
"2005": { "size": "XL", "price": 32, "quantity": 10 }
}
},
"50000006": {
"category": "electronics",
"featured": true,
"name": "Laptop Pro 14",
"description": "High-performance laptop designed for professionals.",
"variants": {
"3001": { "spec": "8GB RAM", "price": 599, "quantity": 0 }
}
},
"50000009": {
"category": "electronics",
"featured": true,
"name": "Smart TV",
"description": "Ultra HD smart television with built-in streaming apps.",
"variants": [
{ "sku": 3007, "spec": "1080p", "price": 199, "quantity": 60 },
{ "sku": 3008, "spec": "4K", "price": 399, "quantity": 30 }
]
}
}
}

Filtering dimensions

catalog (bin)
└── inventory (map of 4 maps)
└── <product ID>
├── category
├── featured <-- product-level filter
├── name
├── description
└── variants
├── { "2001": {...}, "2002": {...} } <-- map-backed variants
└── [ {sku:3007,...}, {sku:3008,...} ] <-- list-backed variants
^ variant-level filter
  • Inventory level: select inventory (map key navigation)
  • Product level: keyed by the product ID, filter on the featured field inside each product map
  • Variant level: filter by the quantity field inside either:
    • a map-backed variant (keyed by SKU), or
    • a list-backed variant (list of map structs)

Query the catalog example above, using two filter expressions and combine them with traversal contexts to select featured products with in-stock variants:

// Product-level filter: featured == true
Exp filterOnFeatured = Exp.eq(
MapExp.getByKey(
MapReturnType.VALUE, Type.BOOL,
Exp.val("featured"),
Exp.mapLoopVar(LoopVarPart.VALUE) // during path resolution the value of the current element is assigned to a map-typed loop variable
),
Exp.val(true)
);
// Variant-level filter: quantity > 0
Exp filterOnVariantInventory = Exp.gt(
MapExp.getByKey(
MapReturnType.VALUE, Type.INT,
Exp.val("quantity"),
Exp.mapLoopVar(LoopVarPart.VALUE) // during path resolution the value of the current element is assigned to a map-typed loop variable
),
Exp.val(0)
);
// Path expression: featured products with in-stock variants
Record record = client.operate(null, key,
CdtOperation.selectByPath("catalog", SelectFlags.MATCHING_TREE,
CTX.allChildren(), // dive into all products
CTX.allChildrenWithFilter(filterOnFeatured), // only featured products
CTX.mapKey(Value.get("variants")), // descend into variants
CTX.allChildrenWithFilter(filterOnVariantInventory)) // only in-stock
);

Output:

{
"inventory" : {
"10000001" : {
"variants" : {
"2001" : {
"size" : "S",
"price" : 25,
"quantity" : 100
},
"2003" : {
"size" : "L",
"price" : 27,
"quantity" : 50
}
}
},
"50000009" : {
"variants" : [ {
"quantity" : 60,
"sku" : 3007,
"price" : 199,
"spec" : "1080p"
}, {
"quantity" : 30,
"sku" : 3008,
"price" : 399,
"spec" : "4K"
} ]
},
"50000006" : {
"variants" : { }
}
}
}
  • ✅ Item 50000009 keeps both variants.
  • ✅ Item 10000001, Classic T-Shirt, keeps variant items 2001 and 2003 (both have quantity > 0).
  • ❌ Item 10000002, Casual Polo Shirt, excluded (featured = false).
  • ❌ Variant 2002 excluded from item 10000001 (quantity = 0).
  • ❌ Item 50000006 excluded, variant 3001 (quantity = 0).

Feature summary

With path expressions you can:

  • traverse all elements of a nested map or list collection data type, and use filter expressions to match and select elements at each layer.

  • Retrieve only relevant subtrees (products + in-stock variants).

  • Avoid denormalization and client-side filtering.

  • Build faster, cleaner APIs for real-world use cases like product catalogs.

Next steps

Explore additional code examples and use cases on the Advanced usage page.

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?