[−][src]Module elastic_types::date
Implementation of the Elasticsearch date
type.
Dates in Elasticsearch are exposed as a formatted string
which can contain a date
and/or a time
component.
All dates used by elastic_types
are expected to be given in Utc
, and if no time is supplied, then 12:00am will be used instead.
Where performance is paramount, the EpochMillis
date format will parse and format dates the fastest.
Date types
Date<M>
The Date<M>
type and chrono
s DateTime<Utc>
are the main date
field types you add to document types.
If the mapping and format aren't important, use DateTime<Utc>
.
If you need to specify mapping properties like boost
, or use a specific format like epoch_millis
, use Date<M>
.
DateValue
and FormattableDateValue<F>
The DateValue
and FormattableDateValue<F>
types are used in methods to represent dates that either don't have a format or have a specific format respectively.
Date
and DateTime<Utc>
can freely convert to and from these types, so you probably won't need to interact with them directly.
Examples
For defining your own date mapping, see mapping details.
Map with a default date
:
use chrono::{DateTime, Utc}; struct MyType { pub field: DateTime<Utc> }
For custom formats, the most ergonomic approach is to declare a type alias using the mapping and format:
type Timestamp = Date<DefaultDateMapping<EpochMillis>>; struct MyType { pub field: Timestamp }
Map with a custom date
mapping:
struct MyType { pub field: Date<MyDateMapping> }
Creating Formats
To make it easier to build your own date formats, derive ElasticDateFormat
on a unit struct.
This will convert an Elasticsearch format string into a Vec<chrono::format::Item>
for efficient parsing and formatting at runtime:
#[derive(Default, ElasticDateFormat)] #[elastic(date_format="yyyy-MM-dd'T'HH:mm:ss")] struct MyFormat;
You can also manually implement DateFormat
and write your own arbitrary format/parse logic:
use chrono::{DateTime, Utc}; #[derive(Default, Clone)] struct Rfc3339Format; impl DateFormat for Rfc3339Format { fn name() -> &'static str { "yyyy-MM-dd'T'HH:mm:ssZ" } fn format<'a>(date: &'a DateValue) -> FormattedDate<'a> { date.to_rfc3339().into() } fn parse(date: &str) -> Result<DateValue, ParseError> { let date = DateTime::parse_from_rfc3339(date)?; Ok(DateTime::from_utc(date.naive_local(), Utc).into()) } }
Links
Modules
mapping | Mapping for the Elasticsearch |
prelude | Includes all types for the |
Structs
BasicDateTime | Format for |
BasicDateTimeNoMillis | Format for |
ChronoFormat | Format for default |
Date | An Elasticsearch |
DateExpr | A date math expression. |
DateValue | A date value produced and consumed by date formats. |
EpochMillis | Format for |
FormattableDateValue | A date value paired with a format. |
FormattedDate | A formatted date. |
ParseError | Represents an error encountered during parsing. |
Traits
DateFormat | A format used for parsing and formatting dates. |
Datelike | The common set of methods for date component. |
Timelike | The common set of methods for time component. |
Type Definitions
ChronoDateTime | A re-export of the |
DefaultDateFormat | The default |