HeroIndex Client
A Rust client library for HeroIndex, a high-performance full-text search server built on Tantivy.
Need the server? Install it with
cargo install heroindexor see heroindex on crates.io
Features
- Async/Await - Built on Tokio for async operations
- Type-Safe - Strongly typed responses
- Simple API - Intuitive method names matching RPC calls
- 10+ Field Types - text, str, u64, i64, f64, date, bool, json, bytes, ip
- 8 Query Types - match, term, fuzzy, phrase, prefix, range, regex, boolean
- Batch Operations - Efficient bulk document insertion
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
= "1"
Quick Start
use HeroIndexClient;
use json;
async
Field Types
When creating a schema, you can use these field types:
| Type | Description | Example Value | Use Case |
|---|---|---|---|
text |
Full-text searchable, tokenized | "Hello World" |
Articles, descriptions |
str |
Exact match keyword, not tokenized | "user-123" |
IDs, tags, status |
u64 |
Unsigned 64-bit integer | 42 |
Counts, ages |
i64 |
Signed 64-bit integer | -10 |
Scores, offsets |
f64 |
64-bit floating point | 3.14 |
Prices, ratings |
date |
DateTime (RFC 3339 format) | "2024-01-15T10:30:00Z" |
Timestamps |
bool |
Boolean | true |
Flags, toggles |
json |
Nested JSON object | {"key": "value"} |
Metadata, attributes |
bytes |
Binary data (base64) | "SGVsbG8=" |
Hashes, binary |
ip |
IP address | "192.168.1.1" |
Network logs |
Field Options
Each field can have these options:
stored: true- Store the value to retrieve it in search resultsindexed: true- Index the field to make it searchablefast: true- Enable fast fields for sorting and aggregations (numeric types)tokenizer: "en_stem"- Use stemming tokenizer for text fields
Schema Example
client.db_create.await?;
Query Types
1. Match Query (Full-Text Search)
Tokenizes the query and finds documents containing any of the terms.
// Simple full-text search
client.search.await?;
2. Term Query (Exact Match)
Finds documents with the exact value (no tokenization). Best for keyword fields.
// Find by exact ID
client.search.await?;
// Find by status
client.search.await?;
3. Fuzzy Query (Typo-Tolerant)
Finds documents even with spelling mistakes. Distance is the max number of character edits.
// Finds "keyboard" even when user types "keybaord"
client.search.await?;
// Finds "smartphone" from "smartfone"
client.search.await?;
4. Phrase Query (Exact Phrase Match)
Finds documents containing the exact phrase in order.
// Must contain "machine learning" as an exact phrase
client.search.await?;
5. Prefix Query (Autocomplete)
Finds documents where the field starts with the given prefix.
// Autocomplete: find all products starting with "wire"
client.search.await?;
6. Range Query (Numeric/Date Ranges)
Finds documents within a numeric or date range.
// Products between $50 and $100
client.search.await?;
// Products with at least 10 in stock
client.search.await?;
// All options: gt, gte, lt, lte
client.search.await?;
7. Regex Query (Pattern Matching)
Finds documents matching a regular expression.
// Find product codes matching pattern
client.search.await?;
// Find emails from specific domain
client.search.await?;
8. Boolean Query (Complex Combinations)
Combines multiple queries with AND/OR/NOT logic.
// Complex search: electronics that are premium but not discontinued
client.search.await?;
9. All Query (Match Everything)
Returns all documents in the index.
// Get all documents
client.search.await?;
Real-World Query Examples
E-commerce Product Search
// User searches "wireless mouse" - combine fuzzy for typo tolerance with filters
client.search.await?;
Log Search
// Find error logs from the last hour
client.search.await?;
Article Search with Pagination
// Search articles, page 3 (20 results per page)
let page = 3;
let per_page = 20;
let offset = * per_page;
let results = client.search.await?;
println!;
Document Operations
Adding Documents
// Single document
client.doc_add.await?;
// Batch insert (much faster for bulk operations)
client.doc_add_batch.await?;
// IMPORTANT: Commit and reload to make documents searchable
client.commit.await?;
client.reload.await?;
Deleting Documents
// Delete by field value
client.doc_delete.await?;
client.commit.await?;
client.reload.await?;
Database Management
// List all databases
let list = client.db_list.await?;
for db in &list.databases
// Create database
client.db_create.await?;
// Select database (required before operations)
client.db_select.await?;
// Get database info
let info = client.db_info.await?;
println!;
// Delete database
client.db_delete.await?;
Error Handling
use ;
match client.db_select.await
Response Types
All methods return strongly-typed responses:
| Type | Fields |
|---|---|
PingResponse |
status, version |
ServerStats |
uptime_secs, databases, total_docs |
DatabaseList |
databases: Vec<DatabaseInfo> |
DatabaseInfo |
name, doc_count, size_bytes, segment_count |
SchemaInfo |
fields: Vec<FieldInfo> |
SearchResult |
total_hits, hits: Vec<SearchHit>, took_ms |
SearchHit |
score, doc: serde_json::Value |
CountResult |
count |
OpResult |
success, opstamp |
Performance Tips
- Use batch inserts -
doc_add_batchis 10-100x faster than individualdoc_addcalls - Commit periodically - Don't commit after every document, batch them
- Enable fast fields - For fields used in sorting/filtering/aggregations
- Use term queries - For exact matches on keyword fields,
termis faster thanmatch - Limit results - Always set reasonable
limitvalues, fetch more with pagination
Related Crates
- heroindex - The HeroIndex server
License
MIT License