elastic_types
elastic_types
is a library for statically-defined document mappings for Rust. It provides tools to define, encode and decode document types efficiently, however they're stored in Elasticsearch.
Define your Elasticsearch types as PORS (Plain Old Rust Structures) and generate an equivalent Elasticsearch mapping from them, where correctness is enforced by Rust's type system. It provides rust implementations of the core Elasticsearch datatypes (like date
, geo_point
).
It's especially helpful for the date
and geo_point
types, where serialisation for the various formats is provided for you.
This library makes heavy use of serde
for serialisation. We also try not to reinvent the wheel wherever possible and rely on some common dependencies for types, such as chrono
for dates and rust-geo
for geometry.
This library is the document serialisation provider for the higher-level elastic
client.
Example
Add elastic_types
to your Cargo.toml
:
[dependencies]
elastic_types = "*"
elastic_types_derive = "*"
And reference it in your crate root:
extern crate elastic_types_derive;
extern crate elastic_types;
Defining indexable types
Define a custom Elasticsearch type called mytype
:
type Timestamp = ;
You can then get the mapping for your type as json
:
let mapping = to_string.unwrap;
Which looks like:
Deserialising indexed types
Types that derive ElasticType
are themselves serialisable, which can be very helpful when using types with special formats, like date
. Take the following document:
Using the Date
type for the timestamp
, we can correctly deserialise the document as a strongly typed object:
type Timestamp = ;
let de: MyType = from_str.unwrap;
assert_eq!;
Macros
elastic_types_derive
Provides custom derive plugins for Elasticsearch datatypes and mappings in elastic_types
and date-specific plugins for the date datatype.