Skip to content

What isn't covered yet

Some areas aren’t generated yet, and a newer Elasticsearch adds what this release doesn’t know. Three ways through, each going through the same client, with the same transport, errors and telemetry.

crates/oxsearch/examples/escape_hatches.rs
/// 1. An endpoint of your own: creating an index, whose API isn't
/// generated yet. It goes through the client like any other, with the
/// same transport, errors and, later, middleware.
struct CreateIndex<'a> {
name: &'a str,
mappings: Value,
}
/// What Elasticsearch answers, in your own type.
#[derive(Debug, Deserialize)]
struct Created {
acknowledged: bool,
index: String,
}
impl Endpoint for CreateIndex<'_> {
type Response = Created;
fn name(&self) -> &'static str {
"indices.create"
}
fn route(&self) -> Result<Route, Error> {
// Each segment is percent-encoded.
Ok(Route::segments(Method::Put, [self.name]))
}
fn query(&self) -> Result<Vec<(&'static str, String)>, Error> {
Ok(vec![("wait_for_active_shards", "1".to_owned())])
}
fn body(&self) -> Result<Option<Body>, Error> {
Body::json(&json!({ "mappings": self.mappings })).map(Some)
}
}

An endpoint is a type that implements Endpoint: its name, its route, its query parameters and its body, and the type its response reads as. Route::segments percent-encodes each segment, and Body::json writes any serializable value. client.send sends it like a generated request.

crates/oxsearch/examples/escape_hatches.rs
// 2. Raw variants. A query the types don't know, written as JSON...
let query_string = QueryContainer::Raw(json!({
"query_string": { "query": "hnsw OR graphs", "default_field": "text" }
}));
// ...inside a retriever written as JSON too.
let retriever = RetrieverContainer::Raw(json!({
"standard": { "query": query_string }
}));
let request = search::Request::new()
.index(INDEX)
.body(search::RequestBody::new().retriever(retriever));
let response: search::Response<Note> = client.send_as(&request).await?;
for note in response.documents() {
println!("found: {}", note.text);
}
// A value a newer Elasticsearch writes still reads: as a raw variant.
let order: SortOrder = serde_json::from_value(json!("sideways"))?;
println!("an unknown sort order reads as {order:?}");

Every query, retriever, aggregation and other union has a Raw variant that holds JSON, for one the types don’t know. The other way, a value a newer Elasticsearch sends that this release doesn’t know reads as a Raw variant, instead of failing.

crates/oxsearch/examples/escape_hatches.rs
// 3. A raw call, for anything else.
let health = client
.send_raw(Method::Get, "/_cluster/health", None)
.await?;
println!("cluster status: {}", health["status"]);

Client::send_raw sends any method and path, with an optional JSON body, and returns the response as JSON.