Source:
<code>private void testCase(ValuesSourceAggregationBuilder<?> aggBuilder, Consumer<InternalAggregation> aggAssertion) throws IOException { Query query = new MatchAllDocsQuery(); try (Directory directory = newDirectory()) { try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { Document document = new Document(); for (int i = 0; i < datasetValues.size(); i++) { if (frequently()) { indexWriter.commit(); } long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(datasetTimes.get(i))) .toInstant().toEpochMilli(); document.add(new SortedNumericDocValuesField(DATE_FIELD, instant)); document.add(new NumericDocValuesField(VALUE_FIELD, datasetValues.get(i))); document.add(new SortedSetDocValuesField(TERM_FIELD, new BytesRef(datasetTerms.get(i)))); indexWriter.addDocument(document); document.clear(); } } DateFieldMapper.DateFieldType dateFieldType = new DateFieldMapper.Builder("_name").fieldType(); dateFieldType.setHasDocValues(true); dateFieldType.setName(DATE_FIELD); MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); valueFieldType.setHasDocValues(true); valueFieldType.setName(VALUE_FIELD); MappedFieldType termFieldType = new KeywordFieldMapper.KeywordFieldType(); termFieldType.setName(TERM_FIELD); termFieldType.setHasDocValues(true); try (IndexReader indexReader = DirectoryReader.open(directory)) { <START> IndexSearcher indexSearcher = newSearcher(indexReader, false, true); <END> InternalAggregation internalAggregation = searchAndReduce(indexSearcher, query, aggBuilder, dateFieldType, valueFieldType, termFieldType); aggAssertion.accept(internalAggregation); } } }</code><technical_language>@polyfractal pointed AggregatorTestCase#newIndexSearcher do job. randomization, moving tests to</technical_language>

Target:
private void testCase(ValuesSourceAggregationBuilder<?> aggBuilder, Consumer<InternalAggregation> aggAssertion) throws IOException { Query query = new MatchAllDocsQuery(); try (Directory directory = newDirectory()) { try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { Document document = new Document(); for (int i = 0; i < datasetValues.size(); i++) { if (frequently()) { indexWriter.commit(); } long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(datasetTimes.get(i))) .toInstant().toEpochMilli(); document.add(new SortedNumericDocValuesField(DATE_FIELD, instant)); document.add(new NumericDocValuesField(VALUE_FIELD, datasetValues.get(i))); document.add(new SortedSetDocValuesField(TERM_FIELD, new BytesRef(datasetTerms.get(i)))); indexWriter.addDocument(document); document.clear(); } } DateFieldMapper.DateFieldType dateFieldType = new DateFieldMapper.Builder("_name").fieldType(); dateFieldType.setHasDocValues(true); dateFieldType.setName(DATE_FIELD); MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); valueFieldType.setHasDocValues(true); valueFieldType.setName(VALUE_FIELD); MappedFieldType termFieldType = new KeywordFieldMapper.KeywordFieldType(); termFieldType.setName(TERM_FIELD); termFieldType.setHasDocValues(true); try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); InternalAggregation internalAggregation = searchAndReduce(indexSearcher, query, aggBuilder, dateFieldType, valueFieldType, termFieldType); aggAssertion.accept(internalAggregation); } } }