Skip to main content

Crate searchcraft

Crate searchcraft 

Source
Expand description

§Searchcraft

An async Rust client for the Searchcraft search API.

This crate provides a typed, ergonomic interface to the Searchcraft search and management APIs. It is built on reqwest and supports both rustls (default) and native-tls backends.

§Quick start

use searchcraft::SearchcraftClient;
use searchcraft::search::query::QueryBuilder;

let client = SearchcraftClient::new(
    "https://my-instance.searchcraft.io",
    Some("sc-read-key"),
    None::<String>,
)?;

let request = QueryBuilder::fuzzy()
    .term("laptop")
    .limit(10)
    .build_request();

let response = client
    .search_index::<serde_json::Value>("products", &request)
    .await?;

println!("Found {} results", response.data.count);

§Modules

§Building queries

The QueryBuilder provides a chainable API for constructing search requests. Each method returns a new builder value, leaving the original unchanged:

use searchcraft::search::query::QueryBuilder;
use searchcraft::types::SortDirection;

let request = QueryBuilder::fuzzy()
    .term("laptop")
    .and("gaming")
    .not("refurbished")
    .order_by("price", SortDirection::Asc)
    .limit(20)
    .build_request();

§Streaming AI summaries

On engine 0.10.0+, search_summary streams an LLM-generated summary of a query’s results as Server-Sent Events. Check get_index_capabilities first — the endpoint needs AI features enabled on the index:

use searchcraft::search::types::SummaryStreamEvent;
use searchcraft::search::StreamExt;

let mut stream = client.search_summary("products", &req).await?;
while let Some(event) = stream.next().await {
    if let SummaryStreamEvent::Delta(d) = event {
        print!("{}", d.content);
    }
}

§Admin operations

Management methods are available directly on SearchcraftClient when the appropriate key is configured:

use searchcraft::SearchcraftClient;
use searchcraft::admin::types::IndexConfig;

let client = SearchcraftClient::new(
    "https://my-instance.searchcraft.io",
    Some("sc-read-key"),
    Some("sc-ingest-key"),
)?;

// Create an index
client.create_index("products", &IndexConfig::default()).await?;

// Insert a document
let doc = serde_json::json!({"id": "1", "title": "Laptop", "price": 999});
client.insert_document("products", &doc).await?;

§Error handling

All fallible operations return error::Result<T>. Use pattern matching or Error::is_retryable to decide how to handle failures:

match client.search_index::<serde_json::Value>("products", &req).await {
    Ok(resp) => println!("{} hits", resp.data.count),
    Err(e) if e.is_retryable() => eprintln!("transient: {e}"),
    Err(e) => return Err(e),
}

§Feature flags

FeatureDefaultDescription
rustlsUse rustls for TLS
native-tlsUse the platform’s native TLS

Re-exports§

pub use client::SearchcraftClient;
pub use config::Config;
pub use config::Operation;
pub use error::Error;

Modules§

admin
Management and administration APIs for the Searchcraft client.
client
The top-level SearchcraftClient.
config
Client configuration and API-key selection.
error
Error types for the Searchcraft client.
search
Search API and query builder for the Searchcraft client.
transport
HTTP transport layer for the Searchcraft API.
types
Shared types used across the Searchcraft client.