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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use error::*;
use client::Client;
use client::requests::{empty_body, DefaultBody, IntoBody, Index, IndicesCreateRequest,
                       RequestBuilder, RawRequestBuilder};
use client::responses::CommandResponse;

/** 
A builder for a [`Client.create_index`][Client.create_index] request. 

[Client.create_index]: ../struct.Client.html#method.create_index
*/
pub struct CreateIndexRequestBuilder<TBody> {
    index: Index<'static>,
    body: TBody,
}

impl Client {
    /** 
    Create a [`RequestBuilder` for a create index request][RequestBuilder.create_index].

    # Examples
    
    Create an index called `myindex`:
    
    ```no_run
    # use elastic::prelude::*;
    # let client = ClientBuilder::new().build().unwrap();
    let my_index = index("myindex");

    let response = client.create_index(my_index).send().unwrap();

    assert!(response.acknowledged);
    ```

    Create an index with settings and document mappings for a [`DocumentType`][documents-mod] called `MyType`:

    ```no_run
    # extern crate serde;
    # #[macro_use] extern crate serde_derive;
    # #[macro_use] extern crate elastic_derive;
    # #[macro_use] extern crate serde_json;
    # extern crate elastic;
    # use elastic::prelude::*;
    # #[derive(Serialize, Deserialize, ElasticType)]
    # struct MyType { }
    # fn main() {
    # let client = ClientBuilder::new().build().unwrap();
    let my_index = index("myindex");

    let body = json!({
        "settings": {
            "index": {
                "number_of_shards": 3,
                "number_of_replicas": 2
            }
        },
        "mappings": {
            MyType::name(): IndexDocumentMapping::from(MyType::mapping())
        }
    });

    let response = client.create_index(my_index)
                         .body(body.to_string())
                         .send()
                         .unwrap();

    assert!(response.acknowledged);
    # }
    ```

    For more details on document types and mapping, see the [`types`][types-mod] module.

    [RequestBuilder.create_index]: requests/struct.RequestBuilder.html#create-index-builder
    [types-mod]: ../types/index.html
    [documents-mod]: ../types/document/index.html
    */
    pub fn create_index<'a>(&'a self,
                            index: Index<'static>)
                            -> RequestBuilder<'a, CreateIndexRequestBuilder<DefaultBody>> {
        RequestBuilder::new(&self,
                            None,
                            CreateIndexRequestBuilder {
                                index: index,
                                body: empty_body(),
                            })
    }
}

impl<TBody> CreateIndexRequestBuilder<TBody>
    where TBody: IntoBody
{
    fn into_request(self) -> IndicesCreateRequest<'static, TBody> {
        IndicesCreateRequest::for_index(self.index, self.body)
    }
}

/** 
# Create index builder

A request builder for a [Create Index][docs-create-index] request.

Call [`Client.create_index`][Client.create_index] to get a `RequestBuilder` for a create index request.

[Client.create_index]: ../struct.Client.html#method.create_index
[docs-create-index]: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
*/
impl<'a, TBody> RequestBuilder<'a, CreateIndexRequestBuilder<TBody>>
    where TBody: IntoBody
{
    /** 
    Set the body for the search request.
    
    If no body is specified then an empty query will be used.
    */
    pub fn body<TNewBody>(self,
                          body: TNewBody)
                          -> RequestBuilder<'a, CreateIndexRequestBuilder<TNewBody>>
        where TNewBody: IntoBody
    {
        RequestBuilder::new(self.client,
                            self.params,
                            CreateIndexRequestBuilder {
                                index: self.req.index,
                                body: body,
                            })
    }

    /** Send the create index request. */
    pub fn send(self) -> Result<CommandResponse> {
        let req = self.req.into_request();

        RequestBuilder::new(self.client, self.params, RawRequestBuilder::new(req))
            .send_raw()?
            .into_response()
    }
}

#[cfg(test)]
mod tests {
    use prelude::*;

    #[test]
    fn default_request() {
        let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();

        let req = client.create_index(index("testindex")).req.into_request();

        assert_eq!("/testindex", req.url.as_ref());
    }

    #[test]
    fn specify_body() {
        let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();

        let req = client
            .create_index(index("testindex"))
            .body("{}")
            .req
            .into_request();

        assert_eq!("{}", req.body);
    }
}