elastic_types/document/
mod.rs

1/*!
2Base requirements for indexable document mappings.
3
4Structures that can be indexed in Elasticsearch should implement `DocumentType`.
5The `DocumentType` is composed of typical mapping metadata, as well as the mapping
6for each of its properties.
7
8Documents can be mapped as indexable types, or as an object field on another type.
9
10# Examples
11
12Define your Elasticsearch types using _Plain Old Rust Structures_.
13
14## Derive Mapping
15
16Mapping can be generated by deriving `ElasticType` on a struct:
17
18```
19# #[macro_use]
20# extern crate json_str;
21# #[macro_use]
22# extern crate serde_derive;
23# #[macro_use]
24# extern crate elastic_types_derive;
25# #[macro_use]
26# extern crate elastic_types;
27# extern crate serde;
28# use elastic_types::prelude::*;
29#[derive(Serialize, ElasticType)]
30pub struct MyType {
31    pub my_date: Date<DefaultDateMapping>,
32    pub my_string: String,
33    pub my_num: i32
34}
35# fn main() {
36# }
37```
38
39This will produce the following field mapping:
40
41```
42# #[macro_use]
43# extern crate json_str;
44# #[macro_use]
45# extern crate serde_derive;
46# #[macro_use]
47# extern crate elastic_types_derive;
48# #[macro_use]
49# extern crate elastic_types;
50# extern crate serde;
51# extern crate serde_json;
52# use elastic_types::prelude::*;
53# #[derive(Serialize, ElasticType)]
54# pub struct MyType {
55#   pub my_date: Date<DefaultDateMapping>,
56#   pub my_string: String,
57#   pub my_num: i32
58# }
59# fn main() {
60# let mapping = elastic_types::derive::standalone_field_ser(MyTypeMapping).unwrap();
61# let json = json_str!(
62{
63    "type": "nested",
64    "properties": {
65        "my_date": {
66            "type": "date",
67            "format": "basic_date_time"
68        },
69        "my_string": {
70            "type": "text",
71            "fields": {
72                "keyword":{
73                    "type":"keyword",
74                    "ignore_above":256
75                }
76            }
77        },
78        "my_num": {
79            "type": "integer"
80        }
81    }
82}
83# );
84# assert_eq!(json, mapping);
85# }
86```
87
88It's also possible to adjust the mapping using the `#[elastic]` attribute.
89
90### Override Default Mapping Properties
91
92You can override the mapping meta properties for an object by providing your own mapping type with `#[elastic(mapping="{TypeName}")]`:
93
94```
95# #[macro_use]
96# extern crate json_str;
97# #[macro_use]
98# extern crate serde_derive;
99# #[macro_use]
100# extern crate elastic_types_derive;
101# #[macro_use]
102# extern crate elastic_types;
103# extern crate serde;
104# use elastic_types::prelude::*;
105#[derive(Serialize, ElasticType)]
106#[elastic(mapping="MyTypeMapping")]
107pub struct MyType {
108    pub my_date: Date<DefaultDateMapping>,
109    pub my_string: String,
110    pub my_num: i32
111}
112
113#[derive(Default)]
114pub struct MyTypeMapping;
115impl DocumentMapping for MyTypeMapping {
116    //Give your own name to a type
117    fn name() -> &'static str { "my_type" }
118
119    fn data_type() -> &'static str { OBJECT_DATATYPE }
120}
121# fn main() {
122# }
123```
124
125This will produce the following field mapping:
126
127```
128# #[macro_use]
129# extern crate json_str;
130# #[macro_use]
131# extern crate serde_derive;
132# #[macro_use]
133# extern crate elastic_types_derive;
134# #[macro_use]
135# extern crate elastic_types;
136# extern crate serde;
137# extern crate serde_json;
138# use elastic_types::prelude::*;
139# #[derive(Default, Serialize, Deserialize, ElasticType)]
140# #[elastic(mapping="MyTypeMapping")]
141# pub struct MyType {
142#   pub my_date: Date<DefaultDateMapping>,
143#   pub my_string: String,
144#   pub my_num: i32
145# }
146#
147# #[derive(Default)]
148# pub struct MyTypeMapping;
149# impl DocumentMapping for MyTypeMapping {
150#   fn name() -> &'static str { "my_type" }
151#   fn data_type() -> &'static str { OBJECT_DATATYPE }
152# }
153# fn main() {
154# let mapping = elastic_types::derive::standalone_field_ser(MyTypeMapping).unwrap();
155# let json = json_str!(
156{
157    "type": "object",
158    "properties": {
159        "my_date": {
160            "type": "date",
161            "format": "basic_date_time"
162        },
163        "my_string": {
164            "type": "text",
165            "fields": {
166                "keyword":{
167                    "type":"keyword",
168                    "ignore_above":256
169                }
170            }
171        },
172        "my_num": {
173            "type": "integer"
174        }
175    }
176}
177# );
178# assert_eq!(json, mapping);
179# }
180```
181
182### Ignore or Rename Fields
183
184You can then serialise type mappings with `#[serde]` attributes:
185
186```
187# #[macro_use]
188# extern crate json_str;
189# #[macro_use]
190# extern crate serde_derive;
191# #[macro_use]
192# extern crate elastic_types_derive;
193# #[macro_use]
194# extern crate elastic_types;
195# extern crate serde;
196# use elastic_types::prelude::*;
197#[derive(ElasticType, Serialize)]
198pub struct MyType {
199    #[serde(rename="my_renamed_date")]
200    pub my_date: Date<DefaultDateMapping>,
201    #[serde(skip_serializing)]
202    pub ignored: String,
203    pub my_num: i32
204}
205# fn main() {
206# }
207```
208
209> NOTE: Fields with a `#[serde(skip_deserializing)]` attribute will still be mapped, because they can
210still be indexed in Elasticsearch.
211
212## Limitations
213
214Automatically deriving mapping has the following limitations:
215
216- Generics aren't supported by auto deriving.
217So you can't `#[derive(ElasticType)]` on `MyType<T>`.
218- Mapping types can't be shared. This is because they need to map the type fields, so are specific to that type.
219So you can't share `MyTypeMapping` between `MyType` and `MyOtherType`.
220
221# Links
222- [Field Types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html)
223- [Document Types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html)
224*/
225
226pub mod mapping;
227
228mod impls;
229pub use self::impls::*;
230
231pub mod prelude {
232    /*!
233    Includes all types for document types.
234    
235    This is a convenience module to make it easy to build mappings for multiple types without too many `use` statements.
236    */
237
238    pub use super::impls::*;
239    pub use super::mapping::*;
240}