Skip to content

Commit

Permalink
Fix a test bug around nested aggregations and field aliases. (#32287)
Browse files Browse the repository at this point in the history
This issue affected both NestedAggregatorTest and ReverseNestedAggregatorTest.
  • Loading branch information
jtibshirani committed Jul 23, 2018
1 parent bba1da6 commit 1b1aa4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ public void testPreGetChildLeafCollectors() throws IOException {

public void testFieldAlias() throws IOException {
int numRootDocs = randomIntBetween(1, 20);
int expectedNestedDocs = 0;

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(
NumberFieldMapper.NumberType.LONG);
Expand All @@ -665,6 +666,7 @@ public void testFieldAlias() throws IOException {
for (int i = 0; i < numRootDocs; i++) {
List<Document> documents = new ArrayList<>();
int numNestedDocs = randomIntBetween(0, 20);
expectedNestedDocs += numNestedDocs;
generateDocuments(documents, numNestedDocs, i, NESTED_OBJECT, VALUE_FIELD_NAME);

Document document = new Document();
Expand All @@ -681,7 +683,6 @@ public void testFieldAlias() throws IOException {
try (IndexReader indexReader = wrap(DirectoryReader.open(directory))) {
NestedAggregationBuilder agg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(
max(MAX_AGG_NAME).field(VALUE_FIELD_NAME));

NestedAggregationBuilder aliasAgg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(
max(MAX_AGG_NAME).field(VALUE_FIELD_NAME + "-alias"));

Expand All @@ -690,8 +691,8 @@ public void testFieldAlias() throws IOException {
Nested aliasNested = search(newSearcher(indexReader, false, true),
new MatchAllDocsQuery(), aliasAgg, fieldType);

assertTrue(nested.getDocCount() > 0);
assertEquals(nested, aliasNested);
assertEquals(expectedNestedDocs, nested.getDocCount());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void testMaxFromParentDocs() throws IOException {

public void testFieldAlias() throws IOException {
int numParentDocs = randomIntBetween(1, 20);
int expectedParentDocs = 0;

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(
NumberFieldMapper.NumberType.LONG);
Expand All @@ -179,6 +180,10 @@ public void testFieldAlias() throws IOException {
for (int i = 0; i < numParentDocs; i++) {
List<Document> documents = new ArrayList<>();
int numNestedDocs = randomIntBetween(0, 20);
if (numNestedDocs > 0) {
expectedParentDocs++;
}

for (int nested = 0; nested < numNestedDocs; nested++) {
Document document = new Document();
document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)),
Expand All @@ -203,7 +208,6 @@ public void testFieldAlias() throws IOException {
}

try (IndexReader indexReader = wrap(DirectoryReader.open(directory))) {

MaxAggregationBuilder maxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
MaxAggregationBuilder aliasMaxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME + "-alias");

Expand All @@ -220,8 +224,8 @@ public void testFieldAlias() throws IOException {
ReverseNested reverseNested = nested.getAggregations().get(REVERSE_AGG_NAME);
ReverseNested aliasReverseNested = aliasNested.getAggregations().get(REVERSE_AGG_NAME);

assertTrue(reverseNested.getDocCount() > 0);
assertEquals(reverseNested, aliasReverseNested);
assertEquals(expectedParentDocs, reverseNested.getDocCount());
}
}
}
Expand Down

0 comments on commit 1b1aa4e

Please sign in to comment.