AST Query System

ChronDB uses an Abstract Syntax Tree (AST) to represent queries uniformly across all protocols (REST, Redis, SQL, CLI). This enables consistent query behavior and advanced features like pagination, sorting, and filtering.

Overview

The AST query system provides:

  • Unified query representation across all protocols

  • Advanced pagination with cursor-based navigation

  • Flexible sorting by any document field

  • Type-aware queries for numeric, temporal, and geospatial data

  • Composable query building with helper functions

AST Structure

Basic Query

(require '[chrondb.query.ast :as ast])

;; Simple FTS query
(ast/query [(ast/fts "content" "search term")]
           {:limit 10
            :offset 0
            :branch "main"})

Query Components

FTS Clause

Full-text search clause:

Sort Descriptors

Range Queries

Query Options

Usage Examples

REST API

Redis Protocol

CLI

Helper Functions

Building Queries

Complex Queries

Pagination

Offset-based Pagination

Cursor-based Pagination (searchAfter)

More efficient for large result sets:

The next-cursor is a map with :doc (document ID) and :score (relevance score) that can be serialized for API responses.

Sorting

Sort by multiple fields:

Sorting is applied in order: first by age ascending, then by name descending.

Protocol Consistency

The AST ensures that queries behave identically across all protocols:

  • REST: /api/v1/search?q=term&sort=age:asc&limit=10

  • Redis: SEARCH term SORT age:asc LIMIT 10

  • CLI: chrondb search --q term --sort age:asc --limit 10

  • Direct AST: (ast/query [(ast/fts "content" "term")] {:sort [(ast/sort-by "age" :asc)] :limit 10})

All return the same results with the same ordering and pagination.

Type Safety

The AST supports type-aware queries:

Migration Notes

The AST system replaces the legacy query format:

Legacy:

New AST:

Both are supported during migration, but new code should use AST.

Last updated

Was this helpful?