Skip to content

Commit

Permalink
Tests for agg missing values (elastic#51068)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon authored and SivagurunathanV committed Jan 21, 2020
1 parent df30a81 commit e5fb48d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,23 @@ 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 -> {
iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D)));
});
}

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);
Expand Down Expand Up @@ -189,6 +198,13 @@ public void testBounds() throws IOException {
private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox,
Consumer<InternalGeoGrid<T>> verify,
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();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e5fb48d

Please sign in to comment.