Skip to content

Commit 10e0e59

Browse files
authored
Tests for agg missing values (#51068) (#54452)
1 parent 3a24fe9 commit 10e0e59

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
@@ -78,14 +78,23 @@ public void testNoDocs() throws IOException {
7878
});
7979
}
8080

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

89+
public void testUnmappedMissing() throws IOException {
90+
GeoGridAggregationBuilder builder = createBuilder("_name")
91+
.field("wrong_field")
92+
.missing("53.69437,6.475031");
93+
testCase(new MatchAllDocsQuery(), randomPrecision(), null, geoGrid -> assertEquals(1, geoGrid.getBuckets().size()),
94+
iw -> iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D))), builder);
95+
96+
}
97+
8998
public void testWithSeveralDocs() throws IOException {
9099
int precision = randomPrecision();
91100
int numPoints = randomIntBetween(8, 128);
@@ -197,6 +206,13 @@ public void testBounds() throws IOException {
197206
private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox,
198207
Consumer<InternalGeoGrid<T>> verify,
199208
CheckedConsumer<RandomIndexWriter, IOException> buildIndex) throws IOException {
209+
testCase(query, precision, geoBoundingBox, verify, buildIndex, createBuilder("_name").field(field));
210+
}
211+
212+
private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox,
213+
Consumer<InternalGeoGrid<T>> verify,
214+
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
215+
GeoGridAggregationBuilder aggregationBuilder) throws IOException {
200216
Directory directory = newDirectory();
201217
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
202218
buildIndex.accept(indexWriter);
@@ -205,7 +221,6 @@ private void testCase(Query query, String field, int precision, GeoBoundingBox g
205221
IndexReader indexReader = DirectoryReader.open(directory);
206222
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
207223

208-
GeoGridAggregationBuilder aggregationBuilder = createBuilder("_name").field(field);
209224
aggregationBuilder.precision(precision);
210225
if (geoBoundingBox != null) {
211226
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)