Skip to main content

Query API Reference

The Query API provides a flexible way to search for memories based on multiple criteria. This document provides a detailed reference for the query-related APIs in EngramDB.

QueryBuilder

The QueryBuilder class provides a fluent interface for building complex queries.

Creating a Query

QueryBuilder::new()

Creates a new query builder. Returns:
  • A new QueryBuilder instance
Example:

Query Criteria

with_vector(query_vector)

Sets the query vector for similarity search. Parameters:
  • query_vector: Vec<f32> - The query vector
Returns:
  • Self - The query builder for method chaining
Example:

with_similarity_threshold(threshold)

Sets the minimum similarity threshold for vector search. Parameters:
  • threshold: f32 - Minimum similarity threshold (0.0 to 1.0)
Returns:
  • Self - The query builder for method chaining
Example:

with_attribute_filter(filter)

Adds an attribute filter to the query. Parameters:
  • filter: AttributeFilter - The attribute filter to add
Returns:
  • Self - The query builder for method chaining
Example:

with_temporal_filter(filter)

Adds a temporal filter to the query. Parameters:
  • filter: TemporalFilter - The temporal filter to add
Returns:
  • Self - The query builder for method chaining
Example:

with_limit(limit)

Sets the maximum number of results to return. Parameters:
  • limit: usize - Maximum number of results
Returns:
  • Self - The query builder for method chaining
Example:

with_exclude_ids(ids)

Adds IDs to exclude from the results. Parameters:
  • ids: Vec<Uuid> - IDs to exclude
Returns:
  • Self - The query builder for method chaining
Example:

Executing the Query

execute(vector_index, memory_nodes)

Executes the query against a vector index and a set of memory nodes. Parameters:
  • vector_index: &VectorIndex - The vector index to search
  • memory_nodes: F - A function that can retrieve memory nodes by ID
Returns:
  • Result<Vec<MemoryNode>> - A vector of memory nodes matching the query, sorted by relevance
Example:

AttributeFilter

The AttributeFilter class provides methods for filtering memories based on their attributes.

Creating Filters

AttributeFilter::equals(key, value)

Creates a filter that matches attributes equal to the specified value. Parameters:
  • key: &str - The attribute key
  • value: AttributeValue - The value to compare against
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::not_equals(key, value)

Creates a filter that matches attributes not equal to the specified value. Parameters:
  • key: &str - The attribute key
  • value: AttributeValue - The value to compare against
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::greater_than(key, value)

Creates a filter that matches numeric attributes greater than the specified value. Parameters:
  • key: &str - The attribute key
  • value: AttributeValue - The value to compare against (must be numeric)
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::less_than(key, value)

Creates a filter that matches numeric attributes less than the specified value. Parameters:
  • key: &str - The attribute key
  • value: AttributeValue - The value to compare against (must be numeric)
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::contains(key, substring)

Creates a filter that matches string attributes containing the specified substring. Parameters:
  • key: &str - The attribute key
  • substring: &str - The substring to search for
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::exists(key)

Creates a filter that matches memories where the specified attribute exists. Parameters:
  • key: &str - The attribute key
Returns:
  • A new AttributeFilter instance
Example:

AttributeFilter::not_exists(key)

Creates a filter that matches memories where the specified attribute does not exist. Parameters:
  • key: &str - The attribute key
Returns:
  • A new AttributeFilter instance
Example:

TemporalFilter

The TemporalFilter class provides methods for filtering memories based on their temporal properties.

Creating Filters

TemporalFilter::before(timestamp)

Creates a filter that matches memories created before the specified timestamp. Parameters:
  • timestamp: u64 - The Unix timestamp
Returns:
  • A new TemporalFilter instance
Example:

TemporalFilter::after(timestamp)

Creates a filter that matches memories created after the specified timestamp. Parameters:
  • timestamp: u64 - The Unix timestamp
Returns:
  • A new TemporalFilter instance
Example:

TemporalFilter::between(start_timestamp, end_timestamp)

Creates a filter that matches memories created between the specified timestamps. Parameters:
  • start_timestamp: u64 - The start Unix timestamp
  • end_timestamp: u64 - The end Unix timestamp
Returns:
  • A new TemporalFilter instance
Example:

TemporalFilter::within_last(seconds)

Creates a filter that matches memories created within the last specified number of seconds. Parameters:
  • seconds: u64 - Number of seconds
Returns:
  • A new TemporalFilter instance
Example:

Combined Query Example

Here’s an example of a complex query that combines multiple criteria: