use opensearch::{
auth::Credentials,
cert::CertificateValidation,
http::transport::{SingleNodeConnectionPool, TransportBuilder},
indices::{
IndicesCreateParts, IndicesDeleteParts, IndicesExistsParts, IndicesGetParts,
IndicesPutMappingParts, IndicesPutSettingsParts,
},
IndexParts, OpenSearch,
};
use serde_json::json;
use url::Url;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = Url::parse("https://localhost:9200")?;
let credentials = Credentials::Basic("admin".into(), "admin".into());
let transport = TransportBuilder::new(SingleNodeConnectionPool::new(url))
.cert_validation(CertificateValidation::None)
.auth(credentials)
.build()?;
let client = OpenSearch::new(transport);
println!("{:?}", client.info().send().await?);
client
.indices()
.create(IndicesCreateParts::Index("paintings"))
.send()
.await?;
client
.indices()
.create(IndicesCreateParts::Index("movies"))
.body(json!({
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"title": { "type": "text" },
"year": { "type": "integer" }
}
}
}))
.send()
.await?;
println!(
"{}",
client
.indices()
.exists(IndicesExistsParts::Index(&["burner"]))
.send()
.await?
.json::<bool>()
.await?
); client
.index(IndexParts::Index("burner"))
.body(json!({ "lorem": "ipsum" }))
.send()
.await?;
println!(
"{}",
client
.indices()
.exists(IndicesExistsParts::Index(&["burner"]))
.send()
.await?
.json::<bool>()
.await?
);
client
.indices()
.put_settings(IndicesPutSettingsParts::Index(&["movies"]))
.body(json!({
"index": {
"number_of_replicas": 0
}
}))
.send()
.await?;
client
.indices()
.put_mapping(IndicesPutMappingParts::Index(&["movies"]))
.body(json!({
"properties": {
"director": { "type": "text" }
}
}))
.send()
.await?;
println!(
"{:#?}",
client
.indices()
.get(IndicesGetParts::Index(&["movies"]))
.send()
.await?
.json::<serde_json::Value>()
.await?
);
client
.indices()
.delete(IndicesDeleteParts::Index(&["movies"]))
.send()
.await?;
client
.indices()
.delete(IndicesDeleteParts::Index(&[
"movies",
"paintings",
"burner",
]))
.ignore_unavailable(true)
.send()
.await?;
Ok(())
}