-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: samuel orji <[email protected]>
- Loading branch information
1 parent
de28ab4
commit b3e25ff
Showing
2 changed files
with
168 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use serde_json::json; | ||
|
||
use opensearch::auth::Credentials; | ||
use opensearch::cert::CertificateValidation; | ||
use opensearch::http::headers::HeaderMap; | ||
use opensearch::http::transport::{SingleNodeConnectionPool, TransportBuilder}; | ||
use opensearch::http::{Method, Url}; | ||
use opensearch::OpenSearch; | ||
|
||
#[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); | ||
|
||
// Delete Index | ||
let response = client | ||
.send( | ||
Method::Delete, | ||
"/movies", | ||
HeaderMap::new(), | ||
Option::<&String>::None, | ||
Option::<&String>::None, | ||
None, | ||
) | ||
.await?; | ||
|
||
// create index | ||
let response = client | ||
.send( | ||
Method::Put, | ||
"/movies", | ||
HeaderMap::new(), | ||
Option::<&String>::None, | ||
Option::<&String>::None, | ||
None, | ||
) | ||
.await?; | ||
assert_eq!(response.status_code().as_u16(), 200_u16); | ||
|
||
// Add a document | ||
let document = r#"{ | ||
"name" : "Mission Impossible", | ||
"year" : "2001" | ||
}"#; | ||
|
||
let response = client | ||
.send( | ||
Method::Post, | ||
"/movies/_doc", | ||
HeaderMap::new(), | ||
Option::<&String>::None, | ||
Some(document), | ||
None, | ||
) | ||
.await?; | ||
|
||
assert_eq!(response.status_code().as_u16(), 201_u16); | ||
|
||
// query document | ||
let query = json!({ | ||
"query": { | ||
"match": { | ||
"name" : "Mission Impossible" | ||
} | ||
} | ||
}); | ||
|
||
let response = client | ||
.send( | ||
Method::Get, | ||
"/movies/_search", | ||
HeaderMap::new(), | ||
Option::<&String>::None, | ||
Some(query.to_string()), | ||
None, | ||
) | ||
.await?; | ||
|
||
assert_eq!(response.status_code().as_u16(), 200_u16); | ||
|
||
Ok(()) | ||
} |