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
/*!
Implementation of the Elasticsearch `keyword` and `text` types.

Keyword fields are stored as a raw string of tokens, and aren't analysed when querying.
They're useful for data that only has meaning when considered as a whole, like an id
or single word.

Text fields are stored as a sequence of tokens, constructed based on the given `analyzer`.
They're useful for blobs of content that can be sliced in various ways, like prose.

As far as serialisation is concerned, `keyword` and `text` are equivalent.

# Examples

For defining your own string mapping, see:

- [keyword mapping details](keyword/mapping/trait.KeywordMapping.html#derive-mapping)
- [text mapping details](text/mapping/trait.TextMapping.html#derive-mapping).

Map with a default `string` (follows the semantics for legacy `string` mapping):

```
struct MyType {
    pub field: String
}
```

Map a `keyword`:

```
# extern crate serde;
# extern crate elastic_types;
# fn main() {
# use elastic_types::prelude::*;
# use elastic_types::string::prelude::*;
struct MyType {
    pub field: Keyword<DefaultKeywordMapping>
}
# }
```

Map `text`:

```
# extern crate serde;
# extern crate elastic_types;
# fn main() {
# use elastic_types::prelude::*;
# use elastic_types::string::prelude::*;
struct MyType {
    pub field: Text<DefaultTextMapping>
}
# }
```

Map a custom type as a `keyword` field.
This is especially useful for simple `enum`s:

```
# extern crate serde;
# #[macro_use]
# extern crate elastic_types;
# #[macro_use]
# extern crate serde_derive;
# fn main() {
# use elastic_types::prelude::*;
#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
enum MyKeywordField {
    VariantA,
    VariantB,
    VariantC,
}

impl KeywordFieldType<DefaultKeywordMapping> for MyKeywordField {}
# }
```

# Links

- [Elasticsearch Doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/string.html)
*/

#[macro_use]
mod macros;

pub mod keyword;
pub mod text;

pub mod mapping;

pub use self::keyword::Keyword;
pub use self::text::Text;

pub mod prelude {
    /*!
    Includes all types for the `string` types.
    
    This is a convenience module to make it easy to build mappings for multiple types without too many `use` statements.
    */

    pub use super::keyword::prelude::*;
    pub use super::text::prelude::*;
    pub use super::mapping::*;
}

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

    use prelude::*;

    #[derive(Default)]
    struct MyKeywordMapping;
    impl KeywordMapping for MyKeywordMapping {}

    #[derive(Default)]
    struct MyTextMapping;
    impl TextMapping for MyTextMapping {}

    #[test]
    fn can_change_keyword_mapping() {
        fn takes_custom_mapping(_: Keyword<MyKeywordMapping>) -> bool {
            true
        }

        let string: Keyword<DefaultKeywordMapping> = Keyword::new("stuff");

        assert!(takes_custom_mapping(Keyword::remap(string)));
    }

    #[test]
    fn serialise_elastic_keyword() {
        let string: Keyword<DefaultKeywordMapping> = Keyword::new("my string");

        let ser = serde_json::to_string(&string).unwrap();

        assert_eq!(r#""my string""#, ser);
    }

    #[test]
    fn deserialise_elastic_keyword() {
        let string: Keyword<DefaultKeywordMapping> = serde_json::from_str(r#""my string""#).unwrap();

        assert_eq!("my string", string);
    }

    #[test]
    fn can_change_text_mapping() {
        fn takes_custom_mapping(_: Text<MyTextMapping>) -> bool {
            true
        }

        let string: Text<DefaultTextMapping> = Text::new("stuff");

        assert!(takes_custom_mapping(Text::remap(string)));
    }

    #[test]
    fn serialise_elastic_text() {
        let string: Text<DefaultTextMapping> = Text::new("my string");

        let ser = serde_json::to_string(&string).unwrap();

        assert_eq!(r#""my string""#, ser);
    }

    #[test]
    fn deserialise_elastic_text() {
        let string: Text<DefaultTextMapping> = serde_json::from_str(r#""my string""#).unwrap();

        assert_eq!("my string", string);
    }
}