In the example below, we create a client, an index, insert a document to the index, search for the document, and finally delete the index.
To create a client to make API calls to OpenSearch running on http://localhost:9200
.
let client = OpenSearch::default();
Alternatively, you can create a client to make API calls against OpenSearch running on a
specific url::Url
.
let transport = Transport::single_node("https://example.com")?;
let client = OpenSearch::new(transport);
let info: Value = client.info().send().await?.json().await?;
println!(
"{}: {}",
info["version"]["distribution"].as_str().unwrap(),
info["version"]["number"].as_str().unwrap()
);
client
.indices()
.create(opensearch::indices::IndicesCreateParts::Index("movies"))
.send()
.await?;
client
.index(opensearch::IndexParts::Index("movies"))
.body(serde_json::json!({
"title": "Moneyball",
"director": "Bennett Miller",
"year": 2011
}
))
.send()
.await?;
Make a query and display the response body and the search results.
let response = client
.search(opensearch::SearchParts::Index(&["movies"]))
.body(serde_json::json!({
"query": {
"match": {
"director": "miller"
}
}
}
))
.send()
.await?;
let response_body = response.json::<Value>().await?;
println!("{}", serde_json::to_string_pretty(&response_body).unwrap());
for hit in response_body["hits"]["hits"].as_array().unwrap() {
println!("{:?}", hit["_source"]);
}
client
.indices()
.delete(opensearch::indices::IndicesDeleteParts::Index(&["movies"]))
.send()
.await?;
To invoke an API that is not supported by the client, use the client.send
method to do so. See examples/json for a complete working example.
The following example returns the server version information via GET /
.
let info: Value = client
.send::<(), ()>(
Method::Get,
"/",
HeaderMap::new(),
None,
None,
None,
)
.await?
.json()
.await?;
println!("Welcome to {} {}" , info["version"]["distribution"] , info["version"]["number"]);
The following example creates an index.
let index_body: JsonBody<_> = json!({
"settings": {
"index": {
"number_of_shards" : 4
}
}
}).into();
client
.send(
Method::Put,
"/movies",
HeaderMap::new(),
Option::<&()>::None,
Some(index_body),
None,
)
.await?;
The following example searches for a document.
let q = "miller";
let query: JsonBody<_> = json!({
"size": 5,
"query": {
"multi_match": {
"query": q,
"fields": ["title^2", "director"]
}
}
}).into();
client
.send(
Method::Post,
"/movies/_search",
HeaderMap::new(),
Option::<&()>::None,
Some(query),
None,
)
.await?;
The following example deletes an index.
client
.send::<(), ()>(
Method::Delete,
"/movies",
HeaderMap::new(),
None,
None,
None,
)
.await?;
This library supports Amazon OpenSearch Service and OpenSearch Serverless.
Create a client with AWS credentials as follows. Make sure to specify the correct service name and signing region.
let url = Url::parse("https://...");
let service_name = "es"; // use "aoss" for OpenSearch Serverless
let conn_pool = SingleNodeConnectionPool::new(url?);
let region_provider = RegionProviderChain::default_provider().or_else("us-east-1");
let aws_config = aws_config::from_env().region(region_provider).load().await.clone();
let transport = TransportBuilder::new(conn_pool)
.auth(aws_config.clone().try_into()?)
.service_name(service_name)
.build()?;
let client = OpenSearch::new(transport);