Typesense
A Rust client library for the Typesense API.
This library provides an ergonomic interface for interacting with Typesense. It supports multi-node configuration and WebAssembly.
Examples
The following examples demonstrate how to define a collection schema using the Typesense derive macro and create it on the server.
Native (Tokio)
This example shows the typical setup for a server-side application using the Tokio runtime. It includes features like connection timeouts and automatic request retries.
#[cfg(not(target_family = "wasm"))]
{
use serde::{Deserialize, Serialize};
use typesense::{Client, Typesense, ExponentialBackoff, prelude::*};
use std::time::Duration;
/// A struct representing a company document.
#[derive(Typesense, Serialize, Deserialize, Debug)]
#[typesense(
collection_name = "companies",
default_sorting_field = "num_employees"
)]
struct Company {
company_name: String,
num_employees: i32,
#[typesense(facet)]
country: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::builder()
.nodes(vec!["http://localhost:8108"])
.api_key("xyz")
.healthcheck_interval(Duration::from_secs(60))
.retry_policy(ExponentialBackoff::builder().build_with_max_retries(3))
.build()?;
// Create the collection in Typesense
let collection = client
.collections()
.create(Company::collection_schema())
.await?;
println!("Created collection: {:?}", collection);
Ok(())
}
}
WebAssembly (Wasm)
This example is tailored for a WebAssembly target.
Key difference: Tokio-dependent features like .retry_policy() are disabled.
#[cfg(target_family = "wasm")]
{
use serde::{Deserialize, Serialize};
use typesense::{Client, Typesense, prelude::*};
use std::time::Duration;
use wasm_bindgen_futures::spawn_local;
/// A struct representing a company document.
#[derive(Typesense, Serialize, Deserialize, Debug)]
#[typesense(
collection_name = "companies",
default_sorting_field = "num_employees"
)]
struct Company {
company_name: String,
num_employees: i32,
#[typesense(facet)]
country: String,
}
fn main() {
spawn_local(async {
let client = Client::builder()
.nodes(vec!["http://localhost:8108"])
.api_key("xyz")
.healthcheck_interval(Duration::from_secs(60))
// .retry_policy(...) <-- disabled in Wasm
.build()
.unwrap();
// Create the collection in Typesense
match client.collections().create(Company::collection_schema()).await {
Ok(collection) => println!("Created collection: {:?}", collection),
Err(e) => eprintln!("Error creating collection: {}", e),
}
});
}
}