Crate elastic [] [src]

Elasticsearch API Client

An efficient native client for the Elasticsearch REST API.

Supported Versions

elastic Elasticsearch
0.x 5.x

This crate is mostly a meta-package composed of a number of smaller pieces including:

  • elastic_reqwest HTTP transport
  • elastic_requests API request builders
  • elastic_responses API response parser
  • elastic_types tools for document and mapping APIs

This crate glues these libraries together with some simple assumptions about how they're going to be used.

Usage

This crate is on crates.io. To get stated, add elastic to your Cargo.toml:

[dependencies]
elastic = "*"

The following optional dependencies may also be useful:

elastic_types_derive = { version = "*", features = ["elastic"] }
json_str = "*"
serde = "*"
serde_derive = "*"

Then reference in your crate root:

extern crate elastic;

// Optional
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate elastic_types_derive;
#[macro_use]
extern crate json_str;

Examples

Making requests

use elastic::prelude::*;

let client = Client::new(RequestParams::default()).unwrap();

let req = PingRequest::new();
let response = client.request(req).send().unwrap();

Configuring requests

Create a set of request parameters that are passed to each request:

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

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

Requests can override parameter values:

let response = client.request(req)
    .params(|p| p.url_param("pretty", false))
    .send()
    .unwrap();

Defining document types

Derive Serialize, Deserialize and ElasticType on your document types:

#[derive(Serialize, Deserialize, ElasticType)]
struct MyType {
    pub id: i32,
    pub title: String,
    pub timestamp: Date<DefaultDateFormat>
}

Use your document type to build index requests:

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

let index = Index::from("index");
let id = Id::from(doc.id.to_string());

let req = IndexRequest::try_for_doc((index, id, &doc)).unwrap();

Use your document type to build mapping requests:

let index = Index::from("index");
let mapping = MyType::mapping();

let req = IndicesPutMappingRequest::try_for_mapping((index, mapping)).unwrap();

For more details on document types, see elastic_types.

Getting Responses

Call response on a sent request to get a SearchResponse or GetResponse:

let response = client.request(req)
    .send()
    .and_then(|res| res.response::<SearchResponse<MyType>>());

Call raw on a sent request to get a raw reqwest::Response:

let response = client.request(req)
    .send()
    .map(|res| res.raw());

You should avoid depending on raw too much, because it's a leaky abstraction that may be removed in the future.

Modules

client

HTTP client, requests and responses.

error

Client-side error types.

http

HTTP headers and status codes.

prelude

A glob import for convenience.

types

Indexable documents and type mapping.