Module elastic::client [] [src]

HTTP client, requests and responses.

This module contains the HTTP client, as well as request and response types.

The request process

The pieces involved in sending a request and parsing the response are modular. Each one exposes Rust traits you can implement to support your own logic. If you just want to send search/get requests and parse a search/get response then you won't need to worry about this so much.

The basic flow from request to response is:

1) Turn a concrete request type into a RequestBuilder:

[RequestType] ---> [Client.request()] ---> [RequestBuilder]

2) Send the RequestBuilder and get a ResponseBuilder:

[RequestBuilder.send()] ---> [ResponseBuilder] 

3) Parse the ResponseBuilder to a response type:

[ResponseBuilder.response()] ---> [ResponseType]

The example below shows how these pieces fit together in code.

Examples

This example sends a simple SearchRequest, with the steps in the above process labelled:

// Create a `Client`
let client = Client::new(RequestParams::default()).unwrap();
 
// Create a `SearchRequest` for all indices
let req = {
    let body = json_str!({
        query: {
            query_string: {
                query: "*"
            }
        }
    });
     
    SearchRequest::for_index("_all", body)
};
 
// Send the request and read the response as a `SearchResponse`
let res = client.request(req) // 1
                .send() // 2
                .and_then(|res| res.response::<SearchResponse<Value>>()); // 3
 
match res {
    Ok(response) => {
        // Iterate through the response hits
        for hit in response.hits() {
            println!("{:?}", hit);
        }
    },
    Err(e) => {
        match *e.kind() {
            ErrorKind::Api(ref e) => {
                // handle a REST API error
            },
            ref e => {
                // handle a HTTP or JSON error
            }
        }
    }
}

Modules

requests

Request types for the Elasticsearch REST API.

responses

Response types for the Elasticsearch REST API.

Structs

Client

A HTTP client for the Elasticsearch REST API.

RequestBuilder

A builder for a request.

RequestParams

Misc parameters for any request.

ResponseBuilder

A builder for a response.