20
20
21
21
import org .elasticsearch .action .index .IndexRequestBuilder ;
22
22
import org .elasticsearch .action .search .SearchResponse ;
23
+ import org .elasticsearch .common .joda .Joda ;
23
24
import org .elasticsearch .common .settings .ImmutableSettings ;
24
25
import org .elasticsearch .common .settings .Settings ;
25
26
import org .elasticsearch .common .xcontent .XContentBuilder ;
27
+ import org .elasticsearch .index .mapper .core .DateFieldMapper ;
26
28
import org .elasticsearch .search .aggregations .AbstractAggregationBuilder ;
27
29
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogram ;
28
30
import org .elasticsearch .search .aggregations .bucket .histogram .Histogram ;
37
39
38
40
import java .io .IOException ;
39
41
import java .util .ArrayList ;
42
+ import java .util .Collection ;
40
43
import java .util .List ;
41
44
42
45
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
@@ -64,6 +67,10 @@ private DateTime date(int month, int day) {
64
67
return new DateTime (2012 , month , day , 0 , 0 , DateTimeZone .UTC );
65
68
}
66
69
70
+ private DateTime date (String date ) {
71
+ return DateFieldMapper .Defaults .DATE_TIME_FORMATTER .parser ().parseDateTime (date );
72
+ }
73
+
67
74
private IndexRequestBuilder indexDoc (int month , int day , int value ) throws Exception {
68
75
return client ().prepareIndex ("idx" , "type" ).setSource (jsonBuilder ()
69
76
.startObject ()
@@ -88,6 +95,23 @@ public void init() throws Exception {
88
95
ensureSearchable ();
89
96
}
90
97
98
+ private static DateHistogram .Bucket getBucket (DateHistogram histogram , DateTime key ) {
99
+ return getBucket (histogram , key , DateFieldMapper .Defaults .DATE_TIME_FORMATTER .format ());
100
+ }
101
+
102
+ private static DateHistogram .Bucket getBucket (DateHistogram histogram , DateTime key , String format ) {
103
+ if (randomBoolean ()) {
104
+ if (randomBoolean ()) {
105
+ return histogram .getBucketByKey (key );
106
+ }
107
+ return histogram .getBucketByKey (key .getMillis ());
108
+ }
109
+ if (randomBoolean ()) {
110
+ return histogram .getBucketByKey ("" + key .getMillis ());
111
+ }
112
+ return histogram .getBucketByKey (Joda .forPattern (format ).printer ().print (key ));
113
+ }
114
+
91
115
@ Test
92
116
public void singleValuedField () throws Exception {
93
117
SearchResponse response = client ().prepareSearch ("idx" )
@@ -102,22 +126,22 @@ public void singleValuedField() throws Exception {
102
126
assertThat (histo .getName (), equalTo ("histo" ));
103
127
assertThat (histo .getBuckets ().size (), equalTo (3 ));
104
128
105
- long key = new DateTime (2012 , 1 , 1 , 0 , 0 , DateTimeZone .UTC ). getMillis ( );
106
- DateHistogram .Bucket bucket = histo . getBucketByKey ( key );
129
+ DateTime key = new DateTime (2012 , 1 , 1 , 0 , 0 , DateTimeZone .UTC );
130
+ DateHistogram .Bucket bucket = getBucket ( histo , key );
107
131
assertThat (bucket , notNullValue ());
108
- assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key ));
132
+ assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key . getMillis () ));
109
133
assertThat (bucket .getDocCount (), equalTo (1l ));
110
134
111
- key = new DateTime (2012 , 2 , 1 , 0 , 0 , DateTimeZone .UTC ). getMillis () ;
112
- bucket = histo . getBucketByKey ( key );
135
+ key = new DateTime (2012 , 2 , 1 , 0 , 0 , DateTimeZone .UTC );
136
+ bucket = getBucket ( histo , key );
113
137
assertThat (bucket , notNullValue ());
114
- assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key ));
138
+ assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key . getMillis () ));
115
139
assertThat (bucket .getDocCount (), equalTo (2l ));
116
140
117
- key = new DateTime (2012 , 3 , 1 , 0 , 0 , DateTimeZone .UTC ). getMillis () ;
118
- bucket = histo . getBucketByKey ( key );
141
+ key = new DateTime (2012 , 3 , 1 , 0 , 0 , DateTimeZone .UTC );
142
+ bucket = getBucket ( histo , key );
119
143
assertThat (bucket , notNullValue ());
120
- assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key ));
144
+ assertThat (bucket .getKeyAsNumber ().longValue (), equalTo (key . getMillis () ));
121
145
assertThat (bucket .getDocCount (), equalTo (3l ));
122
146
}
123
147
@@ -437,7 +461,6 @@ public void singleValuedField_OrderedByMultiValuedSubAggregationAsc_Inherited()
437
461
438
462
assertSearchResponse (response );
439
463
440
-
441
464
DateHistogram histo = response .getAggregations ().get ("histo" );
442
465
assertThat (histo , notNullValue ());
443
466
assertThat (histo .getName (), equalTo ("histo" ));
@@ -980,4 +1003,75 @@ public void emptyAggregation() throws Exception {
980
1003
assertThat (dateHisto .getBuckets ().isEmpty (), is (true ));
981
1004
982
1005
}
1006
+
1007
+ @ Test
1008
+ public void singleValue_WithPreZone () throws Exception {
1009
+ prepareCreate ("idx2" ).addMapping ("type" , "date" , "type=date" ).execute ().actionGet ();
1010
+ IndexRequestBuilder [] reqs = new IndexRequestBuilder [5 ];
1011
+ DateTime date = date ("2014-03-11T00:00:00+00:00" );
1012
+ for (int i = 0 ; i < reqs .length ; i ++) {
1013
+ reqs [i ] = client ().prepareIndex ("idx2" , "type" , "" + i ).setSource (jsonBuilder ().startObject ().field ("date" , date ).endObject ());
1014
+ date = date .plusHours (1 );
1015
+ }
1016
+ indexRandom (true , reqs );
1017
+
1018
+ SearchResponse response = client ().prepareSearch ("idx2" )
1019
+ .setQuery (matchAllQuery ())
1020
+ .addAggregation (dateHistogram ("date_histo" )
1021
+ .field ("date" )
1022
+ .preZone ("-2:00" )
1023
+ .interval (DateHistogram .Interval .DAY )
1024
+ .format ("yyyy-MM-dd" ))
1025
+ .execute ().actionGet ();
1026
+
1027
+ assertThat (response .getHits ().getTotalHits (), equalTo (5l ));
1028
+
1029
+ DateHistogram histo = response .getAggregations ().get ("date_histo" );
1030
+ Collection <? extends DateHistogram .Bucket > buckets = histo .getBuckets ();
1031
+ assertThat (buckets .size (), equalTo (2 ));
1032
+
1033
+ DateHistogram .Bucket bucket = histo .getBucketByKey ("2014-03-10" );
1034
+ assertThat (bucket , Matchers .notNullValue ());
1035
+ assertThat (bucket .getDocCount (), equalTo (2l ));
1036
+
1037
+ bucket = histo .getBucketByKey ("2014-03-11" );
1038
+ assertThat (bucket , Matchers .notNullValue ());
1039
+ assertThat (bucket .getDocCount (), equalTo (3l ));
1040
+ }
1041
+
1042
+ @ Test
1043
+ public void singleValue_WithPreZone_WithAadjustLargeInterval () throws Exception {
1044
+ prepareCreate ("idx2" ).addMapping ("type" , "date" , "type=date" ).execute ().actionGet ();
1045
+ IndexRequestBuilder [] reqs = new IndexRequestBuilder [5 ];
1046
+ DateTime date = date ("2014-03-11T00:00:00+00:00" );
1047
+ for (int i = 0 ; i < reqs .length ; i ++) {
1048
+ reqs [i ] = client ().prepareIndex ("idx2" , "type" , "" + i ).setSource (jsonBuilder ().startObject ().field ("date" , date ).endObject ());
1049
+ date = date .plusHours (1 );
1050
+ }
1051
+ indexRandom (true , reqs );
1052
+
1053
+ SearchResponse response = client ().prepareSearch ("idx2" )
1054
+ .setQuery (matchAllQuery ())
1055
+ .addAggregation (dateHistogram ("date_histo" )
1056
+ .field ("date" )
1057
+ .preZone ("-2:00" )
1058
+ .interval (DateHistogram .Interval .DAY )
1059
+ .preZoneAdjustLargeInterval (true )
1060
+ .format ("yyyy-MM-dd'T'HH:mm:ss" ))
1061
+ .execute ().actionGet ();
1062
+
1063
+ assertThat (response .getHits ().getTotalHits (), equalTo (5l ));
1064
+
1065
+ DateHistogram histo = response .getAggregations ().get ("date_histo" );
1066
+ Collection <? extends DateHistogram .Bucket > buckets = histo .getBuckets ();
1067
+ assertThat (buckets .size (), equalTo (2 ));
1068
+
1069
+ DateHistogram .Bucket bucket = histo .getBucketByKey ("2014-03-10T02:00:00" );
1070
+ assertThat (bucket , Matchers .notNullValue ());
1071
+ assertThat (bucket .getDocCount (), equalTo (2l ));
1072
+
1073
+ bucket = histo .getBucketByKey ("2014-03-11T02:00:00" );
1074
+ assertThat (bucket , Matchers .notNullValue ());
1075
+ assertThat (bucket .getDocCount (), equalTo (3l ));
1076
+ }
983
1077
}
0 commit comments