[][src]Crate elastic_responses

Elasticsearch Response Iterators

A crate to handle parsing and handling Elasticsearch search results which provides convenient iterators to step through the results returned. It is designed to work with elastic-reqwest. It also re-exports serde_json::Value for convenient anonymous json objects.

This crate provides parsers that can be used to convert a http response into a concrete type or an API error.


This crate is on crates.io. Add elastic_responses to your Cargo.toml:

elastic_responses = "~0.21.0-pre.4"

Use the parse function to deserialise a http response to a Result<T, ApiError> for some concrete response type T.


Run a Query DSL query, then iterate through the results:

// Send a search request and read as a response
let (response_status, response_body) = do_request();

// Parse body to JSON as an elastic_responses::SearchResponse object
// If the response is an API error then it'll be parsed into a friendly Rust error
let response = parse::<SearchResponse<Value>>().from_slice(response_status, response_body).unwrap();

// Iterate over hits. Could also use `documents`
for hit in response.hits() {
    let score = hit.score().unwrap_or(f32::default());
    let doc = hit.document();

    println!("score: {}", score);
    println!("doc: {:?}", doc);

// Agregations are flattened into individual stats metrics
for agg in response.aggs() {
    let min_ack_pkts = agg["min_ack_pkts_sent"].as_u64().unwrap();
    let max_ack_pkts = agg["max_ack_pkts_sent"].as_u64().unwrap();

    println!("min: {}, max: {}", min_ack_pkts, max_ack_pkts);

Any type that implements Deserialize can be used as the document type in the search response:

struct MyDocument {
    title: String,
    description: String

let (response_status, response_body) = do_request();

let response = parse::<SearchResponse<MyDocument>>().from_slice(response_status, response_body).unwrap();

for doc in response.documents() {
    println!("title: {}", doc.title);
    println!("description: {}", doc.description);

Run a Get Document request, and handle cases where the document wasn't found or the index doesn't exist:

// Send a document get request and read as a response
let (response_status, response_body) = do_request();

let response = parse::<GetResponse<Value>>().from_slice(response_status, response_body);

match response.map(|res| res.into_document()) {
    Ok(Some(doc)) => {
        // The document was found
    Ok(None) => {
        // The document was not found
    Err(ResponseError::Api(ApiError::IndexNotFound { index })) => {
        // The index doesn't exist
    _ => {
        // Some other error

As with SearchResponse, any type that implements Deserialize can be used as the generic document type in a GetResponse.


pub use self::bulk::BulkErrorsResponse;
pub use self::bulk::BulkResponse;
pub use self::search::SearchResponse;
pub use self::parsing::parse;



Response types for a bulk request.


Error types from Elasticsearch.


Response type parsing.


Response types for a search request.



A standard command acknowledgement response.


Response for a delete document request.


Response for a get document request.


Response for an index document request.


Response for an index exists request.


Response for a cluster ping request.


Returned hits metadata.


An HTTP status code (status-code in RFC 7230 et al.).


Response for a update document request.



Represents any valid JSON value.