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

pub struct Client<TSender> { /* fields omitted */ }

A HTTP client for the Elasticsearch REST API.

The Client is a structure that lets you create and send request builders. Client is generic over a Sender, but rather than use Client directly, use one of:

Examples

Create a synchronous Client and send a ping request:

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

let response = client.request(PingRequest::new())
                     .send()?
                     .into_response::<PingResponse>()?;

Create an asynchronous Client and send a ping request:

let mut core = Core::new()?;
let client = AsyncClientBuilder::new().build(&core.handle())?;

let response_future = client.request(PingRequest::new())
                            .send()
                            .and_then(|res| res.into_response::<PingResponse>());

core.run(response_future)?;

Methods

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a RawRequestBuilder with this Client 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()?.into_response::<PingResponse>()?;

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

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a SearchRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

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

let query = "a query string";

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

// 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()?;

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a GetRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Get a DocumentType called MyType with an id of 1:

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

if let Some(doc) = response.into_document() {
    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.document_get::<Value>(index("myindex"), id(1))
                     .ty("mytype")
                     .send()?;

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a IndexRequestBuilder with this Client that can be configured before sending.

For more details, see:

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.document_index(index("myindex"), id(doc.id), doc)
                     .send()?;
    
assert!(response.created());

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

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create an UpdateRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Update a DocumentType called MyType with an id of 1 using a new document value:

let response = client.document_update::<MyType>(index("myindex"), id(1))
                     .doc(new_doc)
                     .send()?;

assert!(response.updated());

The document doesn't necessarily need to be of the same type so partial updates can be made. Be careful though because this can lead to unexpected results or runtime errors if the document types don't align:

let response = client.document_update::<MyType>(index("myindex"), id(1))
                     .doc(json!({
                         "title": "New Title"
                     }))
                     .send()?;

assert!(response.updated());

Documents can also be updated using a script:

let response = client.document_update::<MyType>(index("myindex"), id(1))
                     .script(r#"ctx._source.title = "New Title""#)
                     .send()?;

assert!(response.updated());

Scripts can be configured with parameters:

let response = client.document_update::<MyType>(index("myindex"), id(1))
                     .script_fluent("ctx._source.title = params.newTitle", |script| script
                        .param("newTitle", "New Title"))
                     .send()?;

assert!(response.updated());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a DeleteRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Delete a DocumentType called MyType with an id of 1:

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

assert!(response.deleted());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a PutMappingRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Put the document mapping for a DocumentType called MyType:

let response = client.document_put_mapping::<MyType>(index("myindex"))
                     .send()?;

assert!(response.acknowledged());

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

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create an IndexCreateRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Create an index called myindex:

let my_index = index("myindex");

let response = client.index_create(my_index).send()?;

assert!(response.acknowledged());

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

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

let response = client.index_create(index("myindex"))
                     .body(body.to_string())
                     .send()?;

assert!(response.acknowledged());

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

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Open an IndexOpenRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Open an index called myindex:

let response = client.index_open(index("myindex")).send()?;

assert!(response.acknowledged());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create an IndexCloseRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Close an index called myindex:

let response = client.index_close(index("myindex")).send()?;

assert!(response.acknowledged());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Delete a IndexDeleteRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Delete an index called myindex:

let response = client.index_delete(index("myindex")).send()?;

assert!(response.acknowledged());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Open an IndexExistsRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Check whether an index called myindex exists:

let response = client.index_exists(index("myindex")).send()?;

assert!(response.exists());

impl<TSender> Client<TSender> where
    TSender: Sender
[src]

[src]

Create a PingRequestBuilder with this Client that can be configured before sending.

For more details, see:

Examples

Ping an Elasticsearch node.

let response = client.ping().send()?;

println!("node: {}", response.name());

Trait Implementations

impl<TSender: Clone> Clone for Client<TSender>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl<TSender> Send for Client<TSender> where
    TSender: Send

impl<TSender> Sync for Client<TSender> where
    TSender: Sync