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.
This commit is contained in:
Subhash Gopalakrishnan 2013-10-10 19:58:59 +05:30 committed by David Pilato
parent 7674f1edd4
commit b758b76da4
3 changed files with 20 additions and 1 deletions

View File

@ -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` The expression starts with an "anchor" date, which can be either `now`
or a date string (in the applicable format) ending with `||`. It can or a date string (in the applicable format) ending with `||`. It can
then follow by a math expression, supporting `+`, `-` and `/` 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). `m` (minute), and `s` (second).
Here are some samples: `now+1h`, `now+1h+1m`, `now+1h/d`, Here are some samples: `now+1h`, `now+1h+1m`, `now+1h/d`,

View File

@ -91,6 +91,19 @@ public class DateMathParser {
} }
char unit = mathString.charAt(i++); char unit = mathString.charAt(i++);
switch (unit) { 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': case 'M':
if (type == 0) { if (type == 0) {
if (roundUp) { if (roundUp) {

View File

@ -27,6 +27,8 @@ public class DateMathParserTests {
assertThat(parser.parse("now+1m+1s/m", 0), equalTo(TimeUnit.MINUTES.toMillis(1))); 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.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 @Test
@ -36,5 +38,9 @@ public class DateMathParserTests {
assertThat(parser.parse("1970-01-01", 0), equalTo(0l)); 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", 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("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)));
} }
} }