From e5fb48d889332ff77d310ea800e385c92a348139 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Thu, 16 Jan 2020 10:53:37 -0500 Subject: [PATCH] Tests for agg missing values (#51068) --- .../geogrid/GeoGridAggregatorTestCase.java | 19 +++++++++++++-- .../AutoDateHistogramAggregatorTests.java | 11 +++++++++ .../metrics/GeoCentroidAggregatorTests.java | 24 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java index 15d6f503d03f6..c7cacb0d960a0 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java @@ -76,7 +76,7 @@ public void testNoDocs() throws IOException { }); } - public void testFieldMissing() throws IOException { + public void testUnmapped() throws IOException { testCase(new MatchAllDocsQuery(), "wrong_field", randomPrecision(), null, geoGrid -> { assertEquals(0, geoGrid.getBuckets().size()); }, iw -> { @@ -84,6 +84,15 @@ public void testFieldMissing() throws IOException { }); } + 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 { int precision = randomPrecision(); int numPoints = randomIntBetween(8, 128); @@ -189,6 +198,13 @@ public void testBounds() throws IOException { private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox, Consumer> verify, CheckedConsumer buildIndex) throws IOException { + testCase(query, precision, geoBoundingBox, verify, buildIndex, createBuilder("_name").field(field)); + } + + private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox, + Consumer> verify, + CheckedConsumer buildIndex, + GeoGridAggregationBuilder aggregationBuilder) throws IOException { Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); buildIndex.accept(indexWriter); @@ -197,7 +213,6 @@ private void testCase(Query query, String field, int precision, GeoBoundingBox g IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - GeoGridAggregationBuilder aggregationBuilder = createBuilder("_name").field(field); aggregationBuilder.precision(precision); if (geoBoundingBox != null) { aggregationBuilder.setGeoBoundingBox(geoBoundingBox); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java index c6bb10fa6c947..05407483a593c 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java @@ -219,6 +219,17 @@ public void testAggregateWrongField() throws IOException { ); } + 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 { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java index 4acae1764953a..4f27cc1e3099e 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java @@ -84,6 +84,30 @@ public void testUnmapped() throws Exception { } } + 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 { int numDocs = scaledRandomIntBetween(64, 256); int numUniqueGeoPoints = randomIntBetween(1, numDocs);