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 String
s 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:
string-cache
crate for interning string valuesinlinable-string
crate for storing small strings on the stack
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]
fn took(&self) -> u64
Time in milliseconds it took for Elasticsearch to process the request.
fn is_ok(&self) -> bool
Returns true
if all bulk items succeeded.
fn is_err(&self) -> bool
Returns true
if any bulk items failed.
fn iter(&self) -> ResultIter<TIndex, TType, TId>
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]
impl<TIndex: Clone, TType: Clone, TId: Clone> Clone for BulkResponse<TIndex, TType, TId>
[src]
fn clone(&self) -> BulkResponse<TIndex, TType, TId>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
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?
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more
impl<TIndex, TType, TId> IsOk for BulkResponse<TIndex, TType, TId>
[src]
fn is_ok<B: ResponseBody>(
head: HttpResponseHead,
body: Unbuffered<B>
) -> Result<MaybeOkResponse<B>, ParseResponseError>
head: HttpResponseHead,
body: Unbuffered<B>
) -> Result<MaybeOkResponse<B>, ParseResponseError>
Inspect the http response to determine whether or not it succeeded.