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
use super::ShardFailure;
use crate::util::ShouldSkip;

/// Number of shards touched with their states
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShardStatistics {
    /// Total number of touched shards
    pub total: u32,

    /// Total number of successful shards
    pub successful: u32,

    /// Total number of skipped shards
    pub skipped: u32,

    /// Total number of failed shards
    pub failed: u32,

    /// Partial response failures
    #[serde(skip_serializing_if = "ShouldSkip::should_skip", default)]
    pub failures: Vec<ShardFailure>,
}

impl Default for ShardStatistics {
    fn default() -> Self {
        Self {
            total: 1,
            successful: 1,
            skipped: 0,
            failed: 0,
            failures: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserializes_successfully() {
        let value = json!({
          "total": 280,
          "successful": 277,
          "skipped": 0,
          "failed": 3,
          "failures": [
            {
              "shard": 1,
              "index": "nbs_comprehend-2021-w41",
              "node": "oGEHA-aRSnmwuEmqSZc6Kw",
              "reason": {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                  "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
                  "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
                  "doc['user.followers_count'].value > 9999 ? 1 : 0",
                  "                           ^---- HERE"
                ],
                "script": "doc['user.followers_count'].value > 9999 ? 1 : 0",
                "lang": "painless",
                "position": {
                  "offset": 27,
                  "start": 0,
                  "end": 48
                },
                "caused_by": {
                  "type": "illegal_state_exception",
                  "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
                }
              }
            }
          ]
        });

        let _ = serde_json::from_value::<ShardStatistics>(value).unwrap();
    }
}