Struct elastic_responses::bulk::BulkResponse [] [src]

pub struct BulkResponse<TIndex = String, TType = String, TId = String> { /* fields omitted */ }

Response for a bulk request.

Individual bulk items are a Result of OkItem or ErrorItem and can be iterated over. Any individual bulk item may be an Err(ErrorItem), so it's important to check them. The is_ok and is_err methods on BulkResponse make it easier to assert there are no errors.

Examples

Send a bulk request and iterate through the results:

let response: BulkResponse = do_request();

// Check if the response contains any errors
if response.is_err() {
    println!("some bulk items failed");
}

// Iterate through all items
for item in response {
    match item {
        Ok(item) => {
            // Do something with the `OkItem`s
            println!("ok: {:?}", item)
        },
        Err(item) => {
            // Do something with the `ErrorItem`s
            println!("err: {:?}", item)
        }
    }
}

Use iter to iterate over individual items without taking ownership of them:

let response: BulkResponse = do_request();

// Do something with successful items for index `myindex`
let item_iter = response.iter()
                        .filter_map(Result::ok)
                        .filter(|o| o.index() == "myindex");

for item in item_iter {
    // Do something with the `OkItem`s
    println!("ok: {:?}", item);
}

Optimising bulk responses

If you're only interested in bulk items that failed, see BulkErrorsResponse. It can avoid allocating bulk item responses that will never be processed.

Both the BulkResponse and BulkErrorsResponse types have generic parameters for the index, type and id fields. If your bulk items have a small set of possible values for these fields you can avoid allocating Strings on the heap by using an alternative type, like an enum.

In the example below, we expect all bulk items to use either a type called mytypea or mytypeb and an index called myindex:

#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
enum Index {
    MyIndex,
}

#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
enum Type {
    MyTypeA,
    MyTypeB,
}

let bulk: BulkResponse<Index, Type> = do_request();

Other crates that can avoid allocating strings:

Taking BulkResonse as an argument

The BulkResponse type has three default generic parameters for the index, type and id fields. If you need to accept a BulkResponse as a function argument, you should specify these generics. Otherwise the function will only accept a default BulkResponse:

// Do: Supports any BulkResponse
fn takes_any_response<TIndex, TType, TId>(res: BulkResponse<TIndex, TType, TId>) { 

}

// Don't: Only supports default BulkResponse
fn takes_default_response(res: BulkResponse) {

}

Methods

impl<TIndex, TType, TId> BulkResponse<TIndex, TType, TId>
[src]

Time in milliseconds it took for Elasticsearch to process the request.

Returns true if all bulk items succeeded.

Returns true if any bulk items failed.

Iterate through the bulk items.

The items in this iterator are a standard Result where Ok means the item succeeded and Err means it failed.

To move out of the items in a BulkResponse instead of borrowing them, call into_iter.

Examples

Iterate through the individual items in a BulkResponse:

let response: BulkResponse = do_request();
    
// Iterate through all items
for item in response.iter() {
    match item {
        Ok(ref item) => {
            // Do something with the `OkItem`s
            println!("ok: {:?}", item)
        },
        Err(ref item) => {
            // Do something with the `ErrorItem`s
            println!("err: {:?}", item)
        }
    }
}

Trait Implementations

impl<TIndex: Debug, TType: Debug, TId: Debug> Debug for BulkResponse<TIndex, TType, TId>
[src]

Formats the value using the given formatter.

impl<TIndex: Clone, TType: Clone, TId: Clone> Clone for BulkResponse<TIndex, TType, TId>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<TIndex, TType, TId> IntoIterator for BulkResponse<TIndex, TType, TId>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<TIndex, TType, TId> IsOk for BulkResponse<TIndex, TType, TId>
[src]

Inspect the http response to determine whether or not it succeeded.