Crate elastic_reqwest [−] [src]
Elasticsearch REST API Client
A lightweight implementation of the Elasticsearch API based on the reqwest
HTTP client.
Each API endpoint is represented as its own type, that only accept a valid combination of route parameters. This library makes very few assumptions, leaving it up to you to decide what to invest your precious CPU cycles into.
The API is generated from the official Elasticsearch spec, so it's always current.
Supported Versions
elastic_types | Elasticsearch |
---|---|
0.x | 5.x |
Usage
This crate is on crates.io.
To get started, add elastic_reqwest
and reqwest
to your Cargo.toml
:
[dependencies] elastic_reqwest = "*" reqwest = "*"
Then reference in your crate root:
extern crate elastic_reqwest;
Making synchronous requests
- Create a
SyncClient
- Call
elastic_req
on the client - Work with the raw http response
- Or call
parse
to get a concrete response or API error
Minimal Example
Execute a search request synchronously and parse the response:
//HTTP POST /myindex/mytype/_search #[macro_use] extern crate serde_json; extern crate elastic_reqwest; use serde_json::Value; use elastic_reqwest::{SyncElasticClient, SyncFromResponse, parse}; use elastic_reqwest::req::SearchRequest; use elastic_reqwest::res::SearchResponse; let (client, params) = elastic_reqwest::sync::default().unwrap(); let search = SearchRequest::for_index_ty( "myindex", "mytype", json!({ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "geo_distance": { "distance": "20km", "location": { "lat": 37.776, "lon": -122.41 } } } } } }) ); let http_res = client.elastic_req(¶ms, search).unwrap(); let search_res = parse::<SearchResponse<Value>>().from_response(http_res).unwrap();
Making asynchronous requests
- Create an
AsyncClient
- Call
elastic_req
on the client - Work with the raw http response
- Or call
parse
to get a concrete response or API error
Minimal Example
Execute a search request asynchronously and parse the response:
//HTTP POST /myindex/mytype/_search #[macro_use] extern crate serde_json; extern crate elastic_reqwest; extern crate tokio_core; extern crate futures; use tokio_core::reactor::Core; use futures::Future; use serde_json::Value; use elastic_reqwest::{AsyncElasticClient, AsyncFromResponse, parse}; use elastic_reqwest::req::SearchRequest; use elastic_reqwest::res::SearchResponse; let mut core = Core::new().unwrap(); let (client, params) = elastic_reqwest::async::default(&core.handle()).unwrap(); let search = SearchRequest::for_index_ty( "myindex", "mytype", json!({ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "geo_distance": { "distance": "20km", "location": { "lat": 37.776, "lon": -122.41 } } } } } }) ); let search_future = client .elastic_req(¶ms, search) .and_then(|http_res| parse::<SearchResponse<Value>>().from_response(http_res)) .and_then(|search_res| { println!("{:?}", search_res); Ok(()) }); core.run(search_future).unwrap();
Search Request with Url Param
Execute a search request with a url parameter:
//HTTP GET /myindex/mytype/_search?q=* extern crate serde_json; extern crate elastic_reqwest; use serde_json::Value; use elastic_reqwest::{ SyncElasticClient, SyncFromResponse, RequestParams, parse }; use elastic_reqwest::req::SimpleSearchRequest; use elastic_reqwest::res::SearchResponse; let (client, _) = elastic_reqwest::sync::default().unwrap(); let params = RequestParams::default() .url_param("pretty", true) .url_param("q", "*"); let search = SimpleSearchRequest::for_index_ty( "myindex", "mytype" ); let http_res = client.elastic_req(¶ms, search).unwrap(); let search_res = parse::<SearchResponse<Value>>().from_response(http_res).unwrap();
See Also
elastic
. A higher-level Elasticsearch client that useselastic_reqwest
as its HTTP layer.rs-es
. A higher-level Elasticsearch client that provides strongly-typed Query DSL buiilders.json_str
A library for generating minified json strings from Rust syntax.
Links
Re-exports
pub use self::sync::SyncBody; |
pub use self::sync::SyncElasticClient; |
pub use self::sync::SyncFromResponse; |
pub use self::async::AsyncBody; |
pub use self::async::AsyncElasticClient; |
pub use self::async::AsyncFromResponse; |
Modules
async |
Asynchronous http client. |
req |
Request types. |
res |
Response types. |
sync |
Synchronous http client. |
Structs
RequestParams |
Misc parameters for any request. |
Enums
Error |
An error sending a request or parsing a response. |
Functions
parse |