rs_es/operations/
delete.rs1use reqwest::StatusCode;
20
21use serde::Deserialize;
22
23use crate::{error::EsError, Client, EsResponse};
24
25use super::common::{OptionVal, Options};
26
27#[derive(Debug)]
28pub struct DeleteOperation<'a, 'b> {
29 client: &'a mut Client,
31
32 index: &'b str,
34
35 doc_type: &'b str,
37
38 id: &'b str,
40
41 options: Options<'b>,
43}
44
45impl<'a, 'b> DeleteOperation<'a, 'b> {
46 pub fn new(
47 client: &'a mut Client,
48 index: &'b str,
49 doc_type: &'b str,
50 id: &'b str,
51 ) -> DeleteOperation<'a, 'b> {
52 DeleteOperation {
53 client,
54 index,
55 doc_type,
56 id,
57 options: Options::default(),
58 }
59 }
60
61 add_option!(with_version, "version");
62 add_option!(with_version_type, "version_type");
63 add_option!(with_routing, "routing");
64 add_option!(with_parent, "parent");
65 add_option!(with_consistency, "consistency");
66 add_option!(with_refresh, "refresh");
67 add_option!(with_timeout, "timeout");
68
69 pub fn send(&'a mut self) -> Result<DeleteResult, EsError> {
70 let url = format!(
71 "/{}/{}/{}{}",
72 self.index, self.doc_type, self.id, self.options
73 );
74 let response = self.client.delete_op(&url)?;
75 match response.status_code() {
76 StatusCode::OK => Ok(response.read_response()?),
77 status_code => Err(EsError::EsError(format!(
78 "Unexpected status: {}",
79 status_code
80 ))),
81 }
82 }
83}
84
85impl Client {
86 pub fn delete<'a>(
90 &'a mut self,
91 index: &'a str,
92 doc_type: &'a str,
93 id: &'a str,
94 ) -> DeleteOperation {
95 DeleteOperation::new(self, index, doc_type, id)
96 }
97}
98
99#[derive(Debug, Deserialize)]
101pub struct DeleteResult {
102 pub found: bool,
103 #[serde(rename = "_index")]
104 pub index: String,
105 #[serde(rename = "_type")]
106 pub doc_type: String,
107 #[serde(rename = "_id")]
108 pub id: String,
109 #[serde(rename = "_version")]
110 pub version: u64,
111}
112
113#[cfg(test)]
114pub mod tests {
115 use crate::tests::{clean_db, make_client, TestDocument};
116
117 #[test]
118 fn test_delete() {
119 let index_name = "test_delete";
120 let mut client = make_client();
121
122 clean_db(&mut client, index_name);
123 let id = {
124 let doc = TestDocument::new().with_int_field(4);
125 let result = client
126 .index(index_name, "test_type")
127 .with_doc(&doc)
128 .send()
129 .unwrap();
130 result.id
131 };
132
133 let delete_result = client.delete(index_name, "test_type", &id).send().unwrap();
134 assert_eq!(id, delete_result.id);
135 assert_eq!(true, delete_result.found);
136 }
137}