Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: samuel orji <[email protected]>
  • Loading branch information
samuelorji committed Oct 26, 2023
1 parent 7a046c5 commit 8f2fb66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
30 changes: 15 additions & 15 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?;
Expand All @@ -161,22 +161,22 @@ The following example searches for a document.
```rust
let q = "miller";

let query = json!({
let query: JsonBody<_> = json!({
"size": 5,
"query": {
"multi_match": {
"query": q,
"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?;
Expand All @@ -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?;
Expand Down
49 changes: 26 additions & 23 deletions opensearch/examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -21,12 +22,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let document_id = "1";

let info: Value = client
.send(
.send::<(), ()>(
Method::Get,
"/",
HeaderMap::new(),
Option::<&String>::None,
Option::<&String>::None,
None,
None,
None,
)
.await?
Expand All @@ -39,79 +40,81 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);

// 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?;

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": {
"query": q,
"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::<Value>().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?;
Expand All @@ -120,12 +123,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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?;
Expand Down

0 comments on commit 8f2fb66

Please sign in to comment.