-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated user guide with a complete example.
Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
6 changed files
with
170 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,123 @@ | ||
- [User Guide](#user-guide) | ||
- [Example](#example) | ||
- [Create a client](#create-a-client) | ||
- [Making API calls](#making-api-calls) | ||
- [Create a Client](#create-a-client) | ||
- [Display Server Version](#display-server-version) | ||
- [Create an Index](#create-an-index) | ||
- [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) | ||
- [Amazon OpenSearch Service](#amazon-opensearch-service) | ||
- [Create a Client](#create-a-client-1) | ||
|
||
# User Guide | ||
|
||
## Example | ||
In the example below, we create a client, an index with non-default settings, insert a document to the index, | ||
search for the document, delete the document and finally delete the index. | ||
|
||
In the example below, we create a client, an index, insert a document to the index, search for the document, and finally delete the index. | ||
|
||
#### Create a client | ||
### Create a Client | ||
|
||
To create a client to make API calls to OpenSearch running on `http://localhost:9200` | ||
To create a client to make API calls to OpenSearch running on `http://localhost:9200`. | ||
|
||
```rust | ||
let client = OpenSearch::default(); | ||
``` | ||
|
||
Alternatively, you can create a client to make API calls against OpenSearch running on a | ||
specific `url::Url` | ||
specific `url::Url`. | ||
|
||
```rust | ||
let transport = Transport::single_node("https://example.com")?; | ||
let client = OpenSearch::new(transport); | ||
``` | ||
|
||
#### Making API calls | ||
### Display Server Version | ||
|
||
```rust | ||
let info: Value = client.info().send().await?.json().await?; | ||
println!( | ||
"{}: {}", | ||
info["version"]["distribution"].as_str().unwrap(), | ||
info["version"]["number"].as_str().unwrap() | ||
); | ||
``` | ||
|
||
### Create an Index | ||
|
||
```rust | ||
client | ||
.indices() | ||
.create(opensearch::indices::IndicesCreateParts::Index("movies")) | ||
.send() | ||
.await?; | ||
``` | ||
|
||
### Add a Document to the Index | ||
|
||
```rust | ||
client | ||
.index(opensearch::IndexParts::Index("movies")) | ||
.body(serde_json::json!({ | ||
"title": "Moneyball", | ||
"director": "Bennett Miller", | ||
"year": 2011 | ||
} | ||
)) | ||
.send() | ||
.await?; | ||
``` | ||
|
||
### Search for a Document | ||
|
||
The following makes an API call to `tweets/_search` with the json body | ||
`{"query":{"match":{"message":"OpenSearch"}}}` | ||
Make a query and display the response body and the search results. | ||
|
||
```rust | ||
let response = client | ||
.search(SearchParts::Index(&["tweets"])) | ||
.from(0) | ||
.size(10) | ||
.body(json!({ | ||
"query": { | ||
"match": { | ||
"message": "OpenSearch rust" | ||
.search(opensearch::SearchParts::Index(&["movies"])) | ||
.body(serde_json::json!({ | ||
"query": { | ||
"match": { | ||
"director": "miller" | ||
} | ||
} | ||
} | ||
})) | ||
)) | ||
.send() | ||
.await?; | ||
|
||
let response_body = response.json::<Value>().await?; | ||
let took = response_body["took"].as_i64().unwrap(); | ||
println!("{}", serde_json::to_string_pretty(&response_body).unwrap()); | ||
|
||
for hit in response_body["hits"]["hits"].as_array().unwrap() { | ||
// print the source document | ||
println!("{:?}", hit["_source"]); | ||
} | ||
``` | ||
``` | ||
|
||
### Delete the Index | ||
|
||
```rust | ||
client | ||
.indices() | ||
.delete(opensearch::indices::IndicesDeleteParts::Index(&["movies"])) | ||
.send() | ||
.await?; | ||
``` | ||
|
||
## Amazon OpenSearch Service | ||
|
||
This library supports [Amazon OpenSearch Service](https://aws.amazon.com/opensearch-service/). | ||
|
||
### Create a Client | ||
|
||
Create a client with AWS credentials as follows. Make sure to specify the correct service name and signing region. | ||
|
||
```rust | ||
let url = Url::parse("https://..."); | ||
let conn_pool = SingleNodeConnectionPool::new(url?); | ||
let region_provider = RegionProviderChain::default_provider().or_else("us-east-1"); | ||
let aws_config = aws_config::from_env().region(region_provider).load().await.clone(); | ||
let transport = TransportBuilder::new(conn_pool) | ||
.auth(aws_config.clone().try_into()?) | ||
.build()?; | ||
let client = OpenSearch::new(transport); | ||
``` |
Oops, something went wrong.