Get started
oxsearch needs Rust 1.95 or newer, and Elasticsearch 9.
-
Add the dependencies. oxsearch, serde for your document type, and Tokio to run it:
Cargo.toml [dependencies]oxsearch = { version = "0.2", features = ["derive", "transport-reqwest"] }serde = { version = "1", features = ["derive"] }tokio = { version = "1", features = ["macros", "rt-multi-thread"] }deriveturns on the typed document layer and its derive macro.transport-reqwest, the HTTP transport, is one of the default features from the release after 0.2; naming it does no harm. -
Start Elasticsearch. Any cluster of Elasticsearch 9 will do. In the repository, the test fixture is one node in Docker:
scripts/es-fixture.sh up trialcargo run -p oxsearch --example quickstart --features deriveThe example embeds text with ELSER, Elastic’s sparse model, which needs a license above Basic, such as the trial the fixture starts. On Basic, the AI search guide brings its own embeddings instead.
-
Write the code.
crates/oxsearch/examples/quickstart.rs use oxsearch::document::{Document, Indexes};use oxsearch::transport_reqwest::ReqwestTransport;use oxsearch::{Client, Refresh};use serde::{Deserialize, Serialize};#[derive(Document, Serialize, Deserialize, Debug)]#[index("quickstart-passages")]struct Passage {#[semantic_text]text: String,}#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {let client = Client::new(ReqwestTransport::new("http://localhost:9200")?);let passages = client.index::<Passage>().refresh(Refresh::WaitFor);let _ = passages.ensure_mapping().await?;passages.put(&Passage {text: "HNSW graphs find nearest neighbours fast.".into(),}).await?;passages.put(&Passage {text: "Replicas keep copies of shards on other nodes.".into(),}).await?;for hit in passages.search(Passage::TEXT.semantic("how do vector indexes work?")).await?{println!("{:.2} {:?}", hit.score.unwrap_or_default(), hit.source);}Ok(())}
What it does
Section titled “What it does”#[derive(Document)]reads the struct:#[index(…)]names its index, and#[semantic_text]mapstextas asemantic_textfield, which Elasticsearch embeds as documents are indexed.client.index::<Passage>()is anIndex<Passage>, which reads, writes and searches that type.refresh(Refresh::WaitFor)makes each write wait until searches can see it.ensure_mappingcreates the index with the type’s mapping if it doesn’t exist, adds the fields an existing one lacks, and reports fields mapped differently, which it never changes.Passage::TEXTis the field’s handle. Asemantic_textfield offerssemantic; a keyword field would offertermandtermsinstead.- Each hit carries its score and its source, read back as a
Passage.
- Your own document types: more of the document layer.
- Connecting to a cluster: several nodes, retries and credentials.
- The overview: what each feature adds.