1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
/*! Response types for a [bulk request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). */ use common::{ DefaultAllocatedField, DocumentResult, Shards, }; use serde::de::{ Deserialize, Deserializer, Error as DeError, MapAccess, SeqAccess, Visitor, }; use serde_json::Value; use parsing::IsOkOnSuccess; use std::cmp; use std::error::Error; use std::fmt; use std::marker::PhantomData; use std::slice::Iter; use std::vec::IntoIter; type BulkError = Value; /** Response for a [bulk request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). Individual bulk items are a `Result` of [`OkItem`](struct.OkItem.html) or [`ErrorItem`](struct.ErrorItem.html) 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: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # fn do_request() -> BulkResponse { unimplemented!() } # fn main() { 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: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # fn do_request() -> BulkResponse { unimplemented!() } # fn main() { 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`](struct.BulkErrorsResponse.html). 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`: ```no_run # extern crate serde; # #[macro_use] extern crate serde_derive; # extern crate serde_json; # extern crate elastic_responses; # use elastic_responses::*; # fn main() { # fn do_request() -> BulkResponse<Index, Type> { unimplemented!() } #[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`](https://github.com/servo/string-cache) crate for interning string values - [`inlinable-string`](https://github.com/fitzgen/inlinable_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`: ``` # use elastic_responses::*; // 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) { } ``` */ #[derive(Deserialize, Debug, Clone)] #[serde(bound( deserialize = "TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>" ))] pub struct BulkResponse< TIndex = DefaultAllocatedField, TType = DefaultAllocatedField, TId = DefaultAllocatedField, > { took: u64, errors: bool, #[serde(deserialize_with = "deserialize_bulk_items")] items: Vec<ItemResult<TIndex, TType, TId>>, } impl<TIndex, TType, TId> BulkResponse<TIndex, TType, TId> { /** Time in milliseconds it took for Elasticsearch to process the request. */ pub fn took(&self) -> u64 { self.took } /** Returns `true` if all bulk items succeeded. */ pub fn is_ok(&self) -> bool { !self.errors } /** Returns `true` if any bulk items failed. */ pub fn is_err(&self) -> bool { self.errors } /** 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`: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # fn do_request() -> BulkResponse { unimplemented!() } # fn main() { 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) } } } # } ``` */ pub fn iter(&self) -> ResultIter<TIndex, TType, TId> { ResultIter(self.items.iter()) } } impl<TIndex, TType, TId> IntoIterator for BulkResponse<TIndex, TType, TId> { type Item = <Self::IntoIter as Iterator>::Item; type IntoIter = ResultIntoIter<TIndex, TType, TId>; fn into_iter(self) -> Self::IntoIter { ResultIntoIter(self.items.into_iter()) } } /** An owning iterator for a bulk item that may have succeeded or failed. */ pub struct ResultIntoIter<TIndex, TType, TId>(IntoIter<ItemResult<TIndex, TType, TId>>); impl<TIndex, TType, TId> Iterator for ResultIntoIter<TIndex, TType, TId> { type Item = ItemResult<TIndex, TType, TId>; fn next(&mut self) -> Option<Self::Item> { self.0.next() } } /** A borrowing iterator for a bulk item that may have succeeded or failed. */ pub struct ResultIter<'a, TIndex: 'a, TType: 'a, TId: 'a>(Iter<'a, ItemResult<TIndex, TType, TId>>); impl<'a, TIndex: 'a, TType: 'a, TId: 'a> Iterator for ResultIter<'a, TIndex, TType, TId> { type Item = ItemResultBrw<'a, TIndex, TType, TId>; fn next(&mut self) -> Option<Self::Item> { self.0.next().map(|item| item.as_ref()) } } /** Response for a [bulk request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). This type only accumulates bulk items that failed. It can be more efficient if you only care about errors. Individual bulk items are [`ErrorItem`](struct.ErrorItem.html) and can be iterated over. # Examples Send a bulk request and iterate through the errors: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # use elastic_responses::bulk::Action; # fn do_request() -> BulkErrorsResponse { unimplemented!() } # fn main() { let response: BulkErrorsResponse = do_request(); // Do something with failed items for item in response { match item.action() { Action::Delete => (), // Ignore failed deletes _ => println!("err: {:?}", item) } } # } ``` Use `iter` to iterate over individual errors without taking ownership of them: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # fn do_request() -> BulkErrorsResponse { unimplemented!() } # fn main() { let response: BulkErrorsResponse = do_request(); // Do something with errors for index `myindex` let item_iter = response.iter() .filter(|o| o.index() == "myindex"); for item in item_iter { println!("err: {:?}", item); } # } ``` # Taking `BulkErrorsResponse` as an argument The `BulkErrorsResponse` type has three default generic parameters for the index, type and id fields. If you need to accept a `BulkErrorsResponse` as a function argument, you should specify these generics. Otherwise the function will only accept a default `BulkErrorsResponse`: ``` # use elastic_responses::*; // Do: Supports any BulkErrorsResponse fn takes_any_response<TIndex, TType, TId>(res: BulkErrorsResponse<TIndex, TType, TId>) { } // Don't: Only supports default BulkErrorsResponse fn takes_default_response(res: BulkErrorsResponse) { } ``` */ #[derive(Deserialize, Debug, Clone)] #[serde(bound( deserialize = "TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>" ))] pub struct BulkErrorsResponse< TIndex = DefaultAllocatedField, TType = DefaultAllocatedField, TId = DefaultAllocatedField, > { took: u64, errors: bool, #[serde(deserialize_with = "deserialize_bulk_item_errors")] items: Vec<ErrorItem<TIndex, TType, TId>>, } impl<TIndex, TType, TId> IntoIterator for BulkErrorsResponse<TIndex, TType, TId> { type Item = <Self::IntoIter as Iterator>::Item; type IntoIter = ErrorIntoIter<TIndex, TType, TId>; fn into_iter(self) -> Self::IntoIter { ErrorIntoIter(self.items.into_iter()) } } /** An owning iterator for a bulk item that failed. */ pub struct ErrorIntoIter<TIndex, TType, TId>(IntoIter<ErrorItem<TIndex, TType, TId>>); impl<TIndex, TType, TId> Iterator for ErrorIntoIter<TIndex, TType, TId> { type Item = ErrorItem<TIndex, TType, TId>; fn next(&mut self) -> Option<Self::Item> { self.0.next() } } /** A borrowing iterator for a bulk item that failed. */ pub struct ErrorIter<'a, TIndex: 'a, TType: 'a, TId: 'a>(Iter<'a, ErrorItem<TIndex, TType, TId>>); impl<'a, TIndex: 'a, TType: 'a, TId: 'a> Iterator for ErrorIter<'a, TIndex, TType, TId> { type Item = &'a ErrorItem<TIndex, TType, TId>; fn next(&mut self) -> Option<Self::Item> { self.0.next() } } impl<TIndex, TType, TId> BulkErrorsResponse<TIndex, TType, TId> { /** Time in milliseconds it took for Elasticsearch to process the request. */ pub fn took(&self) -> u64 { self.took } /** Returns `true` if all bulk items succeeded. */ pub fn is_ok(&self) -> bool { !self.errors } /** Returns `true` if any bulk itemss failed. */ pub fn is_err(&self) -> bool { self.errors } /** Iterate through the bulk item errors. Items in this iterator all all errors that occurred while handling the bulk request. # Examples Iterate through the individual items in a `BulkErrorsResponse`: ```no_run # extern crate elastic_responses; # use elastic_responses::*; # fn do_request() -> BulkErrorsResponse { unimplemented!() } # fn main() { let response: BulkErrorsResponse = do_request(); // Iterate through all items for item in response.iter() { // Do something with failed items println!("err: {:?}", item) } # } ``` */ pub fn iter(&self) -> ErrorIter<TIndex, TType, TId> { ErrorIter(self.items.iter()) } } type ItemResult<TIndex, TType, TId> = Result<OkItem<TIndex, TType, TId>, ErrorItem<TIndex, TType, TId>>; type ItemResultBrw<'a, TIndex, TType, TId> = Result<&'a OkItem<TIndex, TType, TId>, &'a ErrorItem<TIndex, TType, TId>>; /** A successful bulk response item. */ #[derive(Debug, Clone)] pub struct OkItem< TIndex = DefaultAllocatedField, TType = DefaultAllocatedField, TId = DefaultAllocatedField, > { action: Action, index: TIndex, ty: TType, id: TId, version: Option<u32>, shards: Option<Shards>, result: Option<DocumentResult>, } impl<TIndex, TType, TId> OkItem<TIndex, TType, TId> { /** The bulk action for this item. */ pub fn action(&self) -> Action { self.action } /** The document version after this item. */ pub fn version(&self) -> Option<u32> { self.version.clone() } /** Whether or not this item created the document. `created` will only be `true` if the action is `Index` and the document didn't already exist. */ pub fn created(&self) -> bool { match self.result { Some(DocumentResult::Created) => true, _ => false, } } /** Whether or not this item deleted the document. `deleted` will only be `true` if the action is `Delete` and the document existed */ pub fn deleted(&self) -> bool { match self.result { Some(DocumentResult::Deleted) => true, _ => false, } } /** The index for this item. */ pub fn index(&self) -> &TIndex { &self.index } /** The document type for this item. */ pub fn ty(&self) -> &TType { &self.ty } /** The document id for this item. */ pub fn id(&self) -> &TId { &self.id } } /** A failed bulk response item. */ #[derive(Debug, Clone)] pub struct ErrorItem< TIndex = DefaultAllocatedField, TType = DefaultAllocatedField, TId = DefaultAllocatedField, > { action: Action, index: TIndex, ty: TType, id: TId, err: BulkError, } impl<TIndex, TType, TId> ErrorItem<TIndex, TType, TId> { /** The bulk action for this item. */ pub fn action(&self) -> Action { self.action } /** The index for this item. */ pub fn index(&self) -> &TIndex { &self.index } /** The document type for this item. */ pub fn ty(&self) -> &TType { &self.ty } /** The document id for this item. */ pub fn id(&self) -> &TId { &self.id } } impl<TIndex, TType, TId> fmt::Display for ErrorItem<TIndex, TType, TId> where TIndex: fmt::Display + fmt::Debug, TType: fmt::Display + fmt::Debug, TId: fmt::Display + fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "bulk item failed. Details: index: {}, type: {}, id: {}, inner error: {}", self.index, self.ty, self.id, self.err ) } } impl<TIndex, TType, TId> Error for ErrorItem<TIndex, TType, TId> where TIndex: fmt::Display + fmt::Debug, TType: fmt::Display + fmt::Debug, TId: fmt::Display + fmt::Debug, { fn description(&self) -> &str { "bulk item failed" } fn cause(&self) -> Option<&Error> { None } } /** The bulk action being performed. */ #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] pub enum Action { #[serde(rename = "index")] Index, #[serde(rename = "create")] Create, #[serde(rename = "update")] Update, #[serde(rename = "delete")] Delete, } impl<TIndex, TType, TId> IsOkOnSuccess for BulkResponse<TIndex, TType, TId> {} impl<TIndex, TType, TId> IsOkOnSuccess for BulkErrorsResponse<TIndex, TType, TId> {} // Deserialisation struct ItemDe<TIndex, TType, TId> { action: Action, inner: ItemDeInner<TIndex, TType, TId>, } #[derive(Deserialize, Debug, Clone)] struct ItemDeInner<TIndex, TType, TId> { #[serde(rename = "_index")] index: TIndex, #[serde(rename = "_type")] ty: TType, #[serde(rename = "_id")] id: TId, #[serde(rename = "_version")] version: Option<u32>, #[serde(rename = "_shards")] shards: Option<Shards>, result: Option<DocumentResult>, status: u16, error: Option<BulkError>, } impl<'de, TIndex, TType, TId> ItemDe<TIndex, TType, TId> where TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { fn into_err(self) -> Option<ErrorItem<TIndex, TType, TId>> { match self.inner.error { Some(err) => Some(ErrorItem { action: self.action, index: self.inner.index, ty: self.inner.ty, id: self.inner.id, err: err, }), None => None, } } fn into_result(self) -> ItemResult<TIndex, TType, TId> { if self.inner.error.is_some() { Err(self.into_err().expect("expected an error")) } else { Ok(OkItem { action: self.action, index: self.inner.index, ty: self.inner.ty, id: self.inner.id, version: self.inner.version, shards: self.inner.shards, result: self.inner.result, }) } } } impl<'de, TIndex, TType, TId> Deserialize<'de> for ItemDe<TIndex, TType, TId> where TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { fn deserialize<D>(deserializer: D) -> Result<ItemDe<TIndex, TType, TId>, D::Error> where D: Deserializer<'de>, { struct ItemDeVisitor<TIndex, TType, TId> { _marker: PhantomData<(TIndex, TType, TId)>, } impl<'de, TIndex, TType, TId> Visitor<'de> for ItemDeVisitor<TIndex, TType, TId> where TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { type Value = ItemDe<TIndex, TType, TId>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a bulk item") } fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: MapAccess<'de>, { let (action, inner) = visitor .next_entry()? .ok_or(V::Error::custom("expected at least one field"))?; let result = ItemDe { action: action, inner: inner, }; Ok(result) } } deserializer.deserialize_any(ItemDeVisitor { _marker: PhantomData, }) } } fn deserialize_bulk_items<'de, D, TIndex, TType, TId>( deserializer: D, ) -> Result<Vec<ItemResult<TIndex, TType, TId>>, D::Error> where D: Deserializer<'de>, TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { struct OkItemsVisitor<TIndex, TType, TId> { _marker: PhantomData<(TIndex, TType, TId)>, } impl<'de, TIndex, TType, TId> Visitor<'de> for OkItemsVisitor<TIndex, TType, TId> where TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { type Value = Vec<ItemResult<TIndex, TType, TId>>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } #[inline] fn visit_unit<E>(self) -> Result<Vec<ItemResult<TIndex, TType, TId>>, E> where E: DeError, { Ok(vec![]) } #[inline] fn visit_seq<V>( self, mut visitor: V, ) -> Result<Vec<ItemResult<TIndex, TType, TId>>, V::Error> where V: SeqAccess<'de>, { let mut values = Vec::with_capacity(cmp::min(visitor.size_hint().unwrap_or(0), 4096)); while let Some(value) = visitor.next_element::<ItemDe<_, _, _>>()? { values.push(value.into_result()); } Ok(values) } } deserializer.deserialize_any(OkItemsVisitor { _marker: PhantomData, }) } fn deserialize_bulk_item_errors<'de, D, TIndex, TType, TId>( deserializer: D, ) -> Result<Vec<ErrorItem<TIndex, TType, TId>>, D::Error> where D: Deserializer<'de>, TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { struct BulkErrorItemsVisitor<TIndex, TType, TId> { _marker: PhantomData<(TIndex, TType, TId)>, } impl<'de, TIndex, TType, TId> Visitor<'de> for BulkErrorItemsVisitor<TIndex, TType, TId> where TIndex: Deserialize<'de>, TType: Deserialize<'de>, TId: Deserialize<'de>, { type Value = Vec<ErrorItem<TIndex, TType, TId>>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } #[inline] fn visit_unit<E>(self) -> Result<Vec<ErrorItem<TIndex, TType, TId>>, E> where E: DeError, { Ok(vec![]) } #[inline] fn visit_seq<V>( self, mut visitor: V, ) -> Result<Vec<ErrorItem<TIndex, TType, TId>>, V::Error> where V: SeqAccess<'de>, { let mut values = Vec::with_capacity(cmp::min(visitor.size_hint().unwrap_or(0), 4096)); while let Some(value) = visitor.next_element::<ItemDe<_, _, _>>()? { if let Some(value) = value.into_err() { values.push(value); } } Ok(values) } } deserializer.deserialize_any(BulkErrorItemsVisitor { _marker: PhantomData, }) }