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

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]

pub fn took(&self) -> u64[src]

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

pub fn is_ok(&self) -> bool[src]

Returns true if all bulk items succeeded.

pub fn is_err(&self) -> bool[src]

Returns true if any bulk items failed.

Important traits for ResultIter<'a, TIndex, TType, TId>
pub fn iter(&self) -> ResultIter<TIndex, TType, TId>[src]

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, TType, TId> IsOkOnSuccess for BulkResponse<TIndex, TType, TId>[src]

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

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

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

type Item = <Self::IntoIter as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = ResultIntoIter<TIndex, TType, TId>

Which kind of iterator are we turning this into?

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

impl<'de, TIndex, TType, TId> Deserialize<'de> for BulkResponse<TIndex, TType, TId> where
    TIndex: Deserialize<'de>,
    TType: Deserialize<'de>,
    TId: Deserialize<'de>, 
[src]

Auto Trait Implementations

impl<TIndex, TType, TId> Send for BulkResponse<TIndex, TType, TId> where
    TId: Send,
    TIndex: Send,
    TType: Send

impl<TIndex, TType, TId> Sync for BulkResponse<TIndex, TType, TId> where
    TId: Sync,
    TIndex: Sync,
    TType: Sync

Blanket Implementations

impl<T> IsOk for T where
    T: IsOkOnSuccess
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]