Skip to content

Commit be8a531

Browse files
committed
Extend nextDoc to delegate to the wrapped doc-value iterator for date_nanos (#39176)
The type date_nanos does not direct doc-value iterators and it needs to extend `next_doc` in order to delegate the call to the wrapped iterator.
1 parent f40139c commit be8a531

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

server/src/main/java/org/elasticsearch/index/fielddata/plain/SortedNumericDVIndexFieldData.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ public long nextValue() throws IOException {
177177
public int docValueCount() {
178178
return dv.docValueCount();
179179
}
180+
181+
@Override
182+
public int nextDoc() throws IOException {
183+
return dv.nextDoc();
184+
}
180185
};
181186
}
182187

server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
package org.elasticsearch.index.mapper;
2020

2121
import org.apache.lucene.document.LongPoint;
22+
import org.apache.lucene.document.NumericDocValuesField;
2223
import org.apache.lucene.document.SortedNumericDocValuesField;
2324
import org.apache.lucene.index.DirectoryReader;
2425
import org.apache.lucene.index.IndexOptions;
2526
import org.apache.lucene.index.IndexReader;
2627
import org.apache.lucene.index.IndexWriter;
2728
import org.apache.lucene.index.IndexWriterConfig;
2829
import org.apache.lucene.index.MultiReader;
30+
import org.apache.lucene.index.SortedNumericDocValues;
31+
import org.apache.lucene.search.DocIdSetIterator;
2932
import org.apache.lucene.search.IndexOrDocValuesQuery;
3033
import org.apache.lucene.search.Query;
3134
import org.apache.lucene.store.Directory;
@@ -37,6 +40,9 @@
3740
import org.elasticsearch.common.time.DateMathParser;
3841
import org.elasticsearch.core.internal.io.IOUtils;
3942
import org.elasticsearch.index.IndexSettings;
43+
import org.elasticsearch.index.fielddata.AtomicNumericFieldData;
44+
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
45+
import org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData;
4046
import org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType;
4147
import org.elasticsearch.index.mapper.MappedFieldType.Relation;
4248
import org.elasticsearch.index.mapper.ParseContext.Document;
@@ -214,4 +220,33 @@ public void testRangeQuery() throws IOException {
214220
() -> ft.rangeQuery(date1, date2, true, true, null, null, null, context));
215221
assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
216222
}
223+
224+
public void testDateNanoDocValues() throws IOException {
225+
// Create an index with some docValues
226+
Directory dir = newDirectory();
227+
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
228+
Document doc = new Document();
229+
NumericDocValuesField docValuesField = new NumericDocValuesField("my_date", 1444608000000L);
230+
doc.add(docValuesField);
231+
w.addDocument(doc);
232+
docValuesField.setLongValue(1459641600000L);
233+
w.addDocument(doc);
234+
// Create the doc values reader
235+
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
236+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
237+
IndexSettings indexSettings = new IndexSettings(IndexMetaData.builder("foo").settings(settings).build(), settings);
238+
SortedNumericDVIndexFieldData fieldData = new SortedNumericDVIndexFieldData(indexSettings.getIndex(), "my_date",
239+
IndexNumericFieldData.NumericType.DATE_NANOSECONDS);
240+
// Read index and check the doc values
241+
DirectoryReader reader = DirectoryReader.open(w);
242+
assertTrue(reader.leaves().size() > 0);
243+
AtomicNumericFieldData a = fieldData.load(reader.leaves().get(0).reader().getContext());
244+
SortedNumericDocValues docValues = a.getLongValues();
245+
assertEquals(0, docValues.nextDoc());
246+
assertEquals(1, docValues.nextDoc());
247+
assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.nextDoc());
248+
reader.close();
249+
w.close();
250+
dir.close();
251+
}
217252
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,4 +1561,47 @@ private void assertMultiSortResponse(int[] expectedDays, BucketOrder... order) {
15611561
private ZonedDateTime key(Histogram.Bucket bucket) {
15621562
return (ZonedDateTime) bucket.getKey();
15631563
}
1564+
1565+
/**
1566+
* See https://github.com/elastic/elasticsearch/issues/39107. Make sure we handle properly different
1567+
* timeZones.
1568+
*/
1569+
public void testDateNanosHistogram() throws Exception {
1570+
assertAcked(prepareCreate("nanos").addMapping("_doc", "date", "type=date_nanos").get());
1571+
indexRandom(true,
1572+
client().prepareIndex("nanos", "_doc", "1").setSource("date", "2000-01-01"));
1573+
indexRandom(true,
1574+
client().prepareIndex("nanos", "_doc", "2").setSource("date", "2000-01-02"));
1575+
1576+
//Search interval 24 hours
1577+
SearchResponse r = client().prepareSearch("nanos")
1578+
.addAggregation(dateHistogram("histo").field("date").
1579+
interval(1000 * 60 * 60 * 24).timeZone(ZoneId.of("Europe/Berlin")))
1580+
.addDocValueField("date")
1581+
.get();
1582+
assertSearchResponse(r);
1583+
1584+
Histogram histogram = r.getAggregations().get("histo");
1585+
List<? extends Bucket> buckets = histogram.getBuckets();
1586+
assertEquals(2, buckets.size());
1587+
assertEquals(946681200000L, ((ZonedDateTime)buckets.get(0).getKey()).toEpochSecond() * 1000);
1588+
assertEquals(1, buckets.get(0).getDocCount());
1589+
assertEquals(946767600000L, ((ZonedDateTime)buckets.get(1).getKey()).toEpochSecond() * 1000);
1590+
assertEquals(1, buckets.get(1).getDocCount());
1591+
1592+
r = client().prepareSearch("nanos")
1593+
.addAggregation(dateHistogram("histo").field("date")
1594+
.interval(1000 * 60 * 60 * 24).timeZone(ZoneId.of("UTC")))
1595+
.addDocValueField("date")
1596+
.get();
1597+
assertSearchResponse(r);
1598+
1599+
histogram = r.getAggregations().get("histo");
1600+
buckets = histogram.getBuckets();
1601+
assertEquals(2, buckets.size());
1602+
assertEquals(946684800000L, ((ZonedDateTime)buckets.get(0).getKey()).toEpochSecond() * 1000);
1603+
assertEquals(1, buckets.get(0).getDocCount());
1604+
assertEquals(946771200000L, ((ZonedDateTime)buckets.get(1).getKey()).toEpochSecond() * 1000);
1605+
assertEquals(1, buckets.get(1).getDocCount());
1606+
}
15641607
}

0 commit comments

Comments
 (0)