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
use ::do_req;
use ::{Client, EsResponse};
use ::error::EsError;
pub struct AnalyzeOperation<'a, 'b> {
client: &'a mut Client,
body: &'b str,
index: Option<&'b str>,
analyzer: Option<&'b str>
}
impl<'a, 'b> AnalyzeOperation<'a, 'b> {
pub fn new(client: &'a mut Client, body: &'b str) -> AnalyzeOperation<'a, 'b> {
AnalyzeOperation {
client: client,
body: body,
index: None,
analyzer: None
}
}
pub fn with_index(&'b mut self, index: &'b str) -> &'b mut Self {
self.index = Some(index);
self
}
pub fn with_analyzer(&'b mut self, analyzer: &'b str) -> &'b mut Self {
self.analyzer = Some(analyzer);
self
}
pub fn send(&'b mut self) -> Result<AnalyzeResult, EsError> {
let mut url = match self.index {
None => "/_analyze".to_owned(),
Some(index) => format!("{}/_analyze", index)
};
match self.analyzer {
None => (),
Some(analyzer) => {
url.push_str(&format!("?analyzer={}", analyzer))
}
}
let client = &self.client;
let full_url = client.full_url(&url);
let req = try!(client.http_client
.post(&full_url)
.body(self.body)
.send());
let response = try!(do_req(req));
Ok(try!(response.read_response()))
}
}
impl Client {
pub fn analyze<'a>(&'a mut self,
body: &'a str) -> AnalyzeOperation {
AnalyzeOperation::new(self, body)
}
}
#[derive(Debug, Deserialize)]
pub struct AnalyzeResult {
pub tokens: Vec<Token>
}
#[derive(Debug, Deserialize)]
pub struct Token {
pub token: String,
#[serde(rename="type")]
pub token_type: String,
pub position: u64,
pub start_offset: u64,
pub end_offset: u64
}