Skip to content

Commit 25ae050

Browse files
not-napoleonpolyfractal
authored andcommitted
DocValueFormat implementation for date range fields (#47472) (#47606)
1 parent 7b1673a commit 25ae050

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

docs/reference/aggregations/bucket/range-field-note.asciidoc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ POST /range_index/_search?size=0
117117
"november_data" : {
118118
"date_histogram" : {
119119
"field" : "time_frame",
120-
"calendar_interval" : "day"
120+
"calendar_interval" : "day",
121+
"format": "yyyy-MM-dd"
121122
}
122123
}
123124
}
@@ -135,35 +136,43 @@ calculated over the ranges of all matching documents.
135136
"aggregations" : {
136137
"november_data" : {
137138
"buckets" : [
138-
{
139+
{
140+
"key_as_string" : "2019-10-28",
139141
"key" : 1572220800000,
140142
"doc_count" : 1
141143
},
142144
{
145+
"key_as_string" : "2019-10-29",
143146
"key" : 1572307200000,
144147
"doc_count" : 1
145148
},
146149
{
150+
"key_as_string" : "2019-10-30",
147151
"key" : 1572393600000,
148152
"doc_count" : 1
149153
},
150154
{
155+
"key_as_string" : "2019-10-31",
151156
"key" : 1572480000000,
152157
"doc_count" : 1
153158
},
154159
{
160+
"key_as_string" : "2019-11-01",
155161
"key" : 1572566400000,
156162
"doc_count" : 1
157163
},
158164
{
165+
"key_as_string" : "2019-11-02",
159166
"key" : 1572652800000,
160167
"doc_count" : 1
161168
},
162169
{
170+
"key_as_string" : "2019-11-03",
163171
"key" : 1572739200000,
164172
"doc_count" : 1
165173
},
166174
{
175+
"key_as_string" : "2019-11-04",
167176
"key" : 1572825600000,
168177
"doc_count" : 1
169178
}

server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
import org.elasticsearch.index.fielddata.IndexFieldData;
4545
import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData;
4646
import org.elasticsearch.index.query.QueryShardContext;
47+
import org.elasticsearch.search.DocValueFormat;
4748

4849
import java.io.IOException;
4950
import java.net.InetAddress;
5051
import java.net.UnknownHostException;
5152
import java.time.ZoneId;
53+
import java.time.ZoneOffset;
5254
import java.util.HashSet;
5355
import java.util.Iterator;
5456
import java.util.List;
@@ -268,6 +270,23 @@ public Query existsQuery(QueryShardContext context) {
268270
}
269271
}
270272

273+
@Override
274+
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
275+
if (rangeType == RangeType.DATE) {
276+
DateFormatter dateTimeFormatter = this.dateTimeFormatter;
277+
if (format != null) {
278+
dateTimeFormatter = DateFormatter.forPattern(format).withLocale(dateTimeFormatter.locale());
279+
}
280+
if (timeZone == null) {
281+
timeZone = ZoneOffset.UTC;
282+
}
283+
// the resolution here is always set to milliseconds, as aggregations use this formatter mainly and those are always in
284+
// milliseconds. The only special case here is docvalue fields, which are handled somewhere else
285+
return new DocValueFormat.DateTime(dateTimeFormatter, timeZone, DateFieldMapper.Resolution.MILLISECONDS);
286+
}
287+
return super.docValueFormat(format, timeZone);
288+
}
289+
271290
@Override
272291
public Query termQuery(Object value, QueryShardContext context) {
273292
Query query = rangeQuery(value, value, true, true, ShapeRelation.INTERSECTS, null, null, context);

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public void testBasics() throws Exception {
6565
);
6666
}
6767

68+
public void testFormat() throws Exception {
69+
RangeFieldMapper.Range range = new RangeFieldMapper.Range(RangeType.DATE, asLong("2019-08-01T12:14:36"),
70+
asLong("2019-08-01T15:07:22"), true, true);
71+
testCase(
72+
new MatchAllDocsQuery(),
73+
builder -> builder.calendarInterval(DateHistogramInterval.DAY).format("yyyy-MM-dd"),
74+
writer -> writer.addDocument(singleton(new BinaryDocValuesField(FIELD_NAME, RangeType.DATE.encodeRanges(singleton(range))))),
75+
histo -> {
76+
assertEquals(1, histo.getBuckets().size());
77+
assertTrue(AggregationInspectionHelper.hasValue(histo));
78+
79+
assertEquals("2019-08-01", histo.getBuckets().get(0).getKeyAsString());
80+
}
81+
);
82+
}
83+
6884
public void testUnsupportedRangeType() throws Exception {
6985
RangeType rangeType = RangeType.LONG;
7086
final String fieldName = "field";

0 commit comments

Comments
 (0)