rs_es/operations/
delete.rs

1/*
2 * Copyright 2015-2019 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Implementation of delete operations, both Delete-By-Query and Delete-By-Id
18
19use 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    /// The HTTP client
30    client: &'a mut Client,
31
32    /// The index
33    index: &'b str,
34
35    /// The type
36    doc_type: &'b str,
37
38    /// The ID
39    id: &'b str,
40
41    /// Optional options
42    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    /// Delete by ID
87    ///
88    /// See: https://www.elastic.co/guide/en/elasticsearch/reference/1.x/docs-delete.html
89    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/// Result of a DELETE operation
100#[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}