diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 09950e73..83829600 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -7,7 +7,7 @@ - [Add a Document to the Index](#add-a-document-to-the-index) - [Search for a Document](#search-for-a-document) - [Delete the Index](#delete-the-index) - - [Make raw json requests](#make-raw-json-requests) + - [Make Raw Json Requests](#make-raw-json-requests) - [Amazon OpenSearch and OpenSearch Serverless](#amazon-opensearch-and-opensearch-serverless) - [Create a Client](#create-a-client-1) @@ -109,75 +109,88 @@ client .await?; ``` -### Make raw json requests -Make raw http requests +### Make Raw Json Requests + +To invoke an API that is not supported by the client, use `client.send` method to do so. see [examples/json](./opensearch/examples/json.rs) for a complete working example + +#### PUT +```rust +// create index +let response = client + .send( + Method::Put, + "/movies", + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await?; +``` + +#### POST ```rust - 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); +// 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); +``` + +#### GET +```rust +// 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); +``` + +#### DELETE + +```rust +// delete index +let response = client + .send( + Method::Delete, + "/movies", + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await?; ``` ## Amazon OpenSearch and OpenSearch Serverless diff --git a/opensearch/examples/json.rs b/opensearch/examples/json.rs new file mode 100644 index 00000000..47444321 --- /dev/null +++ b/opensearch/examples/json.rs @@ -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> { + 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(()) +}