diff --git a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index ffa362ab5a3..f2a0205e179 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -301,6 +301,9 @@ public class DateFieldMapper extends NumberFieldMapper { } public long parseToMilliseconds(Object value, @Nullable QueryParseContext context, boolean includeUpper) { + if (value instanceof Number) { + return ((Number) value).longValue(); + } long now = context == null ? System.currentTimeMillis() : context.nowInMillis(); return includeUpper && roundCeil ? dateMathParser.parseRoundCeil(convertToString(value), now) : dateMathParser.parse(convertToString(value), now); } @@ -335,14 +338,22 @@ public class DateFieldMapper extends NumberFieldMapper { Long lowerVal = null; Long upperVal = null; if (lowerTerm != null) { - String value = convertToString(lowerTerm); - cache = explicitCaching || !hasNowExpressionWithNoRounding(value); - lowerVal = parseToMilliseconds(value, context, false); + if (lowerTerm instanceof Number) { + lowerVal = ((Number) lowerTerm).longValue(); + } else { + String value = convertToString(lowerTerm); + cache = explicitCaching || !hasNowExpressionWithNoRounding(value); + lowerVal = parseToMilliseconds(value, context, false); + } } if (upperTerm != null) { - String value = convertToString(upperTerm); - cache = explicitCaching || !hasNowExpressionWithNoRounding(value); - upperVal = parseToMilliseconds(value, context, includeUpper); + if (upperTerm instanceof Number) { + upperVal = ((Number) upperTerm).longValue(); + } else { + String value = convertToString(upperTerm); + cache = explicitCaching || !hasNowExpressionWithNoRounding(value); + upperVal = parseToMilliseconds(value, context, includeUpper); + } } Filter filter = NumericRangeFilter.newLongRange( @@ -367,14 +378,22 @@ public class DateFieldMapper extends NumberFieldMapper { Long lowerVal = null; Long upperVal = null; if (lowerTerm != null) { - String value = convertToString(lowerTerm); - cache = explicitCaching || !hasNowExpressionWithNoRounding(value); - lowerVal = parseToMilliseconds(value, context, false); + if (lowerTerm instanceof Number) { + lowerVal = ((Number) lowerTerm).longValue(); + } else { + String value = convertToString(lowerTerm); + cache = explicitCaching || !hasNowExpressionWithNoRounding(value); + lowerVal = parseToMilliseconds(value, context, false); + } } if (upperTerm != null) { - String value = convertToString(upperTerm); - cache = explicitCaching || !hasNowExpressionWithNoRounding(value); - upperVal = parseToMilliseconds(value, context, includeUpper); + if (upperTerm instanceof Number) { + upperVal = ((Number) upperTerm).longValue(); + } else { + String value = convertToString(upperTerm); + cache = explicitCaching || !hasNowExpressionWithNoRounding(value); + upperVal = parseToMilliseconds(value, context, includeUpper); + } } Filter filter = NumericRangeFieldDataFilter.newLongRange( diff --git a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java index ec73a80161f..007b2ae82fc 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -2155,6 +2155,19 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { assertSearchHits(searchResponse, "1"); } + @Test + public void testDateProvidedAsNumber() { + createIndex("test"); + assertAcked(client().admin().indices().preparePutMapping("test").setType("type").setSource("field", "type=date").get()); + client().prepareIndex("test", "type", "1").setSource("field", -1000000000001L).get(); + client().prepareIndex("test", "type", "2").setSource("field", -1000000000000L).get(); + client().prepareIndex("test", "type", "3").setSource("field", -999999999999L).get(); + refresh(); + + assertHitCount(client().prepareCount("test").setQuery(rangeQuery("field").lte(-1000000000000L)).get(), 2); + assertHitCount(client().prepareCount("test").setQuery(rangeQuery("field").lte(-999999999999L)).get(), 3); + } + @Test public void testRangeFilterNoCacheWithNow() throws Exception { assertAcked(prepareCreate("test")