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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*! Mapping for the Elasticsearch `geo_point` type. */

use super::{
    DefaultGeoPointFormat,
    GeoPointFormat,
};
use geo::mapping::Distance;
use std::marker::PhantomData;

/** A field that will be mapped as a `geo_point`. */
pub trait GeoPointFieldType<M> {}

/**
The base requirements for mapping a `geo_point` type.

# Examples

Define a custom `GeoPointMapping`:

```
# #[macro_use]
# extern crate elastic_types;
# extern crate serde;
# use std::marker::PhantomData;
# use elastic_types::prelude::*;
#[derive(Default)]
struct MyGeoPointMapping;
impl GeoPointMapping for MyGeoPointMapping {
    type Format = GeoPointArray;

    //Overload the mapping functions here
    fn geohash() -> Option<bool> {
        Some(true)
    }
}
# fn main() {}
```

This will produce the following mapping:

```
# #[macro_use]
# extern crate elastic_types_derive;
# #[macro_use]
# extern crate json_str;
# #[macro_use]
# extern crate elastic_types;
# extern crate serde;
# extern crate serde_json;
# use std::marker::PhantomData;
# use elastic_types::prelude::*;
# #[derive(Default)]
# struct MyGeoPointMapping;
# impl GeoPointMapping for MyGeoPointMapping {
#     type Format = GeoPointArray;
#     fn geohash() -> Option<bool> {
#         Some(true)
#     }
# }
# fn main() {
# let mapping = elastic_types::derive::standalone_field_ser(MyGeoPointMapping).unwrap();
# let json = json_str!(
{
    "type": "geo_point",
    "geohash": true
}
# );
# assert_eq!(json, mapping);
# }
```

## Map with a generic Format

You can use a generic input parameter to make your `GeoPointMapping` work for any kind of
`GeoPointFormat`:

```
# #[macro_use]
# extern crate elastic_types;
# extern crate serde;
# use std::marker::PhantomData;
# use elastic_types::prelude::*;
#[derive(Default)]
struct MyGeoPointMapping<F> {
    _marker: PhantomData<F>
}

impl <F: GeoPointFormat> GeoPointMapping for MyGeoPointMapping<F> {
    type Format = F;
}
# fn main() {}
```
*/
pub trait GeoPointMapping {
    /**
    The format used to serialise and deserialise the geo point.

    The format isn't actually a part of the Elasticsearch mapping for a `geo_point`,
    but is included on the mapping to keep things consistent.
    */
    type Format: GeoPointFormat;

    /**
    Should the `geo-point` also be indexed as a geohash in the `.geohash` sub-field? Defaults to `false`,
    unless `geohash_prefix` is `true`.
    */
    fn geohash() -> Option<bool> {
        None
    }

    /** The maximum length of the geohash to use for the geohash and `geohash_prefix` options. */
    fn geohash_precision() -> Option<Distance> {
        None
    }

    /** Should the `geo-point` also be indexed as a geohash plus all its prefixes? Defaults to `false`. */
    fn geohash_prefix() -> Option<bool> {
        None
    }

    /**
    If `true`, malformed `geo-points` are ignored.
    If `false` (default), malformed `geo-points` throw an exception and reject the whole document.
    */
    fn ignore_malformed() -> Option<bool> {
        None
    }

    /**
    Should the `geo-point` also be indexed as `.lat` and `.lon` sub-fields?
    Accepts `true` and `false` (default).
    */
    fn lat_lon() -> Option<bool> {
        None
    }
}

/** Default mapping for `geo_point`. */
#[derive(PartialEq, Debug, Default, Clone, Copy)]
pub struct DefaultGeoPointMapping<TFormat = DefaultGeoPointFormat>
where
    TFormat: GeoPointFormat,
{
    _f: PhantomData<TFormat>,
}

impl<TFormat> GeoPointMapping for DefaultGeoPointMapping<TFormat>
where
    TFormat: GeoPointFormat,
{
    type Format = TFormat;
}

mod private {
    use super::{
        GeoPointFieldType,
        GeoPointMapping,
    };
    use private::field::{
        FieldMapping,
        FieldType,
        SerializeFieldMapping,
        StaticSerialize,
    };
    use serde::ser::SerializeStruct;
    use serde::{
        Serialize,
        Serializer,
    };

    #[derive(Default)]
    pub struct GeoPointPivot;

    impl<TField, TMapping> FieldType<TMapping, GeoPointPivot> for TField
    where
        TField: GeoPointFieldType<TMapping> + Serialize,
        TMapping: GeoPointMapping,
    {
    }

    impl<TMapping> FieldMapping<GeoPointPivot> for TMapping
    where
        TMapping: GeoPointMapping,
    {
        type SerializeFieldMapping = SerializeFieldMapping<TMapping, GeoPointPivot>;

        fn data_type() -> &'static str {
            "geo_point"
        }
    }

    impl<TMapping> StaticSerialize for SerializeFieldMapping<TMapping, GeoPointPivot>
    where
        TMapping: FieldMapping<GeoPointPivot> + GeoPointMapping,
    {
        fn static_serialize<S>(serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let mut state = try!(serializer.serialize_struct("mapping", 6));

            try!(state.serialize_field("type", TMapping::data_type()));

            ser_field!(state, "geohash", TMapping::geohash());
            ser_field!(state, "geohash_precision", TMapping::geohash_precision());
            ser_field!(state, "geohash_prefix", TMapping::geohash_prefix());
            ser_field!(state, "ignore_malformed", TMapping::ignore_malformed());
            ser_field!(state, "lat_lon", TMapping::lat_lon());

            state.end()
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json;

    use prelude::*;
    use private::field;

    #[derive(Default, Clone)]
    pub struct MyGeoPointMapping;
    impl GeoPointMapping for MyGeoPointMapping {
        type Format = GeoPointArray;

        fn geohash() -> Option<bool> {
            Some(false)
        }

        fn geohash_precision() -> Option<Distance> {
            Some(Distance(50.0, DistanceUnit::Meters))
        }

        fn geohash_prefix() -> Option<bool> {
            Some(true)
        }

        fn ignore_malformed() -> Option<bool> {
            Some(true)
        }

        fn lat_lon() -> Option<bool> {
            Some(true)
        }
    }

    #[test]
    fn serialise_mapping_default() {
        let ser = serde_json::to_string(&field::serialize(DefaultGeoPointMapping::<
            DefaultGeoPointFormat,
        >::default()))
        .unwrap();

        let expected = json_str!({
            "type": "geo_point"
        });

        assert_eq!(expected, ser);
    }

    #[test]
    fn serialise_mapping_custom() {
        let ser = serde_json::to_string(&field::serialize(MyGeoPointMapping)).unwrap();

        let expected = json_str!({
            "type": "geo_point",
            "geohash": false,
            "geohash_precision": "50m",
            "geohash_prefix": true,
            "ignore_malformed": true,
            "lat_lon": true
        });

        assert_eq!(expected, ser);
    }

}