Crate mapstic

Crate mapstic 

Source
Expand description

Mapstic allows explicit Elasticsearch mappings to be derived from Rust type definitions.

Note that it is the responsibility of the user to ensure that documents generated by the Rust type and indexed by Elasticsearch have the same data types and shape as the mapping generated by deriving 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 typeRust typesNotes
binary[u8], &CStr, CString
booleanbool
longi64, NonZeroI64, AtomicI64
integeri32, NonZeroI32, AtomicI32
shorti16, NonZeroI16, AtomicI16
bytei8, NonZeroI8, AtomicI8
unsigned_longu64, u32, u16, u8(and their atomic and non-zero variants)
doublef64
floatf32
half_floatf16(only on nightly, and with the nightly feature enabled)
dateSystemTime
datechrono::DateTime, chrono::Date, chrono::NaiveDateTime, chrono::NaiveDate(with the chrono feature enabled)
ipIpAddr, 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:

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 the chrono crate
  • nightly: enables support for additional std types in nightly Rust
  • rc: enables support for a handful of ref-counted types, much like Serde’s rc feature

Enums§

Mapping
An Elasticsearch field mapping.

Traits§

ToMapping
A type that can be described as an explicit Elasticsearch mapping.

Derive Macros§

ToMapping
Derives ToMapping for the given type.