Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/reference/aggregations/bucket/range-field-note.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ POST /range_index/_search?size=0
"november_data" : {
"date_histogram" : {
"field" : "time_frame",
"calendar_interval" : "day"
"calendar_interval" : "day",
"format": "yyyy-MM-dd"
}
}
}
Expand All @@ -135,35 +136,43 @@ calculated over the ranges of all matching documents.
"aggregations" : {
"november_data" : {
"buckets" : [
{
{
"key_as_string" : "2019-10-28",
"key" : 1572220800000,
"doc_count" : 1
},
{
"key_as_string" : "2019-10-29",
"key" : 1572307200000,
"doc_count" : 1
},
{
"key_as_string" : "2019-10-30",
"key" : 1572393600000,
"doc_count" : 1
},
{
"key_as_string" : "2019-10-31",
"key" : 1572480000000,
"doc_count" : 1
},
{
"key_as_string" : "2019-11-01",
"key" : 1572566400000,
"doc_count" : 1
},
{
"key_as_string" : "2019-11-02",
"key" : 1572652800000,
"doc_count" : 1
},
{
"key_as_string" : "2019-11-03",
"key" : 1572739200000,
"doc_count" : 1
},
{
"key_as_string" : "2019-11-04",
"key" : 1572825600000,
"doc_count" : 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -268,6 +270,23 @@ public Query existsQuery(QueryShardContext context) {
}
}

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (rangeType == RangeType.DATE) {
DateFormatter dateTimeFormatter = this.dateTimeFormatter;
if (format != null) {
dateTimeFormatter = DateFormatter.forPattern(format).withLocale(dateTimeFormatter.locale());
}
if (timeZone == null) {
timeZone = ZoneOffset.UTC;
}
// the resolution here is always set to milliseconds, as aggregations use this formatter mainly and those are always in
// milliseconds. The only special case here is docvalue fields, which are handled somewhere else
return new DocValueFormat.DateTime(dateTimeFormatter, timeZone, DateFieldMapper.Resolution.MILLISECONDS);
}
return super.docValueFormat(format, timeZone);
}

@Override
public Query termQuery(Object value, QueryShardContext context) {
Query query = rangeQuery(value, value, true, true, ShapeRelation.INTERSECTS, null, null, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public void testBasics() throws Exception {
);
}

public void testFormat() throws Exception {
RangeFieldMapper.Range range = new RangeFieldMapper.Range(RangeType.DATE, asLong("2019-08-01T12:14:36"),
asLong("2019-08-01T15:07:22"), true, true);
testCase(
new MatchAllDocsQuery(),
builder -> builder.calendarInterval(DateHistogramInterval.DAY).format("yyyy-MM-dd"),
writer -> writer.addDocument(singleton(new BinaryDocValuesField(FIELD_NAME, RangeType.DATE.encodeRanges(singleton(range))))),
histo -> {
assertEquals(1, histo.getBuckets().size());
assertTrue(AggregationInspectionHelper.hasValue(histo));

assertEquals("2019-08-01", histo.getBuckets().get(0).getKeyAsString());
}
);
}

public void testUnsupportedRangeType() throws Exception {
RangeType rangeType = RangeType.LONG;
final String fieldName = "field";
Expand Down