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 = "*"
On the nightly
channel, you can use the nightly
feature
to avoid copying borrowed body buffers:
[dependencies] elastic_reqwest = { version = "*", features = ["nightly"] } reqwest = "*"
Then reference in your crate root:
extern crate reqwest; extern crate elastic_reqwest as cli; use cli::{ElasticClient, ParseResponse, parse};
The Gist
- Create a
reqwest::Client
- 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
Ping the availability of your cluster:
//HTTP HEAD / use cli::ElasticClient; use cli::req::PingRequest; let (client, params) = cli::default().unwrap(); client.elastic_req(¶ms, PingRequest::new()).unwrap();
Search Request with Url Param
Execute a search query with a url parameter:
//HTTP GET /myindex/mytype/_search?q=* extern crate elastic_reqwest as cli; use cli::{ ElasticClient, ParseResponse, RequestParams, parse }; use cli::req::SimpleSearchRequest; use cli::res::SearchResponse; let (client, _) = cli::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>().from_response(http_res).unwrap();
Search Request with Json
Using the json_str
crate, you can execute
queries using pure json:
//HTTP POST /myindex/mytype/_search #[macro_use] extern crate json_str; extern crate elastic_reqwest as cli; use cli::{ElasticClient, ParseResponse, parse}; use cli::req::SearchRequest; use cli::res::SearchResponse; let (client, params) = cli::default().unwrap(); let search = SearchRequest::for_index_ty( "myindex", "mytype", json_str!({ 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>().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
Modules
req |
Request types. |
res |
Response types. |
Structs
RequestParams |
Misc parameters for any request. |
Traits
ElasticClient |
Represents a client that can send Elasticsearch requests. |
IntoReqwestBody |
A type that can be converted into a request body. |
ParseResponse |
Represents a response that can be parsed into a concrete Elasticsearch response. |
Functions
default |
Get a default |
parse |
Try parse a http response into a concrete type. |