use std::marker::PhantomData;
use serde_json;
use serde::Serialize;
use error::*;
use client::{into_response, Client};
use client::requests::{Index, Type, IndicesPutMappingRequest, RequestBuilder};
use client::responses::CommandResponse;
use types::document::{FieldType, DocumentType, IndexDocumentMapping};
pub struct PutMappingRequestBuilder<TDocument> {
index: Index<'static>,
ty: Type<'static>,
_marker: PhantomData<TDocument>,
}
impl Client {
pub fn put_mapping<'a, TDocument>
(&'a self,
index: Index<'static>)
-> RequestBuilder<'a, PutMappingRequestBuilder<TDocument>, TDocument>
where TDocument: Serialize + DocumentType
{
let ty = TDocument::name().into();
RequestBuilder::new(&self,
None,
PutMappingRequestBuilder {
index: index,
ty: ty,
_marker: PhantomData,
})
}
}
impl<TDocument> PutMappingRequestBuilder<TDocument>
where TDocument: DocumentType
{
fn into_request(self) -> Result<IndicesPutMappingRequest<'static, Vec<u8>>> {
let body = serde_json::to_vec(&IndexDocumentMapping::from(TDocument::mapping()))?;
Ok(IndicesPutMappingRequest::for_index_ty(self.index, self.ty, body))
}
}
impl<'a, TDocument> RequestBuilder<'a, PutMappingRequestBuilder<TDocument>, TDocument>
where TDocument: DocumentType
{
pub fn ty<I>(mut self, ty: I) -> Self
where I: Into<Type<'static>>
{
self.req.ty = ty.into();
self
}
pub fn send(self) -> Result<CommandResponse> {
let req = self.req.into_request()?;
RequestBuilder::new(self.client, self.params, req)
.send_raw()
.and_then(into_response)
}
}
#[cfg(test)]
mod tests {
use serde_json::Value;
use prelude::*;
#[test]
fn default_request() {
let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();
let req = client
.put_mapping::<Value>(index("test-idx"))
.req
.into_request()
.unwrap();
assert_eq!("/test-idx/_mappings/value", req.url.as_ref());
assert_eq!(r#"{"properties":{}}"#.as_bytes().to_vec(), req.body);
}
#[test]
fn specify_ty() {
let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();
let req = client
.put_mapping::<Value>(index("test-idx"))
.ty("new-ty")
.req
.into_request()
.unwrap();
assert_eq!("/test-idx/_mappings/new-ty", req.url.as_ref());
}
}