diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 62caffe9..bc7d6199 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -111,18 +111,18 @@ client ### 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. +To invoke an API that is not supported by the client, use the `client.send` method to do so. See [examples/json](./opensearch/examples/json.rs) for a complete working example. #### GET The following example returns the server version information via `GET /`. ```rust let info: Value = client - .send( + .send::<(), ()>( Method::Get, "/", HeaderMap::new(), - Option::<&String>::None, - Option::<&String>::None, + None, + None, None, ) .await? @@ -136,21 +136,21 @@ println!("Welcome to {} {}" , info["version"]["distribution"] , info["version"][ The following example creates an index. ```rust -let index_body = json!({ +let index_body: JsonBody<_> = json!({ "settings": { "index": { "number_of_shards" : 4 } } - }); + }).into(); client .send( Method::Put, "/movies", HeaderMap::new(), - Option::<&String>::None, - Some(index_body.to_string()), + Option::<&()>::None, + Some(index_body), None, ) .await?; @@ -161,7 +161,7 @@ The following example searches for a document. ```rust let q = "miller"; -let query = json!({ +let query: JsonBody<_> = json!({ "size": 5, "query": { "multi_match": { @@ -169,14 +169,14 @@ let query = json!({ "fields": ["title^2", "director"] } } -}); +}).into(); client .send( Method::Post, "/movies/_search", HeaderMap::new(), - Option::<&String>::None, - Some(query.to_string()), + Option::<&()>::None, + Some(query), None, ) .await?; @@ -186,12 +186,12 @@ client The following example deletes an index. ```rust client - .send( + .send::<(), ()>( Method::Delete, "/movies", HeaderMap::new(), - Option::<&String>::None, - Option::<&String>::None, + None, + None, None, ) .await?; diff --git a/opensearch/examples/json.rs b/opensearch/examples/json.rs index b19b7538..2b3395f2 100644 --- a/opensearch/examples/json.rs +++ b/opensearch/examples/json.rs @@ -5,6 +5,7 @@ use opensearch::cert::CertificateValidation; use opensearch::http::headers::HeaderMap; use opensearch::http::transport::{SingleNodeConnectionPool, TransportBuilder}; use opensearch::http::{Method, Url}; +use opensearch::http::request::JsonBody; use opensearch::OpenSearch; #[tokio::main] @@ -21,12 +22,12 @@ async fn main() -> Result<(), Box> { let document_id = "1"; let info: Value = client - .send( + .send::<(), ()>( Method::Get, "/", HeaderMap::new(), - Option::<&String>::None, - Option::<&String>::None, + None, + None, None, ) .await? @@ -39,21 +40,21 @@ async fn main() -> Result<(), Box> { ); // Create an index - let index_body = json!({ + let index_body : JsonBody<_> = json!({ "settings": { "index": { "number_of_shards" : 4 } } - }); + }).into(); let create_index_response = client .send( Method::Put, &format!("/{index_name}"), HeaderMap::new(), - Option::<&String>::None, - Some(index_body.to_string()), + Option::<&()>::None, + Some(index_body), None, ) .await?; @@ -61,27 +62,27 @@ async fn main() -> Result<(), Box> { assert_eq!(create_index_response.status_code(), 200); // add a document to the index - let document = json!({ + let document: JsonBody<_> = json!({ "title": "Moneyball", "director": "Bennett Miller", "year": "2011" - }); + }).into(); let create_document_response = client .send( Method::Put, &format!("/{index_name}/_doc/{document_id}"), HeaderMap::new(), - Some(&vec![("refresh", "true")]), - Some(index_body.to_string()), + Some(&[("refresh", "true")]), + Some(document), None, ) .await?; - assert_eq!(create_index_response.status_code(), 200); + assert_eq!(create_document_response.status_code(), 201); // Search for a document let q = "miller"; - let query = json!({ + let query : JsonBody<_> = json!({ "size": 5, "query": { "multi_match": { @@ -89,29 +90,31 @@ async fn main() -> Result<(), Box> { "fields": ["title^2", "director"] } } - }); + }).into(); let search_response = client .send( Method::Post, - "/movies/_search", + &format!("/{index_name}/_search"), HeaderMap::new(), - Option::<&String>::None, - Some(query.to_string()), + Option::<&()>::None, + Some(query), None, ) .await?; assert_eq!(search_response.status_code(), 200); + let search_result = search_response.json::().await?; + println!("Hits: {:#?}", search_result["hits"]["hits"].as_array().unwrap()); // Delete the document let delete_document_response = client - .send( + .send::<(),()>( Method::Delete, &format!("/{index_name}/_doc/{document_id}"), HeaderMap::new(), - Option::<&String>::None, - Option::<&String>::None, + None, + None, None, ) .await?; @@ -120,12 +123,12 @@ async fn main() -> Result<(), Box> { // Delete the index let delete_response = client - .send( + .send::<(),()>( Method::Delete, &format!("/{index_name}"), HeaderMap::new(), - Option::<&String>::None, - Option::<&String>::None, + None, + None, None, ) .await?;