Tests for agg missing values (#51068) (#54452)

This commit is contained in:
Mark Tozzi 2020-03-30 15:05:38 -04:00 committed by GitHub
parent 3a24fe9d37
commit 10e0e59561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 2 deletions

View File

@ -78,7 +78,7 @@ public abstract class GeoGridAggregatorTestCase<T extends InternalGeoGridBucket>
}); });
} }
public void testFieldMissing() throws IOException { public void testUnmapped() throws IOException {
testCase(new MatchAllDocsQuery(), "wrong_field", randomPrecision(), null, geoGrid -> { testCase(new MatchAllDocsQuery(), "wrong_field", randomPrecision(), null, geoGrid -> {
assertEquals(0, geoGrid.getBuckets().size()); assertEquals(0, geoGrid.getBuckets().size());
}, iw -> { }, iw -> {
@ -86,6 +86,15 @@ public abstract class GeoGridAggregatorTestCase<T extends InternalGeoGridBucket>
}); });
} }
public void testUnmappedMissing() throws IOException {
GeoGridAggregationBuilder builder = createBuilder("_name")
.field("wrong_field")
.missing("53.69437,6.475031");
testCase(new MatchAllDocsQuery(), randomPrecision(), null, geoGrid -> assertEquals(1, geoGrid.getBuckets().size()),
iw -> iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D))), builder);
}
public void testWithSeveralDocs() throws IOException { public void testWithSeveralDocs() throws IOException {
int precision = randomPrecision(); int precision = randomPrecision();
int numPoints = randomIntBetween(8, 128); int numPoints = randomIntBetween(8, 128);
@ -197,6 +206,13 @@ public abstract class GeoGridAggregatorTestCase<T extends InternalGeoGridBucket>
private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox, private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox,
Consumer<InternalGeoGrid<T>> verify, Consumer<InternalGeoGrid<T>> verify,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex) throws IOException { CheckedConsumer<RandomIndexWriter, IOException> buildIndex) throws IOException {
testCase(query, precision, geoBoundingBox, verify, buildIndex, createBuilder("_name").field(field));
}
private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox,
Consumer<InternalGeoGrid<T>> verify,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
GeoGridAggregationBuilder aggregationBuilder) throws IOException {
Directory directory = newDirectory(); Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter); buildIndex.accept(indexWriter);
@ -205,7 +221,6 @@ public abstract class GeoGridAggregatorTestCase<T extends InternalGeoGridBucket>
IndexReader indexReader = DirectoryReader.open(directory); IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true); IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
GeoGridAggregationBuilder aggregationBuilder = createBuilder("_name").field(field);
aggregationBuilder.precision(precision); aggregationBuilder.precision(precision);
if (geoBoundingBox != null) { if (geoBoundingBox != null) {
aggregationBuilder.setGeoBoundingBox(geoBoundingBox); aggregationBuilder.setGeoBoundingBox(geoBoundingBox);

View File

@ -219,6 +219,17 @@ public class AutoDateHistogramAggregatorTests extends AggregatorTestCase {
); );
} }
public void testUnmappedMissing() throws IOException {
testBothCases(DEFAULT_QUERY, DATES_WITH_TIME,
aggregation -> aggregation.setNumBuckets(10).field("wrong_field").missing("2017-12-12"),
histogram -> {
assertEquals(1, histogram.getBuckets().size());
assertTrue(AggregationInspectionHelper.hasValue(histogram));
}
);
}
public void testIntervalYear() throws IOException { public void testIntervalYear() throws IOException {

View File

@ -84,6 +84,30 @@ public class GeoCentroidAggregatorTests extends AggregatorTestCase {
} }
} }
public void testUnmappedWithMissing() throws Exception {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg")
.field("another_field")
.missing("53.69437,6.475031");
GeoPoint expectedCentroid = new GeoPoint(53.69437, 6.475031);
Document document = new Document();
document.add(new LatLonDocValuesField("field", 10, 10));
w.addDocument(document);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true);
fieldType.setName("another_field");
InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertEquals(result.centroid(), expectedCentroid);
assertTrue(AggregationInspectionHelper.hasValue(result));
}
}
}
public void testSingleValuedField() throws Exception { public void testSingleValuedField() throws Exception {
int numDocs = scaledRandomIntBetween(64, 256); int numDocs = scaledRandomIntBetween(64, 256);
int numUniqueGeoPoints = randomIntBetween(1, numDocs); int numUniqueGeoPoints = randomIntBetween(1, numDocs);