Crate elastic [] [src]

Elasticsearch API Client

A modular and efficient native client for the Elasticsearch REST API.

Supported Versions

elastic Elasticsearch
0.x 5.x

This crate depends heavily on the following crates:

elastic is designed to scale up to the complexity of Elasticsearch's API, and with the complexity of the environments Elasticsearch is deployed in.


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

Be careful when using this code, it's not being tested!
elastic = "*"
elastic_derive = "*"

The following optional dependencies may also be useful:

Be careful when using this code, it's not being tested!
serde = "*"
serde_json = "*"
serde_derive = "*"

Then reference in your crate root:

extern crate elastic;
extern crate elastic_derive;


Creating a synchronous client

The SyncClient type is an easy way to interact with an Elasticsearch cluster. A synchronous client can be created through the SyncClientBuilder.

The builder allows you to configure default parameters for all requests:

use elastic::prelude::*;
use elastic::http::header::Authorization;

let builder = SyncClientBuilder::new()
    .params(|p| p
        .url_param("pretty", true)
        .header(Authorization("let me in".to_owned())));

let client =;

Individual requests can override these parameter values:

let client = SyncClientBuilder::new().build()?;

let response =<Value>()
                     .params(|p| p.url_param("pretty", false))

elastic also offers an AsyncClient. For more details, see the client and requests modules.

Making requests

For a list of common client methods, see here.

Each endpoint in the Elasticsearch REST API is provided as a strongly-typed structure. The client offers high-level request builders for some common Elasticsearch operations.

Getting and Indexing documents

The Document Mapping API is provided as a custom derive plugin and set of Rust traits. Derive Serialize, Deserialize and ElasticType on your document types:

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

Call Client.document_put_mapping to ensure an index has the right mapping for your document types:


Then call Client.document_index to index documents in Elasticsearch:

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

let response = client.document_index(index("myindex"), id(, doc)

Call Client.document_get to retrieve a single document from an index:

let response = client.document_get::<MyType>(index("myindex"), id(1))

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

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

Searching documents

Call to execute Query DSL queries:

let response =<MyType>()
                         "query": {
                            "query_string": {
                                "query": "*"

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

Crate design

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 parsers
  • 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.



pub use error::Error;



HTTP client, requests and responses.


Client-side error types.


Raw HTTP modules.


A glob import for convenience.


Indexable documents and type mapping.