Skip to content

Commit d67a64c

Browse files
authored
Tests for agg missing values (#51068)
1 parent aa66d1a commit d67a64c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,23 @@ public void testNoDocs() throws IOException {
7676
});
7777
}
7878

79-
public void testFieldMissing() throws IOException {
79+
public void testUnmapped() throws IOException {
8080
testCase(new MatchAllDocsQuery(), "wrong_field", randomPrecision(), null, geoGrid -> {
8181
assertEquals(0, geoGrid.getBuckets().size());
8282
}, iw -> {
8383
iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D)));
8484
});
8585
}
8686

87+
public void testUnmappedMissing() throws IOException {
88+
GeoGridAggregationBuilder builder = createBuilder("_name")
89+
.field("wrong_field")
90+
.missing("53.69437,6.475031");
91+
testCase(new MatchAllDocsQuery(), randomPrecision(), null, geoGrid -> assertEquals(1, geoGrid.getBuckets().size()),
92+
iw -> iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D))), builder);
93+
94+
}
95+
8796
public void testWithSeveralDocs() throws IOException {
8897
int precision = randomPrecision();
8998
int numPoints = randomIntBetween(8, 128);
@@ -189,6 +198,13 @@ public void testBounds() throws IOException {
189198
private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox,
190199
Consumer<InternalGeoGrid<T>> verify,
191200
CheckedConsumer<RandomIndexWriter, IOException> buildIndex) throws IOException {
201+
testCase(query, precision, geoBoundingBox, verify, buildIndex, createBuilder("_name").field(field));
202+
}
203+
204+
private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox,
205+
Consumer<InternalGeoGrid<T>> verify,
206+
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
207+
GeoGridAggregationBuilder aggregationBuilder) throws IOException {
192208
Directory directory = newDirectory();
193209
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
194210
buildIndex.accept(indexWriter);
@@ -197,7 +213,6 @@ private void testCase(Query query, String field, int precision, GeoBoundingBox g
197213
IndexReader indexReader = DirectoryReader.open(directory);
198214
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
199215

200-
GeoGridAggregationBuilder aggregationBuilder = createBuilder("_name").field(field);
201216
aggregationBuilder.precision(precision);
202217
if (geoBoundingBox != null) {
203218
aggregationBuilder.setGeoBoundingBox(geoBoundingBox);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ public void testAggregateWrongField() throws IOException {
219219
);
220220
}
221221

222+
public void testUnmappedMissing() throws IOException {
223+
testBothCases(DEFAULT_QUERY, DATES_WITH_TIME,
224+
aggregation -> aggregation.setNumBuckets(10).field("wrong_field").missing("2017-12-12"),
225+
histogram -> {
226+
assertEquals(1, histogram.getBuckets().size());
227+
assertTrue(AggregationInspectionHelper.hasValue(histogram));
228+
}
229+
);
230+
}
231+
232+
222233
public void testIntervalYear() throws IOException {
223234

224235

server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ public void testUnmapped() throws Exception {
8484
}
8585
}
8686

87+
public void testUnmappedWithMissing() throws Exception {
88+
try (Directory dir = newDirectory();
89+
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
90+
GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg")
91+
.field("another_field")
92+
.missing("53.69437,6.475031");
93+
94+
GeoPoint expectedCentroid = new GeoPoint(53.69437, 6.475031);
95+
Document document = new Document();
96+
document.add(new LatLonDocValuesField("field", 10, 10));
97+
w.addDocument(document);
98+
try (IndexReader reader = w.getReader()) {
99+
IndexSearcher searcher = new IndexSearcher(reader);
100+
101+
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
102+
fieldType.setHasDocValues(true);
103+
fieldType.setName("another_field");
104+
InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
105+
assertEquals(result.centroid(), expectedCentroid);
106+
assertTrue(AggregationInspectionHelper.hasValue(result));
107+
}
108+
}
109+
}
110+
87111
public void testSingleValuedField() throws Exception {
88112
int numDocs = scaledRandomIntBetween(64, 256);
89113
int numUniqueGeoPoints = randomIntBetween(1, numDocs);

0 commit comments

Comments
 (0)