Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
[refactor] cleaner bulk results
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed Dec 14, 2018
1 parent 5f2b6dc commit 0b21b4f
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/elasticsearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,39 @@ pub struct Bucket {
#[derive(Clone, Debug, Deserialize)]
pub struct BulkActionResponse {
#[serde(rename = "_id")]
id: String,
status: u16,
result: Option<String>,
pub id: String,
pub status: u16,
pub result: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct BulkResponseItem {
index: Option<BulkActionResponse>,
update: Option<BulkActionResponse>,
delete: Option<BulkActionResponse>,
pub index: Option<BulkActionResponse>,
pub update: Option<BulkActionResponse>,
pub delete: Option<BulkActionResponse>,
}

impl BulkResponseItem {
pub fn action_response(&self) -> Option<&BulkActionResponse> {
self
.index
.as_ref()
.or_else(|| self.update.as_ref())
.or_else(|| self.delete.as_ref())
}

pub fn is_success(&self) -> bool {
self
.action_response()
.map(|response| response.status < 300)
.unwrap_or(false)
}
}

#[derive(Clone, Debug, Deserialize)]
pub struct BulkResponse {
errors: bool,
items: Vec<BulkResponseItem>,
pub errors: bool,
pub items: Vec<BulkResponseItem>,
}

#[derive(Clone, Debug, Default)]
Expand All @@ -279,11 +296,9 @@ impl BulkResult {
let mut result: BulkResult = Default::default();

for item in response.items {
match item.index.or(item.update).or(item.delete) {
Some(ref action_response) if action_response.status < 300 => {
result.successes.push(action_response.id.to_owned())
}
Some(action_response) => result.failures.push(action_response.id),
match item.action_response() {
Some(action_response) if action_response.status < 300 => result.successes.push(action_response.id.to_owned()),
Some(action_response) => result.failures.push(action_response.id.to_owned()),
None => (),
}
}
Expand Down

0 comments on commit 0b21b4f

Please sign in to comment.