Search
RushDB provides a powerful and flexible search system that allows you to efficiently query and traverse your graph data. The Search API is a cornerstone of RushDB, enabling you to find records, filter by conditions, navigate relationships, compute metrics, and format the returned data exactly as needed. The legacy aggregate clause is deprecated and should only be used for vector similarity until select supports it.
Core Capabilities
RushDB's Search API offers a comprehensive set of features:
- Powerful Filtering: Use the
whereclause with a wide range of operators to precisely filter records - Graph Traversal: Navigate through connected records with relationship queries
- Select Expressions: Compute aggregates, derive metrics, and shape output using the
selectclause - Pagination and Sorting: Control result volume and order with pagination and sorting options
- Label-Based Filtering: Target specific types of records using label filtering
- Semantic Search: Find records by meaning using AI embedding indexes — see AI Search
SearchQuery Structure
All search operations in RushDB use a consistent SearchQuery data structure:
interface SearchQuery {
labels?: string[]; // Filter by record labels
where?: WhereCondition; // Filter by property values and relationships
limit?: number; // Maximum number of records to return (default: 100)
skip?: number; // Number of records to skip (for pagination)
orderBy?: OrderByClause; // Sorting configuration
select?: SelectExprMap; // Output-shaping expressions
}
Basic Example
Here's a simple example of searching for products:
db.records.find({
labels: ["PRODUCT"],
where: {
title: { $contains: "Sneakers" },
SIZE: {
uk: { $gte: 8, $lte: 9 },
qty: { $gt: 0 }
}
},
limit: 20,
orderBy: { price: 'asc' }
})
This query:
- Searches for records with the
PRODUCTlabel - Filters for products with "Sneakers" in the title
- Finds only those with UK sizes 8-9 that are in stock
- Limits results to 20 records
- Sorts results by price in ascending order
Advanced Search Features
Filtering with Where Clauses
The where clause is the primary mechanism for filtering records. It supports:
- Property Matching: Filter by exact values, string patterns, numeric ranges, etc.
- Logical Operators: Combine conditions with AND, OR, NOT, etc.
- Relationship Traversal: Filter based on properties of related records
{
where: {
category: "Electronics",
price: { $gte: 100, $lte: 500 },
$or: [
{ inStock: true },
{ preorderAvailable: true }
]
}
}
Aggregating Results
Select Expressions allow you to perform calculations and transform the structure of your results:
{
labels: ["ORDER"],
where: {
PRODUCT: {
$alias: "$product",
category: "Electronics"
}
},
select: {
totalSpent: { $sum: "$record.amount" },
products: { $collect: { from: "$product" } }
}
}
Pagination and Sorting
Control the volume and order of results using pagination and sorting options:
{
labels: ["CUSTOMER"],
limit: 50, // Return up to 50 records
skip: 100, // Skip the first 100 records
orderBy: {
lastName: "asc", // Sort by lastName ascending
firstName: "asc" // Then by firstName ascending
}
}
Performance Best Practices
For optimal performance when using the Search API:
- Be Specific: Filter by labels when possible to narrow the search scope
- Use Indexed Properties: Prioritize filtering on properties that have indexes
- Limit Results: Use pagination to retrieve only the records you need
- Optimize Queries: Avoid deep relationship traversals when possible
- Use Aliases Efficiently: Define aliases only for records you need to reference in aggregations
Next Steps
- Learn more about filtering with where clauses
- Explore select expressions and aggregation capabilities
- Understand pagination and sorting options
- Discover how to filter by record labels