Skip to content

consistently pass FieldTypes in aggregator tests #53250

@andyb-elastic

Description

@andyb-elastic

Found during #42949. This isn't breaking anything currently but is a minor bug in some aggregator tests.

Some tests write multiple fields to the documents in their test index, but don't pass along a MappedFieldType for all fields when searching the index. The fields that don't have a field type passed along can't be read, even if the aggregator was trying to.

This test is supposed to be testing using the missing parameter on unmapped fields, but it's an example of how it could mask a bug when testing it with a mapped field

public void testUnmappedWithMissing() throws Exception {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
document.add(new NumericDocValuesField("unrelated_value", 100));
indexWriter.addDocument(document);
try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType();
fieldType1.setName("unrelated_value");
fieldType1.setHasDocValues(true);
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
ValueType[] valueTypes = new ValueType[]{ValueType.STRING, ValueType.LONG, ValueType.DOUBLE};
String[] fieldNames = new String[]{"string", "long", "double"};
Object[] missingValues = new Object[]{"abc", 19L, 19.2};
for (int i = 0; i < fieldNames.length; i++) {
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name", valueTypes[i])
.field(fieldNames[i]).missing(missingValues[i]);
Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType1);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();
Terms result = (Terms) aggregator.buildAggregation(0L);
assertEquals("_name", result.getName());
assertEquals(1, result.getBuckets().size());
assertEquals(missingValues[i], result.getBuckets().get(0).getKey());
assertEquals(1, result.getBuckets().get(0).getDocCount());
}
}
}
}
}

If you write a value for the fields that aren't supposed to exist, like

document.add(new SortedSetDocValuesField("string", new BytesRef("not abc")));

the test should fail, but passes

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Analytics/AggregationsAggregations>tech debt>testIssues or PRs that are addressing/adding testsTeam:AnalyticsMeta label for analytical engine team (ESQL/Aggs/Geo)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions