-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathjson.rs
152 lines (134 loc) · 3.73 KB
/
json.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
use opensearch::{
auth::Credentials,
cert::CertificateValidation,
http::{
headers::HeaderMap,
request::JsonBody,
transport::{SingleNodeConnectionPool, TransportBuilder},
Method, Url,
},
OpenSearch,
};
use serde_json::{json, Value};
#[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);
let index_name = "movies";
let document_id = "1";
let info: Value = client
.send::<(), ()>(Method::Get, "/", HeaderMap::new(), None, None, None)
.await?
.json()
.await?;
println!(
"Welcome to {} {}",
info["version"]["distribution"], info["version"]["number"]
);
// Create an index
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::<&()>::None,
Some(index_body),
None,
)
.await?;
assert_eq!(create_index_response.status_code(), 200);
// add a document to the index
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(&[("refresh", "true")]),
Some(document),
None,
)
.await?;
assert_eq!(create_document_response.status_code(), 201);
// Search for a document
let q = "miller";
let query: JsonBody<_> = json!({
"size": 5,
"query": {
"multi_match": {
"query": q,
"fields": ["title^2", "director"]
}
}
})
.into();
let search_response = client
.send(
Method::Post,
&format!("/{index_name}/_search"),
HeaderMap::new(),
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::<(), ()>(
Method::Delete,
&format!("/{index_name}/_doc/{document_id}"),
HeaderMap::new(),
None,
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(),
None,
None,
None,
)
.await?;
assert_eq!(delete_response.status_code(), 200);
Ok(())
}