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 25, 2023
1 parent de28ab4 commit b3e25ff
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 68 deletions.
149 changes: 81 additions & 68 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions opensearch/examples/json.rs
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(())
}

0 comments on commit b3e25ff

Please sign in to comment.