diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 09950e73..62caffe9 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,92 @@ 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. + +#### GET +The following example returns the server version information via `GET /`. ```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 info: Value = client + .send( + Method::Get, + "/", + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await? + .json() + .await?; + +println!("Welcome to {} {}" , info["version"]["distribution"] , info["version"]["number"]); + +``` +#### PUT +The following example creates an index. + +```rust +let index_body = json!({ + "settings": { + "index": { + "number_of_shards" : 4 + } + } }); - 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); +client + .send( + Method::Put, + "/movies", + HeaderMap::new(), + Option::<&String>::None, + Some(index_body.to_string()), + None, + ) + .await?; +``` +#### POST +The following example searches for a document. + +```rust +let q = "miller"; + +let query = json!({ + "size": 5, + "query": { + "multi_match": { + "query": q, + "fields": ["title^2", "director"] + } + } +}); +client + .send( + Method::Post, + "/movies/_search", + HeaderMap::new(), + Option::<&String>::None, + Some(query.to_string()), + None, + ) + .await?; +``` + +#### DELETE +The following example deletes an index. +```rust +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..b19b7538 --- /dev/null +++ b/opensearch/examples/json.rs @@ -0,0 +1,136 @@ +use serde_json::{json, Value}; + +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); + + let index_name = "movies"; + let document_id = "1"; + + let info: Value = client + .send( + Method::Get, + "/", + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await? + .json() + .await?; + + println!( + "Welcome to {} {}", + info["version"]["distribution"], info["version"]["number"] + ); + + // Create an index + let index_body = json!({ + "settings": { + "index": { + "number_of_shards" : 4 + } + } + }); + + let create_index_response = client + .send( + Method::Put, + &format!("/{index_name}"), + HeaderMap::new(), + Option::<&String>::None, + Some(index_body.to_string()), + None, + ) + .await?; + + assert_eq!(create_index_response.status_code(), 200); + + // add a document to the index + let document = json!({ + "title": "Moneyball", + "director": "Bennett Miller", + "year": "2011" + }); + 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()), + None, + ) + .await?; + + assert_eq!(create_index_response.status_code(), 200); + + // Search for a document + let q = "miller"; + let query = json!({ + "size": 5, + "query": { + "multi_match": { + "query": q, + "fields": ["title^2", "director"] + } + } + }); + + let search_response = client + .send( + Method::Post, + "/movies/_search", + HeaderMap::new(), + Option::<&String>::None, + Some(query.to_string()), + None, + ) + .await?; + + assert_eq!(search_response.status_code(), 200); + + // Delete the document + let delete_document_response = client + .send( + Method::Delete, + &format!("/{index_name}/_doc/{document_id}"), + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await?; + + assert_eq!(delete_document_response.status_code(), 200); + + // Delete the index + let delete_response = client + .send( + Method::Delete, + &format!("/{index_name}"), + HeaderMap::new(), + Option::<&String>::None, + Option::<&String>::None, + None, + ) + .await?; + + assert_eq!(delete_response.status_code(), 200); + + Ok(()) +}