Overview

edit

This is the official Rust client for Elasticsearch. Full documentation is hosted on docs.rs — this page provides only an overview.

Further resources:

Features

edit
  • Fluent builders for all Elasticsearch REST API endpoints
  • Persistent keep-alive connections
  • TLS support with system or custom certificates
  • Proxy support with authentication
  • Async support with Tokio

Elasticsearch Version Compatibility

edit

The Elasticsearch Rust client is forward compatible; meaning that the client supports communicating with greater minor versions of Elasticsearch. Elasticsearch language clients are also backwards compatible with lesser supported minor Elasticsearch versions.

Create a client

edit

To create a client to make API calls to Elasticsearch running on \http://localhost:9200

let client = Elasticsearch::default();

Alternatively, you can create a client to make API calls against Elasticsearch running on a specific url::Url

let transport = Transport::single_node("https://example.com")?;
let client = Elasticsearch::new(transport);

If you’re running against an Elasticsearch deployment in Elastic Cloud, a client can be created using a Cloud ID and credentials retrieved from the Cloud web console

let cloud_id = "<cloud id from cloud web console>";
let credentials = Credentials::Basic("<username>".into(), "<password>".into());
let transport = Transport::cloud(cloud_id, credentials)?;
let client = Elasticsearch::new(transport);

Making API calls

edit

The following makes an API call to tweets/_search with the json body {"query":{"match":{"message":"Elasticsearch"}}}

let response = client
    .search(SearchParts::Index(&["tweets"]))
    .from(0)
    .size(10)
    .body(json!({
        "query": {
            "match": {
                "message": "Elasticsearch rust"
            }
        }
    }))
    .send()
    .await?;

let response_body = response.json::<Value>().await?;
let took = response_body["took"].as_i64().unwrap();
for hit in response_body["hits"]["hits"].as_array().unwrap() {
    // print the source document
    println!("{:?}", hit["_source"]);
}