[][src]Struct elastic_types::document::prelude::IndexDocumentMapping

pub struct IndexDocumentMapping<TMapping> where
    TMapping: ObjectMapping
{ /* fields omitted */ }

A wrapper type for serialising user types.

Serialising Document will produce the mapping for the given type, suitable as the mapping for Put Mapping or Create Index.

Examples

To serialise a document mapping, you can use its mapping type as a generic parameter in IndexDocumentMapping<M>. For example, we can define an index type for the Create Index API that includes the mapping for MyType:

#[derive(Serialize, ElasticType)]
pub struct MyType {
    pub my_date: Date<DefaultDateMapping>,
    pub my_string: String,
    pub my_num: i32
}

#[derive(Default, Serialize)]
pub struct MyIndex {
    pub mappings: Mappings
}

#[derive(Default, Serialize)]
pub struct Mappings {
    pub mytype: IndexDocumentMapping<MyTypeMapping>
}

Serialising MyIndex will produce the following json:

{
    "mappings": {
        "mytype": {
            "properties": {
                "my_date": {
                    "type": "date",
                    "format": "basic_date_time"
                },
                "my_string": {
                    "type": "text",
                    "fields": {
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "my_num": {
                    "type": "integer"
                }
            }
        }
    }
}

Alternatively, you can implement serialisation manually for MyIndex and avoid having to keep field names up to date if the document type name changes:

#[derive(Serialize, ElasticType)]
#[derive(Default, Serialize)]
pub struct MyIndex {
    mappings: Mappings
}

#[derive(Default)]
struct Mappings;
impl Serialize for Mappings {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("mappings", 1)?;

        state.serialize_field(MyType::static_ty(), &MyType::index_mapping())?;

        state.end()
    }
}

Trait Implementations

impl<TMapping> Default for IndexDocumentMapping<TMapping> where
    TMapping: ObjectMapping
[src]

impl<TMapping> Serialize for IndexDocumentMapping<TMapping> where
    TMapping: ObjectMapping
[src]

Auto Trait Implementations

impl<TMapping> Send for IndexDocumentMapping<TMapping> where
    TMapping: Send

impl<TMapping> Sync for IndexDocumentMapping<TMapping> where
    TMapping: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Same for T

type Output = T

Should always be Self

impl<SS, SP> SupersetOf for SP where
    SS: SubsetOf<SP>,