Skip to content

Connecting to a cluster

ReqwestTransport::new(url) sends each request once, to one node. For a cluster in production, its builder adds the layers you need, each one opt-in.

Cargo.toml
[dependencies]
oxsearch = { version = "0.2", features = ["transport-reqwest"] }
tokio = { version = "1", features = ["macros", "rt"] }
crates/oxsearch/examples/transport.rs
// Requests go to the nodes in turn. One that fails rests, and is tried
// again later; a request it couldn't answer goes to the next node.
let mut builder = ReqwestTransport::builder()
.nodes(urls.split(','))
.retries(3)
.backoff(Duration::from_millis(100), Duration::from_secs(5))
.sniff_on_failure(true);
// An API key, encoded as Elasticsearch gives it, on every request.
if let Ok(key) = std::env::var("OXSEARCH_API_KEY") {
builder = builder.auth(Auth::api_key(key));
}
let transport = builder.build()?;
println!("nodes: {:?}", transport.nodes());
let client = Client::new(transport);
crates/oxsearch/examples/transport.rs
// `ping` answers whether the cluster is there; `info` says what it is.
let up = client.send(&ping::Request::new()).await.is_ok();
let info = client.send(&info::Request::new()).await?;
println!(
"cluster {:?} is up: {up}; Elasticsearch {:?}",
info.cluster_name.as_deref().unwrap_or("?"),
info.version.as_ref().and_then(|v| v.number.as_deref())
);
  • Nodes and failover. Requests go to the nodes in turn. A node that fails rests, for a minute after its first failure and twice as long after each one in a row, up to half an hour; then it’s tried again. When every node rests, the one resting least is tried.
  • Retries (retries, none by default). A request that couldn’t reach a node, or that a node answered with 502, 503 or 504, goes to the next node. One answered with 429 goes again after a growing backoff. Timeouts are retried only with retry_on_timeout, since the node may have acted on the request.
  • Sniffing (sniff_every, sniff_on_failure): the node list is refreshed from the cluster’s own list of HTTP addresses.
  • Credentials (auth): basic, bearer or an API key, on every request. cloud_id takes an Elastic Cloud deployment’s ID in place of node URLs.
  • Compression: with the transport crate’s gzip feature, request bodies are compressed and compressed responses accepted.

The transport is a trait in oxsearch-core, Transport, and the generated crates depend only on it. A layer of your own is a Transport that wraps another, and oxsearch::auth signs requests with credentials that refresh, such as short-lived tokens.

Each request records a span, named for its endpoint, and its duration, through the tracing and metrics facades, following OpenTelemetry’s conventions for Elasticsearch. The transport adds where the request went and each retry. Install a subscriber and a recorder, such as oxtel’s, to export them.