rs_es/operations/
delete_index.rs

1/*
2 * Copyright 2015-2018 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 ElasticSearch Delete Index operation
18
19use reqwest::StatusCode;
20
21use crate::{error::EsError, Client, EsResponse};
22
23use super::GenericResult;
24
25impl Client {
26    /// Delete given index
27    ///
28    /// TODO: ensure all options are supported, replace with a `DeleteIndexOperation` to
29    /// follow the pattern defined elsewhere.
30    ///
31    /// See: https://www.elastic.co/guide/en/elasticsearch/reference/2.x/indices-delete-index.html
32    pub fn delete_index<'a>(&'a mut self, index: &'a str) -> Result<GenericResult, EsError> {
33        let url = format!("/{}/", index);
34        let response = self.delete_op(&url)?;
35
36        match response.status_code() {
37            StatusCode::OK => Ok(response.read_response()?),
38            status_code => Err(EsError::EsError(format!(
39                "Unexpected status: {}",
40                status_code
41            ))),
42        }
43    }
44}
45
46#[cfg(test)]
47pub mod tests {
48    use crate::tests::{clean_db, make_client, TestDocument};
49
50    #[test]
51    fn test_delete_index() {
52        let index_name = "test_delete_index";
53        let mut client = make_client();
54
55        clean_db(&mut client, index_name);
56        {
57            let result = client
58                .index(index_name, "test_type")
59                .with_doc(&TestDocument::new().with_int_field(1))
60                .send();
61            assert!(result.is_ok());
62        }
63        {
64            let result = client.delete_index(index_name);
65            log::info!("DELETE INDEX RESULT: {:?}", result);
66
67            assert!(result.is_ok());
68
69            let result_wrapped = result.unwrap();
70            assert!(result_wrapped.acknowledged);
71        }
72    }
73}