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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/*! Elasticsearch API Client A modular and efficient native client for the Elasticsearch REST API. # Supported Versions `elastic` | Elasticsearch --------------- | ------------- `0.x` | `5.x` This crate depends heavily on the following crates: - [`reqwest`/`hyper`][reqwest] as the default HTTP layer - [`serde`/`serde_json`][serde] for serialisation - [`futures`/`tokio`][tokio] for async io. `elastic` is designed to scale up to the complexity of Elasticsearch's API, and with the complexity of the environments Elasticsearch is deployed in. # Usage This crate is on [crates.io][crates-io]. To get stated, add `elastic` to your `Cargo.toml`: ```ignore [dependencies] elastic = "*" elastic_derive = "*" ``` The following optional dependencies may also be useful: ```ignore serde = "*" serde_json = "*" serde_derive = "*" ``` Then reference in your crate root: ``` # fn main() {} extern crate elastic; #[macro_use] extern crate elastic_derive; ``` # Examples ## Creating a synchronous client The [`SyncClient`][SyncClient] type is an easy way to interact with an Elasticsearch cluster. A synchronous client can be created through the [`SyncClientBuilder`][SyncClientBuilder]. The builder allows you to configure default parameters for all requests: ```no_run # extern crate elastic; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { use elastic::prelude::*; use elastic::http::header::Authorization; let builder = SyncClientBuilder::new() .base_url("http://es_host:9200") .params(|p| p .url_param("pretty", true) .header(Authorization("let me in".to_owned()))); let client = builder.build()?; # Ok(()) # } ``` Individual requests can override these parameter values: ```no_run # extern crate elastic; # extern crate serde_json; # use serde_json::Value; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { let client = SyncClientBuilder::new().build()?; let response = client.search::<Value>() .params(|p| p.url_param("pretty", false)) .send()?; # Ok(()) # } ``` `elastic` also offers an [`AsyncClient`][AsyncClient]. For more details, see the [`client`][client-mod] and [`requests`][requests-mod] modules. ## Making requests _For a list of common client methods, see [here][request-builders]._ Each endpoint in the Elasticsearch REST API is provided as a strongly-typed structure. The client offers high-level request builders for some common Elasticsearch operations. ### Getting and Indexing documents The [Document Mapping API][docs-mapping] is provided as a custom derive plugin and set of Rust traits. Derive `Serialize`, `Deserialize` and `ElasticType` on your document types: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { #[derive(Serialize, Deserialize, ElasticType)] struct MyType { pub id: i32, pub title: String, pub timestamp: Date<DefaultDateMapping> } # Ok(()) # } ``` Call [`Client.document_put_mapping`][Client.document_put_mapping] to ensure an index has the right mapping for your document types: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Serialize, Deserialize, ElasticType)] # struct MyType { } # let client = SyncClientBuilder::new().build()?; client.document_put_mapping::<MyType>(index("myindex")) .send()?; # Ok(()) # } ``` Then call [`Client.document_index`][Client.document_index] to index documents in Elasticsearch: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Serialize, Deserialize, ElasticType)] # struct MyType { # pub id: i32, # pub title: String, # pub timestamp: Date<DefaultDateMapping> # } # let client = SyncClientBuilder::new().build()?; let doc = MyType { id: 1, title: String::from("A title"), timestamp: Date::now() }; let response = client.document_index(index("myindex"), id(doc.id), doc) .send()?; # Ok(()) # } ``` Call [`Client.document_get`][Client.document_get] to retrieve a single document from an index: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Serialize, Deserialize, ElasticType)] # struct MyType { # pub id: i32, # pub title: String, # pub timestamp: Date<DefaultDateMapping> # } # let client = SyncClientBuilder::new().build()?; let response = client.document_get::<MyType>(index("myindex"), id(1)) .send()?; if let Some(doc) = response.into_document() { println!("id: {}", doc.id); } # Ok(()) # } ``` For more details on document types, see the [`types`][types-mod] module. ### Searching documents Call [`Client.search`][Client.search] to execute [Query DSL][docs-search] queries: ```no_run # extern crate serde; # #[macro_use] extern crate serde_json; # #[macro_use] extern crate serde_derive; # #[macro_use] extern crate elastic_derive; # extern crate elastic; # use elastic::prelude::*; # fn main() { run().unwrap() } # fn run() -> Result<(), Box<::std::error::Error>> { # #[derive(Debug, Serialize, Deserialize, ElasticType)] # struct MyType { } # let client = SyncClientBuilder::new().build()?; let response = client.search::<MyType>() .index("myindex") .body(json!({ "query": { "query_string": { "query": "*" } } })) .send()?; // Iterate through the hits (of type `MyType`) for hit in response.hits() { println!("{:?}", hit); } # Ok(()) # } ``` # Crate design This crate is mostly a meta-package composed of a number of smaller pieces including: - `elastic_reqwest` HTTP transport - `elastic_requests` API request builders - `elastic_responses` API response parsers - `elastic_types` tools for document and mapping APIs This crate glues these libraries together with some simple assumptions about how they're going to be used. # Links - [Elasticsearch Docs][docs-root] - [Github][github] [reqwest]: https://github.com/seanmonstar/reqwest [serde]: https://serde.rs/ [tokio]: https://tokio.rs [crates-io]: https://crates.io/crates/elastic [github]: https://github.com/elastic-rs/elastic [docs-root]: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html [docs-mapping]: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html [docs-search]: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html [SyncClient]: client/type.SyncClient.html [SyncClientBuilder]: client/struct.SyncClientBuilder.html [AsyncClient]: client/type.AsyncClient.html [Client]: client/struct.Client.html [Client.document_put_mapping]: client/struct.Client.html#method.document_put_mapping [Client.document_index]: client/struct.Client.html#method.document_index [Client.document_get]: client/struct.Client.html#method.document_get [Client.search]: client/struct.Client.html#method.search [client-mod]: client/index.html [requests-mod]: client/requests/index.html [types-mod]: types/index.html [request-builders]: client/index.html#request-builders */ #![deny(warnings, missing_docs)] #![allow(unknown_lints, doc_markdown)] extern crate elastic_reqwest; extern crate elastic_types; #[macro_use] extern crate error_chain; extern crate futures; extern crate futures_cpupool; #[macro_use] extern crate log; extern crate reqwest; extern crate serde; #[macro_use] extern crate serde_derive; #[cfg_attr(test, macro_use)] extern crate serde_json; extern crate tokio_core; extern crate uuid; pub mod error; pub use error::Error; pub mod http { /*! Raw HTTP modules. These types are re-exported from `reqwest` and used in parts of `elastic`s public API. They may eventually be wrapped and made implementation details. */ pub use reqwest::header; pub use reqwest::Body as SyncBody; pub use reqwest::unstable::async::Chunk as AsyncChunk; } pub mod client; pub mod types; pub mod prelude { /*! A glob import for convenience. */ pub use client::prelude::*; pub use types::prelude::*; } #[cfg(test)] mod tests { pub fn assert_send<T: Send>() {} pub fn assert_sync<T: Sync>() {} }