use serde::Deserialize;
use crate::{
error::EsError,
{Client, EsResponse},
};
#[derive(Debug)]
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,
body,
index: None,
analyzer: None,
}
}
pub fn with_index(&mut self, index: &'b str) -> &mut Self {
self.index = Some(index);
self
}
pub fn with_analyzer(&mut self, analyzer: &'b str) -> &mut Self {
self.analyzer = Some(analyzer);
self
}
pub fn send(&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 response = self.client.do_es_op(&url, |url| {
self.client.http_client.post(url).body(self.body.to_owned())
})?;
Ok(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,
}