Rust Client Examples
Comprehensive examples for the hlquery Rust client library.
Example Files
The Rust client includes several example files demonstrating different aspects of the API:
- basic_usage.rs - Basic client initialization and simple operations
- collections.rs - Collection management operations
- documents.rs - Document CRUD operations
- search.rs - Various search patterns and options
Running Examples
Main Example File
The main example file (example.rs) provides a comprehensive demonstration of all API features:
# Run all examples (default)
cargo run --example example
# Run specific command
cargo run --example example cols # List collections
cargo run --example example docs my_collection # List documents in collection
cargo run --example example status # Show server health and status
cargo run --example example help # Show help message
Individual Example Files
# Basic usage
cargo run --example basic_usage
# Collections
cargo run --example collections
# Documents
cargo run --example documents
# Search
cargo run --example search
Basic Operations
Initialize Client
use hlquery_rust_client::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Basic initialization
let client = Client::new("http://localhost:9200", None)?;
// With authentication
use std::collections::HashMap;
let mut options = HashMap::new();
options.insert("token".to_string(), "your_token_here".to_string());
options.insert("auth_method".to_string(), "bearer".to_string());
let client = Client::new("http://localhost:9200", Some(options))?;
Ok(())
}
Health Check
let health = client.health().await?;
if health.is_success() {
let body = health.get_body();
println!("Status: {}", body["status"]);
}
Collection Management
Create Collection
let schema = serde_json::json!({
"fields": [
{ "name": "title", "type": "string" },
{ "name": "price", "type": "float" }
]
});
let result = client.collections().create("products", schema).await?;
if result.is_success() {
println!("Collection created");
}
Document Operations
Add Document
let product = serde_json::json!({
"id": "prod_001",
"title": "Laptop",
"price": 999.99
});
let result = client.documents().add("products", product).await?;
Bulk Import
let products = vec![
serde_json::json!({"id": "prod_001", "title": "Laptop", "price": 999.99}),
serde_json::json!({"id": "prod_002", "title": "Mouse", "price": 29.99})
];
let result = client.documents().import("products", products).await?;
Search Examples
Basic Search
use std::collections::HashMap;
let mut params = HashMap::new();
params.insert("q".to_string(), "laptop".to_string());
params.insert("query_by".to_string(), "title".to_string());
params.insert("limit".to_string(), "10".to_string());
let result = client.search("products", params).await?;
if result.is_success() {
let body = result.get_body();
if let Some(hits) = body.get("hits").and_then(|h| h.as_array()) {
for hit in hits {
if let Some(doc) = hit.get("document") {
println!("{}", doc["title"]);
}
}
}
}
Error Handling
use hlquery_rust_client::HlqueryError;
match client.search("products", params).await {
Ok(response) => {
// Handle success
}
Err(HlqueryError::AuthenticationError(msg)) => {
println!("Auth failed: {}", msg);
}
Err(e) => {
println!("Error: {}", e);
}
}
Status Command
The status command provides concise server information:
cargo run --example example status
This will show:
- Health status
- Server statistics
- Protocol codes
- Server status
- Root status response
Next Steps
- API Reference - Complete API documentation
- Main Example:
etc/api/rust/example.rs- Full example file with all features