- Fixed timezone parsing when input starts with '+'sign. Fixes issue #2141
This commit is contained in:
parent
37e7a54b0e
commit
195e586fd8
|
@ -183,7 +183,8 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
|
|||
if (offset.charAt(0) == '-') {
|
||||
return -TimeValue.parseTimeValue(offset.substring(1), null).millis();
|
||||
}
|
||||
return TimeValue.parseTimeValue(offset, null).millis();
|
||||
int beginIndex = offset.charAt(0) == '+' ? 1 : 0;
|
||||
return TimeValue.parseTimeValue(offset.substring(beginIndex), null).millis();
|
||||
}
|
||||
|
||||
private DateTimeZone parseZone(XContentParser parser, XContentParser.Token token) throws IOException {
|
||||
|
@ -193,9 +194,10 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
|
|||
String text = parser.text();
|
||||
int index = text.indexOf(':');
|
||||
if (index != -1) {
|
||||
int beginIndex = text.charAt(0) == '+' ? 1 : 0;
|
||||
// format like -02:30
|
||||
return DateTimeZone.forOffsetHoursMinutes(
|
||||
Integer.parseInt(text.substring(0, index)),
|
||||
Integer.parseInt(text.substring(beginIndex, index)),
|
||||
Integer.parseInt(text.substring(index + 1))
|
||||
);
|
||||
} else {
|
||||
|
|
|
@ -1478,6 +1478,73 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
// https://github.com/elasticsearch/elasticsearch/issues/2141
|
||||
public void testDateHistoFacets_preZoneBug() throws Exception {
|
||||
try {
|
||||
client.admin().indices().prepareDelete("test").execute().actionGet();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
client.admin().indices().prepareCreate("test").execute().actionGet();
|
||||
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
|
||||
|
||||
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
|
||||
.field("date", "2009-03-05T23:31:01")
|
||||
.field("num", 1)
|
||||
.endObject()).execute().actionGet();
|
||||
client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();
|
||||
|
||||
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
|
||||
.field("date", "2009-03-05T18:01:01")
|
||||
.field("num", 2)
|
||||
.endObject()).execute().actionGet();
|
||||
client.admin().indices().prepareRefresh().execute().actionGet();
|
||||
|
||||
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
|
||||
.field("date", "2009-03-05T22:01:01")
|
||||
.field("num", 3)
|
||||
.endObject()).execute().actionGet();
|
||||
client.admin().indices().prepareRefresh().execute().actionGet();
|
||||
|
||||
|
||||
for (int i = 0; i < numberOfRuns(); i++) {
|
||||
SearchResponse searchResponse = client.prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.addFacet(dateHistogramFacet("stats1").field("date").interval("day").preZone("+02:00"))
|
||||
.addFacet(dateHistogramFacet("stats2").field("date").valueField("num").interval("day").preZone("+01:30"))
|
||||
.execute().actionGet();
|
||||
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
logger.warn("Failed shards:");
|
||||
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
|
||||
logger.warn("-> {}", shardSearchFailure);
|
||||
}
|
||||
}
|
||||
assertThat(searchResponse.failedShards(), equalTo(0));
|
||||
|
||||
// time zone causes the dates to shift by 2:00
|
||||
DateHistogramFacet facet = searchResponse.facets().facet("stats1");
|
||||
assertThat(facet.name(), equalTo("stats1"));
|
||||
assertThat(facet.entries().size(), equalTo(2));
|
||||
assertThat(facet.entries().get(0).time(), equalTo(utcTimeInMillis("2009-03-05")));
|
||||
assertThat(facet.entries().get(0).count(), equalTo(1l));
|
||||
assertThat(facet.entries().get(1).time(), equalTo(utcTimeInMillis("2009-03-06")));
|
||||
assertThat(facet.entries().get(1).count(), equalTo(2l));
|
||||
|
||||
// time zone causes the dates to shift by 1:30
|
||||
facet = searchResponse.facets().facet("stats2");
|
||||
assertThat(facet.name(), equalTo("stats2"));
|
||||
assertThat(facet.entries().size(), equalTo(2));
|
||||
assertThat(facet.entries().get(0).time(), equalTo(utcTimeInMillis("2009-03-05")));
|
||||
assertThat(facet.entries().get(0).count(), equalTo(2l));
|
||||
assertThat(facet.entries().get(0).total(), equalTo(5d));
|
||||
assertThat(facet.entries().get(1).time(), equalTo(utcTimeInMillis("2009-03-06")));
|
||||
assertThat(facet.entries().get(1).count(), equalTo(1l));
|
||||
assertThat(facet.entries().get(1).total(), equalTo(1d));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTermsStatsFacets() throws Exception {
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue