Skip to content

Get started

oxsearch needs Rust 1.95 or newer, and Elasticsearch 9.

  1. 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"] }

    derive turns 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.

  2. 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 trial
    cargo run -p oxsearch --example quickstart --features derive

    The 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.

  3. 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(())
    }
  • #[derive(Document)] reads the struct: #[index(…)] names its index, and #[semantic_text] maps text as a semantic_text field, which Elasticsearch embeds as documents are indexed.
  • client.index::<Passage>() is an Index<Passage>, which reads, writes and searches that type. refresh(Refresh::WaitFor) makes each write wait until searches can see it.
  • ensure_mapping creates 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::TEXT is the field’s handle. A semantic_text field offers semantic; a keyword field would offer term and terms instead.
  • Each hit carries its score and its source, read back as a Passage.