Expand description
Mapstic allows explicit Elasticsearch mappings to be derived from Rust type definitions.
ToMapping. In most normal cases where the types derive
Serialize or implement Serialize in an idiomatic way, the mapping and
data should match, but special care should be taken when using explicit mapping types with the
#[mapstic(mapping_type = ...)] attribute.
§Deriving mappings
Mapstic can derive many common Rust types automatically:
use mapstic::ToMapping;
#[derive(ToMapping)]
struct MyIndex {
id: u64,
score: f64,
time: std::time::SystemTime,
tags: Vec<Tag>,
}
#[derive(ToMapping)]
struct Tag(String);
This will generate a mapping that looks like this when serialised to JSON:
{
"properties": {
"id": {
"type": "unsigned_long"
},
"score": {
"type": "double"
},
"time": {
"type": "date"
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}It is also possible to control the type and parameters derived for each type and/or field: see
the documentation for the ToMapping derive macro for more detail.
§Implicitly derived types
The following Elasticsearch types are derived by default:
| Elasticsearch type | Rust types | Notes |
|---|---|---|
binary | [u8], &CStr, CString | |
boolean | bool | |
long | i64, NonZeroI64, AtomicI64 | |
integer | i32, NonZeroI32, AtomicI32 | |
short | i16, NonZeroI16, AtomicI16 | |
byte | i8, NonZeroI8, AtomicI8 | |
unsigned_long | u64, u32, u16, u8 | (and their atomic and non-zero variants) |
double | f64 | |
float | f32 | |
half_float | f16 | (only on nightly, and with the nightly feature enabled) |
date | SystemTime | |
date | chrono::DateTime, chrono::Date, chrono::NaiveDateTime, chrono::NaiveDate | (with the chrono feature enabled) |
ip | IpAddr, Ipv4Addr, Ipv6Addr | |
text | &str, String, Cow<str> | (the derived mapping will match the default behaviour when Elasticsearch uses dynamic field mapping: specifically, by also specifying a .keyword sub-field with type keyword) |
§Options and collections and containers, oh my
A number of container types will be automatically derived if their inner types (indicated as
T below) implement ToMapping:
Option<T>BTreeSet<T>Box<T>Cell<T>Cow<T>(whereTalso implementsClone)HashSet<T>LinkedList<T>Mutex<T>RefCell<T>Reverse<T>RwLock<T>Vec<T>VecDeque<T>
If the rc feature is enabled, the following types will also be automatically derived:
§Usage with elasticsearch
The Mapping type can be used directly as a body with the elasticsearch
crate. See the Mapping documentation for
an example.
§Optional features
chrono: enables support for thechronocratenightly: enables support for additionalstdtypes in nightly Rustrc: enables support for a handful of ref-counted types, much like Serde’srcfeature
Enums§
- Mapping
- An Elasticsearch field mapping.
Traits§
- ToMapping
- A type that can be described as an explicit Elasticsearch mapping.