Struct elastic::client::Client [] [src]

pub struct Client { /* fields omitted */ }

A HTTP client for the Elasticsearch REST API.

The Client is a structure that lets you create and send RequestBuilders. It's mostly a thin wrapper over a reqwest::Client and is re-usable.

Examples

Create a Client for an Elasticsearch node at es_host:9200:

let params = RequestParams::new("http://es_host:9200").url_param("pretty", true);

let client = Client::new(params).unwrap();

Methods

impl Client
[src]

Create a RequestBuilder for a search request.

Examples

Run a simple Query String query for a DocumentType called MyType:

let response = client.search::<MyType>()
                     .index("myindex")
                     .body(json_str!({
                         query: {
                             query_string: {
                                 query: "*"
                             }
                         }
                     }))
                     .send()
                     .unwrap();

// Iterate through the hits (of type `MyType`)
for hit in response.hits() {
    println!("{:?}", hit);
}

For more details on document types and mapping, see the types module.

It's also possible to use serde_json::Values as documents when searching:

let response = client.search::<Value>()
                     .index("myindex")
                     .ty(Some("mytype"))
                     .send()
                     .unwrap();

impl Client
[src]

Create a RequestBuilder for a get request.

Examples

Get a DocumentType called MyType with an id of 1:

let response = client.get_document::<MyType>(index("myindex"), id(1))
                     .send()
                     .unwrap();

if let Some(doc) = response.source {
    println!("id: {}", doc.id);
}

For more details on document types, see the types module.

Get the same document as a serde_json::Value:

let response = client.get_document::<Value>(index("myindex"), id(1))
                     .ty("mytype")
                     .send()
                     .unwrap();

impl Client
[src]

Create a RequestBuilder for an index request.

Examples

Index a DocumentType called MyType with an id of 1:

let doc = MyType {
    id: 1,
    title: String::from("A title"),
    timestamp: Date::now()
};

let response = client.index_document(index("myindex"), id(doc.id), doc)
                     .send()
                     .unwrap();

For more details on document types and mapping, see the types module.

impl Client
[src]

Create a RequestBuilder for a put mapping request.

Examples

Put the document mapping for a DocumentType called MyType:

client.put_mapping::<MyType>(index("myindex"))
      .send()
      .unwrap();

For more details on document types and mapping, see the types module.

impl Client
[src]

Create a RequestBuilder for a create index request.

Examples

Create an index called myindex:

let my_index = index("myindex");

let response = client.create_index(my_index).send().unwrap();

assert!(response.acknowledged);

Create an index with settings and document mappings for a DocumentType called MyType:

let my_index = index("myindex");

let body = json!({
    "settings": {
        "index": {
            "number_of_shards": 3,
            "number_of_replicas": 2
        }
    },
    "mappings": {
        MyType::name(): IndexDocumentMapping::from(MyType::mapping())
    }
});

let response = client.create_index(my_index)
                     .body(body.to_string())
                     .send()
                     .unwrap();

assert!(response.acknowledged);

For more details on document types and mapping, see the types module.

impl Client
[src]

Create a raw RequestBuilder that can be configured before sending.

The request method accepts any type that can be converted into a HttpRequest<'static>, which includes the endpoint types in the endpoints module.

Examples

Send a cluster ping and read the returned metadata:

// `PingRequest` implements `Into<HttpRequest>`
let req = PingRequest::new();
    
// Turn the `PingRequest` into a `RequestBuilder`
let builder = client.request(req);
    
// Send the `RequestBuilder` and parse as a `PingResponse`
let ping = builder.send().and_then(into_response::<PingResponse>).unwrap();

println!("cluster: {}", ping.name);

impl Client
[src]

Create a new client for the given parameters.

The parameters given here are used as the defaults for any request made by this client, but can be overriden on a per-request basis. This method can return a HttpError if the underlying reqwest::Client fails to create.

Examples

Create a Client with default parameters:

let client = ClientBuilder::new().build().unwrap();

Create a Client for a specific node:

let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();

See RequestParams for more configuration options.