Browse Source

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.

Subhash Gopalakrishnan 12 years ago
parent
commit
b758b76da4

+ 1 - 1
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`,

+ 13 - 0
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) {

+ 6 - 0
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)));
     }
 }