From b758b76da4db59df10c7a2e2fb62b38faa5dd6ec Mon Sep 17 00:00:00 2001 From: Subhash Gopalakrishnan Date: Thu, 10 Oct 2013 19:58:59 +0530 Subject: [PATCH] Support year units in date math expressions According to http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html, the date math expressions support M (month), w (week), h (hour), m (minute), and s (second) units. Why years are not supported? Please add support for year units. Closes #3828. Closes #3874. --- docs/reference/mapping/date-format.asciidoc | 2 +- .../elasticsearch/common/joda/DateMathParser.java | 13 +++++++++++++ .../common/joda/DateMathParserTests.java | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/reference/mapping/date-format.asciidoc b/docs/reference/mapping/date-format.asciidoc index bb9736b25b9..a02554f1c63 100644 --- a/docs/reference/mapping/date-format.asciidoc +++ b/docs/reference/mapping/date-format.asciidoc @@ -27,7 +27,7 @@ query/filter (mainly make sense in `range` query/filter). The expression starts with an "anchor" date, which can be either `now` or a date string (in the applicable format) ending with `||`. It can then follow by a math expression, supporting `+`, `-` and `/` -(rounding). The units supported are `M` (month), `w` (week), `h` (hour), +(rounding). The units supported are `y` (year), `M` (month), `w` (week), `h` (hour), `m` (minute), and `s` (second). Here are some samples: `now+1h`, `now+1h+1m`, `now+1h/d`, diff --git a/src/main/java/org/elasticsearch/common/joda/DateMathParser.java b/src/main/java/org/elasticsearch/common/joda/DateMathParser.java index 17bf4d2d852..760f69c9de4 100644 --- a/src/main/java/org/elasticsearch/common/joda/DateMathParser.java +++ b/src/main/java/org/elasticsearch/common/joda/DateMathParser.java @@ -91,6 +91,19 @@ public class DateMathParser { } char unit = mathString.charAt(i++); switch (unit) { + case 'y': + if (type == 0) { + if (roundUp) { + dateTime.yearOfCentury().roundCeiling(); + } else { + dateTime.yearOfCentury().roundFloor(); + } + } else if (type == 1) { + dateTime.addYears(num); + } else if (type == 2) { + dateTime.addYears(-num); + } + break; case 'M': if (type == 0) { if (roundUp) { diff --git a/src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java b/src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java index 8c737f3ba60..4b1a81f6407 100644 --- a/src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java +++ b/src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java @@ -27,6 +27,8 @@ public class DateMathParserTests { assertThat(parser.parse("now+1m+1s/m", 0), equalTo(TimeUnit.MINUTES.toMillis(1))); assertThat(parser.parseUpperInclusive("now+1m+1s/m", 0), equalTo(TimeUnit.MINUTES.toMillis(2))); + + assertThat(parser.parse("now+4y", 0), equalTo(TimeUnit.DAYS.toMillis(4*365 + 1))); } @Test @@ -36,5 +38,9 @@ public class DateMathParserTests { assertThat(parser.parse("1970-01-01", 0), equalTo(0l)); assertThat(parser.parse("1970-01-01||+1m", 0), equalTo(TimeUnit.MINUTES.toMillis(1))); assertThat(parser.parse("1970-01-01||+1m+1s", 0), equalTo(TimeUnit.MINUTES.toMillis(1) + TimeUnit.SECONDS.toMillis(1))); + + assertThat(parser.parse("2013-01-01||+1y", 0), equalTo(parser.parse("2013-01-01", 0) + TimeUnit.DAYS.toMillis(365))); + assertThat(parser.parse("2013-03-03||/y", 0), equalTo(parser.parse("2013-01-01", 0))); + assertThat(parser.parseUpperInclusive("2013-03-03||/y", 0), equalTo(parser.parse("2014-01-01", 0))); } }