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
use hyper::status::StatusCode;
use rustc_serialize::Decodable;
use rustc_serialize::json::{Decoder, Json};
use Client;
use error::EsError;
use util::StrJoin;
#[macro_use]
pub mod common;
pub mod bulk;
pub mod delete;
pub mod get;
pub mod index;
pub mod search;
pub mod analyze;
fn format_multi(parts: &[&str]) -> String {
if parts.is_empty() {
return "_all".to_owned()
} else {
parts.iter().join(",")
}
}
fn format_indexes_and_types(indexes: &[&str], types: &[&str]) -> String {
if types.len() == 0 {
format!("{}", format_multi(indexes))
} else {
format!("{}/{}", format_multi(indexes), format_multi(types))
}
}
pub struct RefreshOperation<'a, 'b> {
client: &'a mut Client,
indexes: &'b [&'b str]
}
impl<'a, 'b> RefreshOperation<'a, 'b> {
pub fn new(client: &'a mut Client) -> RefreshOperation {
RefreshOperation {
client: client,
indexes: &[]
}
}
pub fn with_indexes(&'b mut self, indexes: &'b [&'b str]) -> &'b mut Self {
self.indexes = indexes;
self
}
pub fn send(&mut self) -> Result<RefreshResult, EsError> {
let url = format!("/{}/_refresh",
format_multi(&self.indexes));
let (status_code, result) = try!(self.client.post_op(&url));
match status_code {
StatusCode::Ok => Ok(RefreshResult::from(&result.expect("No Json payload"))),
_ => Err(EsError::EsError(format!("Unexpected status: {}", status_code)))
}
}
}
fn decode_json<T: Decodable>(doc: Json) -> Result<T, EsError> {
Ok(try!(Decodable::decode(&mut Decoder::new(doc))))
}
#[derive(Debug, RustcDecodable)]
pub struct ShardCountResult {
pub total: u64,
pub successful: u64,
pub failed: u64
}
pub struct RefreshResult {
pub shards: ShardCountResult
}
impl<'a> From<&'a Json> for RefreshResult {
fn from(r: &'a Json) -> RefreshResult {
RefreshResult {
shards: decode_json(
r.find("_shards")
.expect("No field '_shards'")
.clone())
.unwrap()
}
}
}