Struct elastic_types::date::prelude::DateExpr []

pub struct DateExpr<F> { /* fields omitted */ }

A date math expression.

Date math expressions start from an anchor date, like the literal now or 2017-05-06 and apply math operations to produce a new date value.

Examples

A date expression for now plus 2 days:

let expr: DateExpr<BasicDateTime> = DateExpr::now().add_days(2);

Which serialises to:

"now+2d"

A date expression using a concrete date value plus 2 days:

let expr: DateExpr<BasicDateTime> = DateExpr::value(DateValue::now()).add_days(2);

Which serialises to:

"20150301T145500.000Z||+2d"

Methods

impl<F> DateExpr<F> where
    F: DateFormat
[src]

Create a new date expression for now.

This value is different from Date::now() because it doesn't calculate the current date from the system clock. It serialises to the literal string "now", which is interpreted by Elasticsearch when indexing.

Create a new date expression from a concrete date value.

If the input is a DateValue, then it'll use any format specified on the DateExpr. If the input is a Date or chrono::DateTime, then the format on its mapping must match the format expected by the DateExpr.

Examples

Create a date expression from a chrono::DateTime:

let date = DateValue::now();

// The format annotation `EpochMillis` is required
let expr: DateExpr<EpochMillis> = DateExpr::value(date);

Create a date expression from a Date:

let date: Date<DefaultDateMapping<EpochMillis>> = Date::now();

// The format `EpochMillis` is inferred
let expr = DateExpr::value(date);

Attempting to create a date expression from a Date with a different format will fail to compile:

let date: Date<DefaultDateMapping<BasicDateTime>> = Date::now();

// Error: expected struct `EpochMillis`, found struct `BasicDateTime`
let expr: DateExpr<EpochMillis> = DateExpr::value(date);

This is to ensure formats aren't silently converted when they shouldn't be, leading to runtime errors in Elasticsearch. Convert the date into a DateValue first:

let date: Date<DefaultDateMapping<BasicDateTime>> = Date::now();

let expr: DateExpr<EpochMillis> = DateExpr::value(DateValue::from(date));

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Add to the anchored date.

Subtract from the anchored date.

Round the anchored date.

Trait Implementations

impl<F: Debug> Debug for DateExpr<F>
[src]

Formats the value using the given formatter.

impl<F: Clone> Clone for DateExpr<F>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<F: PartialEq> PartialEq for DateExpr<F>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<F> Display for DateExpr<F> where
    F: DateFormat
[src]

Formats the value using the given formatter. Read more

impl<F> Serialize for DateExpr<F> where
    F: DateFormat
[src]

Serialize this value into the given Serde serializer. Read more