Elasticsearch REST API Client
A lightweight implementation of the Elasticsearch API based on the
hyper
HTTP client.
Each API endpoint is represented as its own function, so each possible http route gets its own function. This library makes very few assumptions, leaving it up to you to decide what to invest your precious CPU cycles into.
The entire 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_hyper
and hyper
to your Cargo.toml
:
[dependencies]
elastic_hyper = "*"
hyper = "*"
For Windows
, you may need to exclude openssl
or the build can fail:
[dependencies]
elastic_hyper = { version = "*", default-features = false }
hyper = { version = "*", default-features = false }
Then reference in your crate root:
extern crate elastic_hyper as elastic;
extern crate hyper;
Minimal Example
Ping the availability of your cluster:
//HTTP HEAD /
# extern crate hyper;
# extern crate elastic_hyper as elastic;
# fn main() {
let (mut client, params) = elastic::default();
elastic::ping::head(&mut client, ¶ms).unwrap();
# }
Search Request with Url Param
Execute a search query with a url parameter:
//HTTP GET /myindex/mytype/_search?q='my string'
# extern crate hyper;
# extern crate elastic_hyper as elastic;
# fn main() {
let mut client = hyper::Client::new();
let mut params = elastic::RequestParams::default()
.url_params(vec![
("q", "'my string'".to_owned()),
("pretty", "true".to_owned())
]);
elastic::search::get_index_type(&mut client, ¶ms, "myindex", "mytype").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 hyper;
# extern crate elastic_hyper as elastic;
# fn main() {
let (mut client, params) = elastic::default();
elastic::search::post_index_type(&mut client, ¶ms,
"myindex", "mytype", &json_str!({
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "20km",
"location": {
"lat": 37.776,
"lon": -122.41
}
}
}
}
}
})
).unwrap();
# }
See more examples.
See Also
rs-es
An alternative Elasticsearch client for Rust that provides an implementation of the Query DSL.
elastic_types
A library that implements the core datatypes in Elasticsearch documents and automatically generates a json mapping from your Rust structures.
json_str
A library for generating minified json strings from Rust syntax.