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
use rustc_serialize::Decodable;
use rustc_serialize::json::Json;
use ::Client;
use ::error::EsError;
use ::util::StrJoin;
use super::common::{Options, OptionVal};
use super::decode_json;
pub enum Preference {
Primary,
Local
}
impl From<Preference> for OptionVal {
fn from(from: Preference) -> OptionVal {
OptionVal(match from {
Preference::Primary => "_primary",
Preference::Local => "_local"
}.to_owned())
}
}
pub struct GetOperation<'a, 'b> {
client: &'a mut Client,
index: &'b str,
doc_type: Option<&'b str>,
id: &'b str,
options: Options<'b>
}
impl<'a, 'b> GetOperation<'a, 'b> {
pub fn new(client: &'a mut Client,
index: &'b str,
id: &'b str) -> GetOperation<'a, 'b> {
GetOperation {
client: client,
index: index,
doc_type: None,
id: id,
options: Options::new()
}
}
pub fn with_all_types(&'b mut self) -> &'b mut Self {
self.doc_type = Some("_all");
self
}
pub fn with_doc_type(&'b mut self, doc_type: &'b str) -> &'b mut Self {
self.doc_type = Some(doc_type);
self
}
pub fn with_fields(&'b mut self, fields: &[&'b str]) -> &'b mut Self {
self.options.push("fields", fields.iter().join(","));
self
}
add_option!(with_realtime, "realtime");
add_option!(with_source, "_source");
add_option!(with_routing, "routing");
add_option!(with_preference, "preference");
add_option!(with_refresh, "refresh");
add_option!(with_version, "version");
add_option!(with_version_type, "version_type");
pub fn send(&'b mut self) -> Result<GetResult, EsError> {
let url = format!("/{}/{}/{}{}",
self.index,
self.doc_type.expect("No doc_type specified"),
self.id,
self.options);
let (_, result) = try!(self.client.get_op(&url));
Ok(GetResult::from(&result.expect("No Json payload")))
}
}
#[derive(Debug)]
pub struct GetResult {
pub index: String,
pub doc_type: String,
pub id: String,
pub version: Option<u64>,
pub found: bool,
pub source: Option<Json>
}
impl GetResult {
pub fn source<T: Decodable>(self) -> Result<T, EsError> {
match self.source {
Some(doc) => decode_json(doc),
None => Err(EsError::EsError("No source".to_owned()))
}
}
}
impl<'a> From<&'a Json> for GetResult {
fn from(r: &'a Json) -> GetResult {
info!("GetResult FROM: {:?}", r);
GetResult {
index: get_json_string!(r, "_index"),
doc_type: get_json_string!(r, "_type"),
id: get_json_string!(r, "_id"),
version: r.search("_version").map(|v| {
v.as_u64().expect("Field '_search' not an integer")
}),
found: get_json_bool!(r, "found"),
source: r.search("_source").map(|source| source.clone())
}
}
}