1
- # elasticsearch-rs
1
+ # elasticsearch
2
2
3
- Repository for the official Elasticsearch Rust Client, ` elasticsearch ` .
3
+ An official Rust Client for Elasticsearch .
4
4
5
5
The project is still very much a _ work in progress_ and in an _ alpha_ state;
6
6
input and contributions welcome!
7
7
8
8
## Versions and Compatibility
9
9
10
- < table >
11
- <tr>
12
- <th><b>Rust client<b></th>
13
- <th><b>Elasticsearch<b></th>
14
- <th><b>Status</b></th>
15
- </tr>
16
- <tr>
17
- <td><code>7.x</code></td>
18
- <td><code>7.x</code></td>
19
- <td><code>alpha</code></td>
20
- </tr>
21
- </ table >
22
-
23
- The Rust client is largely generated from the REST API specs of Elasticsearch.
24
- As such , the API functions available align with the version of the specs from which
25
- they're generated. Elasticsearch strives to adhere to [ Semantic Versioning ] ( https://semver.org/ ) ,
26
- which is reflected in the specs and thus this client.
27
-
28
- What this means in practice is that a 7.x Rust client is compatible with Elasticsearch 7.x. Where
29
- the client minor version is greater than Elasticsearch minor version, it may contain API functions
30
- for APIs that are not available in the version of Elasticsearch, but all other API functions will
31
- be compatible. Where the client minor version is less than Elasticsearch minor version, all API
32
- functions will be compatible.
10
+ | Rust client | Elasticsearch | Status |
11
+ | ------------- | --------------- | -------- |
12
+ | 7.x | 7.x | alpha |
13
+
14
+ A major version of the client is compatible with the same major version of Elasticsearch.
15
+ Since Elasticsearch is developed following [ Semantic Versioning ] ( https://semver.org/ ) principles,
16
+ Any minor/patch version of the client can be used against any minor/patch version of Elasticsearch
17
+ ** within the same major version lineage ** . For example,
18
+
19
+ - A ` 7.5.0 ` client can be used against ` 7.0.0 ` Elasticsearch
20
+ - A ` 7.4.0 ` client can be used against ` 7.5.1 ` Elasticsearch
21
+
22
+ In the former case, a 7.5.0 client may contain additional API functions that are not available
23
+ in 7.0.0 Elasticsearch. In this case, these APIs cannot be used, but for any APIs available in
24
+ Elasticsearch , the respective API functions on the client will be compatible.
25
+
26
+ In the latter case, a 7.4.0 client won't contain API functions for APIs that are introduced in
27
+ Elasticsearch 7.5.0+, but for all other APIs available in Elasticsearch, the respective API
28
+ functions on the client will be compatible.
29
+
30
+ ** No compatibility assurances are given between different major versions of the client and
31
+ Elasticsearch ** . Major differences likely exist between major versions of Elasticsearch, particularly
32
+ around request and response object formats, but also around API urls and behaviour.
33
33
34
34
## Overview
35
35
@@ -68,33 +68,85 @@ that is compatible with the version of Elasticsearch you're using
68
68
elasticsearch = " 7.5.1-alpha1"
69
69
```
70
70
71
- #### Connecting
71
+ The following _ optional_ dependencies may also be useful to create requests and read responses
72
+
73
+ ``` toml
74
+ serde = " ~1"
75
+ serde_json = " ~1"
76
+ ```
77
+
78
+ #### Create a client
72
79
73
80
Build a transport to make API requests to Elasticsearch using the ` TransportBuilder ` ,
74
81
which allows setting of proxies and authentication schemes
75
82
83
+ To create a client to make API calls to Elasticsearch running on ` http://localhost:9200 `
84
+
85
+ ``` rust,no_run
86
+ use elasticsearch::{Error, Elasticsearch};
87
+
88
+ fn run() {
89
+ let client = Elasticsearch::default();
90
+ }
91
+ ```
92
+ Alternatively, you can create a client to make API calls against Elasticsearch running on a specific url
93
+
76
94
``` rust,no_run
77
95
use elasticsearch::{
78
- Elasticsearch, Error,
79
- auth::Credentials,
80
- http::transport::{TransportBuilder, SingleNodeConnectionPool}
96
+ Error, Elasticsearch,
97
+ http::transport::{Transport, SingleNodeConnectionPool}
98
+ };
99
+
100
+ fn run() -> Result<(), Error> {
101
+ let transport = Transport::single_node("https://example.com")?;
102
+ let client = Elasticsearch::new(transport);
103
+ Ok(())
104
+ }
105
+ ```
106
+
107
+ If you're running against an Elasticsearch deployment in [ Elastic Cloud] ( https://www.elastic.co/cloud/ ) ,
108
+ a client can be created using a [ Cloud ID] ( https://www.elastic.co/guide/en/cloud/current/ec-cloud-id.html )
109
+ and credentials retrieved from the Cloud web console
110
+
111
+ ``` rust,no_run
112
+ use elasticsearch::{
113
+ auth::Credentials,
114
+ Error, Elasticsearch,
115
+ http::transport::Transport,
81
116
};
82
117
use url::Url;
83
118
84
- async fn run() -> Result<(), Error> {
119
+ fn run() -> Result<(), Error> {
120
+ let cloud_id = "cluster_name:Y2xvdWQtZW5kcG9pbnQuZXhhbXBsZSQzZGFkZjgyM2YwNTM4ODQ5N2VhNjg0MjM2ZDkxOGExYQ==";
121
+ let credentials = Credentials::Basic("<username>".into(), "<password>".into());
122
+ let transport = Transport::cloud(cloud_id, credentials)?;
123
+ let client = Elasticsearch::new(transport);
124
+ Ok(())
125
+ }
126
+ ```
85
127
86
- let conn_pool = SingleNodeConnectionPool::new(Url::parse("https://example.com").unwrap());
87
- let transport = TransportBuilder::new(conn_pool)
88
- .auth(Credentials::Basic("<username>".into(), "<password>".into()))
89
- .disable_proxy()
90
- .build()?;
91
-
128
+ More control over how a [ Transport] ( http::transport::Transport ) is built can be
129
+ achieved using [ TransportBuilder] ( http::transport::TransportBuilder ) to build a transport, and
130
+ passing it to [ Elasticsearch::new] create a new instance of [ Elasticsearch]
131
+
132
+ ``` rust,no_run
133
+ use elasticsearch::{
134
+ auth::Credentials,
135
+ Error, Elasticsearch,
136
+ http::transport::{TransportBuilder,SingleNodeConnectionPool},
137
+ };
138
+ use url::Url;
139
+
140
+ fn run() -> Result<(), Error> {
141
+ let url = Url::parse("https://example.com")?;
142
+ let conn_pool = SingleNodeConnectionPool::new(url);
143
+ let transport = TransportBuilder::new(conn_pool).disable_proxy().build()?;
92
144
let client = Elasticsearch::new(transport);
93
145
Ok(())
94
146
}
95
147
```
96
148
97
- #### Calling an API endpoint
149
+ #### Making API calls
98
150
99
151
The following will execute a ` POST ` request to ` /_search?allow_no_indices=true ` with
100
152
a JSON body of ` {"query":{"match_all":{}}} `
@@ -157,12 +209,16 @@ The `quote` and `syn` crates help
157
209
- Start simple and iterate
158
210
- Design of the API is conducive to ease of use
159
211
- Asynchronous only
160
- - Control API invariants through arguments on API function
212
+ - Control API invariants through arguments on API function. For example
213
+
214
+ ``` norun
215
+ client.delete_script(DeleteScriptParts::Id("script_id"))
216
+ .send()
217
+ .await?;
218
+ ```
161
219
162
- e.g. ` client.delete_script("script_id".into()).send()?; `
163
-
164
- An id must always be provided for a script, so the ` delete_script() ` function must accept
165
- it as a value.
220
+ An id must always be provided for a delete script API call, so the `delete_script()` function
221
+ must accept it as a value.
166
222
167
223
## Contributing
168
224
@@ -176,12 +232,12 @@ to address it, please assign the issue to yourself, so that others know that it
176
232
177
233
### Sign the Contributor License Agreement
178
234
179
- We do ask that you sign the [ Contiributor License Agreement] ( https://www.elastic.co/contributor-agreement ) before we can accept pull requests from you.
235
+ We do ask that you sign the [Contiributor License Agreement](https://www.elastic.co/contributor-agreement)
236
+ before we can accept pull requests from you.
180
237
181
238
### Current Setup
182
239
183
- Using Rust nightly for development, whilst reqwest crate, the HTTP client used, has support for ` async `
184
- in an alpha release and has a dependency which uses features that only work on nightly:
240
+ Use Rust nightly for development for now:
185
241
186
242
```sh
187
243
> rustup show
@@ -194,7 +250,7 @@ nightly-x86_64-pc-windows-msvc (default)
194
250
rustc 1.41.0-nightly (a44774c3a 2019-11-25)
195
251
```
196
252
197
- It is expected to move to stable once dependencies compile on stable.
253
+ It is expected to move to rust stable once dependencies compile on stable.
198
254
199
255
### Coding styleguide
200
256
0 commit comments