[7.x] Modified searchAndReduce() to return empty agg when no docs exist (#55967)

Backports #55826 to 7.x

    Modified AggregatorTestCase.searchAndReduce() method so that it returns an empty aggregation result when no documents have been inserted.

    Also refactored several aggregation tests so they do not re-implement method AggregatorTestCase.testCase()

    Fixes #55824
This commit is contained in:
Christos Soulios 2020-04-30 00:28:32 +03:00 committed by GitHub
parent bf0204ba06
commit 43dab77186
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 235 additions and 390 deletions

View File

@ -47,7 +47,6 @@ import org.elasticsearch.search.aggregations.pipeline.DerivativePipelineAggregat
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Assert;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
@ -205,7 +204,10 @@ public class AutoDateHistogramAggregatorTests extends AggregatorTestCase {
} }
); );
testSearchAndReduceCase(DEFAULT_QUERY, dates, aggregation, testSearchAndReduceCase(DEFAULT_QUERY, dates, aggregation,
Assert::assertNull histogram -> {
assertEquals(0, histogram.getBuckets().size());
assertFalse(AggregationInspectionHelper.hasValue(histogram));
}
); );
} }
@ -218,20 +220,12 @@ public class AutoDateHistogramAggregatorTests extends AggregatorTestCase {
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
fieldType.setName("date_field"); fieldType.setName("date_field");
testCase( testCase(aggregation, DEFAULT_QUERY,
aggregation,
DEFAULT_QUERY,
iw -> {}, iw -> {},
(Consumer<InternalAutoDateHistogram>) histogram -> { (Consumer<InternalAutoDateHistogram>) histogram -> {
// TODO: searchAndReduce incorrectly returns null for empty aggs
assertNull(histogram);
/*
assertEquals(0, histogram.getBuckets().size()); assertEquals(0, histogram.getBuckets().size());
assertFalse(AggregationInspectionHelper.hasValue(histogram)); assertFalse(AggregationInspectionHelper.hasValue(histogram));
*/ }, fieldType);
},
fieldType
);
} }
public void testUnmappedMissing() throws IOException { public void testUnmappedMissing() throws IOException {
@ -243,23 +237,14 @@ public class AutoDateHistogramAggregatorTests extends AggregatorTestCase {
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
fieldType.setName("date_field"); fieldType.setName("date_field");
testCase( testCase(aggregation, DEFAULT_QUERY,
aggregation,
DEFAULT_QUERY,
iw -> {}, iw -> {},
(Consumer<InternalAutoDateHistogram>) histogram -> { (Consumer<InternalAutoDateHistogram>) histogram -> {
// TODO: searchAndReduce incorrectly returns null for empty aggs assertEquals(0, histogram.getBuckets().size());
assertNull(histogram); assertFalse(AggregationInspectionHelper.hasValue(histogram));
/* }, fieldType);
assertEquals(1, histogram.getBuckets().size());
assertTrue(AggregationInspectionHelper.hasValue(histogram));
*/
},
fieldType
);
} }
public void testIntervalYear() throws IOException { public void testIntervalYear() throws IOException {
final long start = LocalDate.of(2015, 1, 1).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); final long start = LocalDate.of(2015, 1, 1).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();
final long end = LocalDate.of(2017, 12, 31).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); final long end = LocalDate.of(2017, 12, 31).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();

View File

@ -155,18 +155,17 @@ public class DateHistogramAggregatorTests extends AggregatorTestCase {
public void testNoDocsDeprecatedInterval() throws IOException { public void testNoDocsDeprecatedInterval() throws IOException {
Query query = new MatchNoDocsQuery(); Query query = new MatchNoDocsQuery();
List<String> dates = Collections.emptyList(); List<String> dates = Collections.emptyList();
Consumer<DateHistogramAggregationBuilder> aggregation = agg -> Consumer<DateHistogramAggregationBuilder> aggregation =
agg.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD); agg -> agg.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD);
testSearchCase(query, dates, aggregation, testSearchCase(query, dates, aggregation, histogram -> {
histogram -> { assertEquals(0, histogram.getBuckets().size());
assertEquals(0, histogram.getBuckets().size()); assertFalse(AggregationInspectionHelper.hasValue(histogram));
assertFalse(AggregationInspectionHelper.hasValue(histogram)); }, false);
}, false testSearchAndReduceCase(query, dates, aggregation, histogram -> {
); assertEquals(0, histogram.getBuckets().size());
testSearchAndReduceCase(query, dates, aggregation, assertFalse(AggregationInspectionHelper.hasValue(histogram));
histogram -> assertNull(histogram), false }, false);
);
assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future."); assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future.");
} }
@ -179,7 +178,7 @@ public class DateHistogramAggregatorTests extends AggregatorTestCase {
histogram -> assertEquals(0, histogram.getBuckets().size()), false histogram -> assertEquals(0, histogram.getBuckets().size()), false
); );
testSearchAndReduceCase(query, dates, aggregation, testSearchAndReduceCase(query, dates, aggregation,
histogram -> assertNull(histogram), false histogram -> assertEquals(0, histogram.getBuckets().size()), false
); );
aggregation = agg -> aggregation = agg ->
@ -188,7 +187,7 @@ public class DateHistogramAggregatorTests extends AggregatorTestCase {
histogram -> assertEquals(0, histogram.getBuckets().size()), false histogram -> assertEquals(0, histogram.getBuckets().size()), false
); );
testSearchAndReduceCase(query, dates, aggregation, testSearchAndReduceCase(query, dates, aggregation,
histogram -> assertNull(histogram), false histogram -> assertEquals(0, histogram.getBuckets().size()), false
); );
} }

View File

@ -69,7 +69,6 @@ import org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder;
import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder; import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.junit.Assert;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -239,15 +238,13 @@ public class RareTermsAggregatorTests extends AggregatorTestCase {
agg -> assertEquals(0, agg.getBuckets().size()), ValueType.STRING agg -> assertEquals(0, agg.getBuckets().size()), ValueType.STRING
); );
// Note: the search and reduce test will generate no segments (due to no docs)
// and so will return a null agg because the aggs aren't run/reduced
testSearchAndReduceCase(query, Collections.emptyList(), testSearchAndReduceCase(query, Collections.emptyList(),
aggregation -> aggregation.field(LONG_FIELD).maxDocCount(1), aggregation -> aggregation.field(LONG_FIELD).maxDocCount(1),
Assert::assertNull, ValueType.NUMERIC agg -> assertEquals(0, agg.getBuckets().size()), ValueType.NUMERIC
); );
testSearchAndReduceCase(query, Collections.emptyList(), testSearchAndReduceCase(query, Collections.emptyList(),
aggregation -> aggregation.field(KEYWORD_FIELD).maxDocCount(1), aggregation -> aggregation.field(KEYWORD_FIELD).maxDocCount(1),
Assert::assertNull, ValueType.STRING agg -> assertEquals(0, agg.getBuckets().size()), ValueType.STRING
); );
} }

View File

@ -133,7 +133,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, avg -> { }, avg -> {
assertEquals(Double.NaN, avg.getValue(), 0); assertEquals(Double.NaN, avg.getValue(), 0);
@ -142,7 +142,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 3))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 3)));
}, avg -> { }, avg -> {
@ -152,7 +152,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesSortedNumericDocValues() throws IOException { public void testSomeMatchesSortedNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 2))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 2)));
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 3))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 3)));
@ -163,7 +163,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesNumericDocValues() throws IOException { public void testSomeMatchesNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 2))); iw.addDocument(singleton(new NumericDocValuesField("number", 2)));
iw.addDocument(singleton(new NumericDocValuesField("number", 3))); iw.addDocument(singleton(new NumericDocValuesField("number", 3)));
@ -174,7 +174,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(IntPoint.newRangeQuery("number", 0, 3), iw -> { testAggregation(IntPoint.newRangeQuery("number", 0, 3), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2))); iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 3))); iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 3)));
@ -185,7 +185,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltersAll() throws IOException { public void testQueryFiltersAll() throws IOException {
testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> { testAggregation(IntPoint.newRangeQuery("number", -1, 0), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2))); iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 7)));
@ -228,31 +228,31 @@ public class AvgAggregatorTests extends AggregatorTestCase {
public void testUnmappedField() throws IOException { public void testUnmappedField() throws IOException {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, avg -> { }, avg -> {
assertEquals(Double.NaN, avg.getValue(), 0); assertEquals(Double.NaN, avg.getValue(), 0);
assertFalse(AggregationInspectionHelper.hasValue(avg)); assertFalse(AggregationInspectionHelper.hasValue(avg));
}, null); }, (MappedFieldType) null);
} }
public void testUnmappedWithMissingField() throws IOException { public void testUnmappedWithMissingField() throws IOException {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number").missing(0L); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number").missing(0L);
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, avg -> { }, avg -> {
assertEquals(0.0, avg.getValue(), 0); assertEquals(0.0, avg.getValue(), 0);
assertTrue(AggregationInspectionHelper.hasValue(avg)); assertTrue(AggregationInspectionHelper.hasValue(avg));
}, null); }, (MappedFieldType) null);
} }
private void verifyAvgOfDoubles(double[] values, double expected, double delta) throws IOException { private void verifyAvgOfDoubles(double[] values, double expected, double delta) throws IOException {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
fieldType.setName("number"); fieldType.setName("number");
testCase(aggregationBuilder, new MatchAllDocsQuery(), testAggregation(aggregationBuilder, new MatchAllDocsQuery(),
iw -> { iw -> {
for (double value : values) { for (double value : values) {
iw.addDocument(singleton(new NumericDocValuesField("number", NumericUtils.doubleToSortableLong(value)))); iw.addDocument(singleton(new NumericDocValuesField("number", NumericUtils.doubleToSortableLong(value))));
@ -302,7 +302,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testSingleValuedField() throws IOException { public void testSingleValuedField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 2))); iw.addDocument(singleton(new NumericDocValuesField("number", 2)));
iw.addDocument(singleton(new NumericDocValuesField("number", 3))); iw.addDocument(singleton(new NumericDocValuesField("number", 3)));
@ -322,7 +322,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
.field("value") .field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -342,7 +342,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
.field("value") .field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -360,7 +360,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -382,7 +382,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -394,7 +394,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
} }
public void testMultiValuedField() throws IOException { public void testMultiValuedField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -415,7 +415,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -440,7 +440,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -463,7 +463,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
.field("value") .field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -483,7 +483,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
.field("values") .field("values")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -505,7 +505,7 @@ public class AvgAggregatorTests extends AggregatorTestCase {
.field("values") .field("values")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -571,34 +571,22 @@ public class AvgAggregatorTests extends AggregatorTestCase {
directory.close(); directory.close();
} }
private void testCase(Query query, private void testAggregation(Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalAvg> verify) throws IOException { Consumer<InternalAvg> verify) throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("number"); fieldType.setName("number");
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
testCase(aggregationBuilder, query, buildIndex, verify, fieldType); testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType);
} }
private void testCase(AvgAggregationBuilder aggregationBuilder, Query query, private void testAggregation(
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, AggregationBuilder aggregationBuilder,
Consumer<InternalAvg> verify, MappedFieldType fieldType) throws IOException { Query query,
Directory directory = newDirectory(); CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); Consumer<InternalAvg> verify,
buildIndex.accept(indexWriter); MappedFieldType fieldType) throws IOException {
indexWriter.close(); testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalAvg) aggregator.buildAggregation(0L));
indexReader.close();
directory.close();
} }
/** /**

View File

@ -23,20 +23,17 @@ import org.apache.lucene.document.BinaryDocValuesField;
import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.mapper.RangeFieldMapper; import org.elasticsearch.index.mapper.RangeFieldMapper;
import org.elasticsearch.index.mapper.RangeType; import org.elasticsearch.index.mapper.RangeType;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
@ -49,8 +46,9 @@ import java.util.function.Consumer;
import static java.util.Collections.singleton; import static java.util.Collections.singleton;
public class CardinalityAggregatorTests extends AggregatorTestCase { public class CardinalityAggregatorTests extends AggregatorTestCase {
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, card -> { }, card -> {
assertEquals(0.0, card.getValue(), 0); assertEquals(0.0, card.getValue(), 0);
@ -69,7 +67,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2); Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2);
multiRecord.add(range1); multiRecord.add(range1);
multiRecord.add(range2); multiRecord.add(range2);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2)))));
@ -81,7 +79,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
}, card -> { }, card -> {
@ -91,7 +89,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesSortedNumericDocValues() throws IOException { public void testSomeMatchesSortedNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 1))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 1)));
}, card -> { }, card -> {
@ -101,7 +99,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesNumericDocValues() throws IOException { public void testSomeMatchesNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, card -> { }, card -> {
@ -111,7 +109,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(IntPoint.newRangeQuery("number", 0, 5), iw -> { testAggregation(IntPoint.newRangeQuery("number", 0, 5), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), iw.addDocument(Arrays.asList(new IntPoint("number", 7),
new SortedNumericDocValuesField("number", 7))); new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), iw.addDocument(Arrays.asList(new IntPoint("number", 1),
@ -123,7 +121,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltersAll() throws IOException { public void testQueryFiltersAll() throws IOException {
testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> { testAggregation(IntPoint.newRangeQuery("number", -1, 0), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), iw.addDocument(Arrays.asList(new IntPoint("number", 7),
new SortedNumericDocValuesField("number", 7))); new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), iw.addDocument(Arrays.asList(new IntPoint("number", 1),
@ -138,7 +136,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name") CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name")
.field("number").missing("🍌🍌🍌"); .field("number").missing("🍌🍌🍌");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -152,7 +150,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name") CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name")
.field("number").missing(1234); .field("number").missing(1234);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -166,7 +164,7 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name") CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("name")
.field("number").missing(new GeoPoint(42.39561, -71.13051)); .field("number").missing(new GeoPoint(42.39561, -71.13051));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -176,34 +174,18 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
}, null); }, null);
} }
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, private void testAggregation(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalCardinality> verify) throws IOException { Consumer<InternalCardinality> verify) throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(
NumberFieldMapper.NumberType.LONG); NumberFieldMapper.NumberType.LONG);
fieldType.setName("number"); fieldType.setName("number");
final CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("_name").field("number"); final CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("_name").field("number");
testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType);
}
private void testAggregation(AggregationBuilder aggregationBuilder, Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalCardinality> verify,
MappedFieldType fieldType) throws IOException {
testCase(aggregationBuilder, query, buildIndex, verify, fieldType); testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
} }
private void testCase(CardinalityAggregationBuilder aggregationBuilder, Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalCardinality> verify,
MappedFieldType fieldType) throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
CardinalityAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher,
fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalCardinality) aggregator.buildAggregation(0L));
indexReader.close();
directory.close();
}
} }

View File

@ -176,7 +176,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, max -> { }, max -> {
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0); assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0);
@ -185,7 +185,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
}, max -> { }, max -> {
@ -195,7 +195,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesSortedNumericDocValues() throws IOException { public void testSomeMatchesSortedNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 1))); iw.addDocument(singleton(new SortedNumericDocValuesField("number", 1)));
}, max -> { }, max -> {
@ -205,7 +205,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesNumericDocValues() throws IOException { public void testSomeMatchesNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, max -> { }, max -> {
@ -215,7 +215,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(IntPoint.newRangeQuery("number", 0, 5), iw -> { testAggregation(IntPoint.newRangeQuery("number", 0, 5), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1))); iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1)));
}, max -> { }, max -> {
@ -225,7 +225,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltersAll() throws IOException { public void testQueryFiltersAll() throws IOException {
testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> { testAggregation(IntPoint.newRangeQuery("number", -1, 0), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1))); iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1)));
}, max -> { }, max -> {
@ -236,34 +236,33 @@ public class MaxAggregatorTests extends AggregatorTestCase {
public void testUnmappedField() throws IOException { public void testUnmappedField() throws IOException {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number"); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number");
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, max -> { }, max -> {
assertEquals(max.getValue(), Double.NEGATIVE_INFINITY, 0); assertEquals(max.getValue(), Double.NEGATIVE_INFINITY, 0);
assertFalse(AggregationInspectionHelper.hasValue(max)); assertFalse(AggregationInspectionHelper.hasValue(max));
}, null); }, (MappedFieldType) null);
} }
public void testUnmappedWithMissingField() throws IOException { public void testUnmappedWithMissingField() throws IOException {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L);
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, max -> { }, max -> {
assertEquals(max.getValue(), 19.0, 0); assertEquals(max.getValue(), 19.0, 0);
assertTrue(AggregationInspectionHelper.hasValue(max)); assertTrue(AggregationInspectionHelper.hasValue(max));
}, null); }, (MappedFieldType) null);
} }
public void testMissingFieldOptimization() throws IOException { public void testMissingFieldOptimization() throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("number"); fieldType.setName("number");
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L); AggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L);
testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1))); iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1)));
iw.addDocument(emptyList()); iw.addDocument(emptyList());
@ -277,11 +276,11 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("number"); fieldType.setName("number");
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name") AggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name")
.field("number") .field("number")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap()));
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7))); iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1))); iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, max -> { }, max -> {
@ -290,33 +289,19 @@ public class MaxAggregatorTests extends AggregatorTestCase {
}, fieldType); }, fieldType);
} }
private void testCase(Query query, private void testAggregation(Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalMax> verify) throws IOException { Consumer<InternalMax> verify) throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
fieldType.setName("number"); fieldType.setName("number");
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number"); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number");
testCase(aggregationBuilder, query, buildIndex, verify, fieldType); testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType);
} }
private void testCase(MaxAggregationBuilder aggregationBuilder, Query query, private void testAggregation(AggregationBuilder aggregationBuilder, Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalMax> verify,
Consumer<InternalMax> verify, MappedFieldType fieldType) throws IOException { MappedFieldType fieldType) throws IOException {
Directory directory = newDirectory(); testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
MaxAggregator aggregator = createAggregator(query, aggregationBuilder, indexSearcher, createIndexSettings(), fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalMax) aggregator.buildAggregation(0L));
indexReader.close();
directory.close();
} }
public void testMaxShortcutRandom() throws Exception { public void testMaxShortcutRandom() throws Exception {
@ -426,7 +411,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testSingleValuedField() throws IOException { public void testSingleValuedField() throws IOException {
testCase( new MatchAllDocsQuery(), iw -> { testAggregation( new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("number", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("number", i + 1)));
@ -445,7 +430,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
.format("0000.0") .format("0000.0")
.field("value"); .field("value");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -547,7 +532,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
.field("value") .field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -568,7 +553,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
.field("value") .field("value")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -581,7 +566,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
} }
public void testMultiValuedField() throws IOException { public void testMultiValuedField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -603,7 +588,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
.field("values") .field("values")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -626,7 +611,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
.field("values") .field("values")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -647,7 +632,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -669,7 +654,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1))); iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
@ -687,7 +672,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();
@ -712,7 +697,7 @@ public class MaxAggregatorTests extends AggregatorTestCase {
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
final int numDocs = 10; final int numDocs = 10;
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document document = new Document(); Document document = new Document();

View File

@ -22,15 +22,11 @@ package org.elasticsearch.search.aggregations.metrics;
import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
@ -42,7 +38,6 @@ import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
@ -92,14 +87,14 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
// intentionally not writing any docs // intentionally not writing any docs
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
testCase(new MatchAllDocsQuery(), writer -> {}, agg -> { testAggregation(new MatchAllDocsQuery(), writer -> {}, agg -> {
assertThat(agg.getMedianAbsoluteDeviation(), equalTo(Double.NaN)); assertThat(agg.getMedianAbsoluteDeviation(), equalTo(Double.NaN));
assertFalse(AggregationInspectionHelper.hasValue(agg)); assertFalse(AggregationInspectionHelper.hasValue(agg));
}); });
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase( testAggregation(
new MatchAllDocsQuery(), new MatchAllDocsQuery(),
writer -> { writer -> {
writer.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1))); writer.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
@ -115,7 +110,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
public void testSomeMatchesSortedNumericDocValues() throws IOException { public void testSomeMatchesSortedNumericDocValues() throws IOException {
final int size = randomIntBetween(100, 1000); final int size = randomIntBetween(100, 1000);
final List<Long> sample = new ArrayList<>(size); final List<Long> sample = new ArrayList<>(size);
testCase( testAggregation(
new DocValuesFieldExistsQuery(FIELD_NAME), new DocValuesFieldExistsQuery(FIELD_NAME),
randomSample(size, point -> { randomSample(size, point -> {
sample.add(point); sample.add(point);
@ -131,7 +126,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
public void testSomeMatchesNumericDocValues() throws IOException { public void testSomeMatchesNumericDocValues() throws IOException {
final int size = randomIntBetween(100, 1000); final int size = randomIntBetween(100, 1000);
final List<Long> sample = new ArrayList<>(size); final List<Long> sample = new ArrayList<>(size);
testCase( testAggregation(
new DocValuesFieldExistsQuery(FIELD_NAME), new DocValuesFieldExistsQuery(FIELD_NAME),
randomSample(size, point -> { randomSample(size, point -> {
sample.add(point); sample.add(point);
@ -149,7 +144,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
final int upperRange = 500; final int upperRange = 500;
final int[] sample = IntStream.rangeClosed(1, 1000).toArray(); final int[] sample = IntStream.rangeClosed(1, 1000).toArray();
final int[] filteredSample = Arrays.stream(sample).filter(point -> point >= lowerRange && point <= upperRange).toArray(); final int[] filteredSample = Arrays.stream(sample).filter(point -> point >= lowerRange && point <= upperRange).toArray();
testCase( testAggregation(
IntPoint.newRangeQuery(FIELD_NAME, lowerRange, upperRange), IntPoint.newRangeQuery(FIELD_NAME, lowerRange, upperRange),
writer -> { writer -> {
for (int point : sample) { for (int point : sample) {
@ -164,7 +159,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltersAll() throws IOException { public void testQueryFiltersAll() throws IOException {
testCase( testAggregation(
IntPoint.newRangeQuery(FIELD_NAME, -1, 0), IntPoint.newRangeQuery(FIELD_NAME, -1, 0),
writer -> { writer -> {
writer.addDocument(Arrays.asList(new IntPoint(FIELD_NAME, 1), new SortedNumericDocValuesField(FIELD_NAME, 1))); writer.addDocument(Arrays.asList(new IntPoint(FIELD_NAME, 1), new SortedNumericDocValuesField(FIELD_NAME, 1)));
@ -181,13 +176,13 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
MedianAbsoluteDeviationAggregationBuilder aggregationBuilder = new MedianAbsoluteDeviationAggregationBuilder("foo") MedianAbsoluteDeviationAggregationBuilder aggregationBuilder = new MedianAbsoluteDeviationAggregationBuilder("foo")
.field(FIELD_NAME); .field(FIELD_NAME);
testCase(aggregationBuilder, new DocValuesFieldExistsQuery(FIELD_NAME), iw -> { testAggregation(aggregationBuilder, new DocValuesFieldExistsQuery(FIELD_NAME), iw -> {
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1)));
}, agg -> { }, agg -> {
assertEquals(Double.NaN, agg.getMedianAbsoluteDeviation(),0); assertEquals(Double.NaN, agg.getMedianAbsoluteDeviation(),0);
assertFalse(AggregationInspectionHelper.hasValue(agg)); assertFalse(AggregationInspectionHelper.hasValue(agg));
}, null); }, null);
} }
public void testUnmappedMissing() throws IOException { public void testUnmappedMissing() throws IOException {
@ -195,7 +190,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
.field(FIELD_NAME) .field(FIELD_NAME)
.missing(1234); .missing(1234);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -216,7 +211,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
final int size = randomIntBetween(100, 1000); final int size = randomIntBetween(100, 1000);
final List<Long> sample = new ArrayList<>(size); final List<Long> sample = new ArrayList<>(size);
testCase(aggregationBuilder, testAggregation(aggregationBuilder,
new MatchAllDocsQuery(), new MatchAllDocsQuery(),
randomSample(size, point -> { randomSample(size, point -> {
sample.add(point); sample.add(point);
@ -237,7 +232,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
final int size = randomIntBetween(100, 1000); final int size = randomIntBetween(100, 1000);
final List<Long> sample = new ArrayList<>(size); final List<Long> sample = new ArrayList<>(size);
testCase(aggregationBuilder, testAggregation(aggregationBuilder,
new MatchAllDocsQuery(), new MatchAllDocsQuery(),
iw -> { iw -> {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -250,7 +245,7 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
}, fieldType); }, fieldType);
} }
private void testCase(Query query, private void testAggregation(Query query,
CheckedConsumer<RandomIndexWriter, CheckedConsumer<RandomIndexWriter,
IOException> buildIndex, IOException> buildIndex,
Consumer<InternalMedianAbsoluteDeviation> verify) throws IOException { Consumer<InternalMedianAbsoluteDeviation> verify) throws IOException {
@ -261,26 +256,13 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
testCase(builder, query, buildIndex, verify, fieldType); testAggregation(builder, query, buildIndex, verify, fieldType);
} }
private void testCase(MedianAbsoluteDeviationAggregationBuilder aggregationBuilder, Query query, private void testAggregation(AggregationBuilder aggregationBuilder, Query query,
CheckedConsumer<RandomIndexWriter, IOException> indexer, CheckedConsumer<RandomIndexWriter, IOException> indexer,
Consumer<InternalMedianAbsoluteDeviation> verify, MappedFieldType fieldType) throws IOException { Consumer<InternalMedianAbsoluteDeviation> verify, MappedFieldType fieldType) throws IOException {
try (Directory directory = newDirectory()) { testCase(aggregationBuilder, query, indexer, verify, fieldType);
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
indexer.accept(indexWriter);
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalMedianAbsoluteDeviation) aggregator.buildAggregation(0L));
}
}
} }
public static class IsCloseToRelative extends TypeSafeMatcher<Double> { public static class IsCloseToRelative extends TypeSafeMatcher<Double> {

View File

@ -59,7 +59,6 @@ import org.elasticsearch.search.lookup.LeafDocLookup;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -80,7 +79,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
private static final String FIELD_SCRIPT_NAME = "field_script"; private static final String FIELD_SCRIPT_NAME = "field_script";
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, count -> { }, count -> {
assertEquals(0L, count.getValue(), 0d); assertEquals(0L, count.getValue(), 0d);
@ -89,7 +88,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new NumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("wrong_number", 1))); iw.addDocument(singleton(new NumericDocValuesField("wrong_number", 1)));
}, count -> { }, count -> {
@ -99,7 +98,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
public void testNumericDocValues() throws IOException { public void testNumericDocValues() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 2))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 2)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1)));
@ -123,7 +122,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
public void testSortedNumericDocValues() throws IOException { public void testSortedNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery(FIELD_NAME), iw -> { testAggregation(new DocValuesFieldExistsQuery(FIELD_NAME), iw -> {
iw.addDocument(Arrays.asList(new SortedNumericDocValuesField(FIELD_NAME, 3), iw.addDocument(Arrays.asList(new SortedNumericDocValuesField(FIELD_NAME, 3),
new SortedNumericDocValuesField(FIELD_NAME, 4))); new SortedNumericDocValuesField(FIELD_NAME, 4)));
iw.addDocument(Arrays.asList(new SortedNumericDocValuesField(FIELD_NAME, 3), iw.addDocument(Arrays.asList(new SortedNumericDocValuesField(FIELD_NAME, 3),
@ -136,7 +135,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(new TermQuery(new Term("match", "yes")), iw -> { testAggregation(new TermQuery(new Term("match", "yes")), iw -> {
iw.addDocument(Arrays.asList(new StringField("match", "yes", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(Arrays.asList(new StringField("match", "yes", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 1)));
iw.addDocument(Arrays.asList(new StringField("match", "no", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 2))); iw.addDocument(Arrays.asList(new StringField("match", "no", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 2)));
iw.addDocument(Arrays.asList(new StringField("match", "yes", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 3))); iw.addDocument(Arrays.asList(new StringField("match", "yes", Field.Store.NO), new NumericDocValuesField(FIELD_NAME, 3)));
@ -150,7 +149,7 @@ public class SumAggregatorTests extends AggregatorTestCase {
public void testStringField() throws IOException { public void testStringField() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> { IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("1")))); iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("1"))));
}, count -> { }, count -> {
assertEquals(0L, count.getValue(), 0d); assertEquals(0L, count.getValue(), 0d);
@ -193,15 +192,16 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
private void verifySummationOfDoubles(double[] values, double expected, double delta) throws IOException { private void verifySummationOfDoubles(double[] values, double expected, double delta) throws IOException {
testCase(new MatchAllDocsQuery(), testAggregation(
sum("_name").field(FIELD_NAME), sum("_name").field(FIELD_NAME),
new MatchAllDocsQuery(),
iw -> { iw -> {
for (double value : values) { for (double value : values) {
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, NumericUtils.doubleToSortableLong(value)))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, NumericUtils.doubleToSortableLong(value))));
} }
}, },
result -> assertEquals(expected, result.getValue(), delta), result -> assertEquals(expected, result.getValue(), delta),
singleton(defaultFieldType(NumberType.DOUBLE)) defaultFieldType(NumberType.DOUBLE)
); );
} }
@ -321,16 +321,14 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
final long finalSum = sum; final long finalSum = sum;
testCase(new MatchAllDocsQuery(), testAggregation(
sum("_name") sum("_name").field(aggField.name()).missing(missingValue),
.field(aggField.name()) new MatchAllDocsQuery(),
.missing(missingValue),
writer -> writer.addDocuments(docs), writer -> writer.addDocuments(docs),
internalSum -> { internalSum -> {
assertEquals(finalSum, internalSum.getValue(), 0d); assertEquals(finalSum, internalSum.getValue(), 0d);
assertTrue(AggregationInspectionHelper.hasValue(internalSum)); assertTrue(AggregationInspectionHelper.hasValue(internalSum));
}, }, aggField, irrelevantField
org.elasticsearch.common.collect.List.of(aggField, irrelevantField)
); );
} }
@ -367,38 +365,26 @@ public class SumAggregatorTests extends AggregatorTestCase {
} }
final long finalSum = sum; final long finalSum = sum;
testCase(new MatchAllDocsQuery(), testAggregation(
builder, builder,
new MatchAllDocsQuery(),
writer -> writer.addDocuments(docs), writer -> writer.addDocuments(docs),
internalSum -> verify.apply(finalSum, docs, internalSum), internalSum -> verify.apply(finalSum, docs, internalSum),
singleton(fieldType) fieldType
); );
} }
private void testCase(Query query, private void testAggregation(Query query,
CheckedConsumer<RandomIndexWriter, IOException> indexer, CheckedConsumer<RandomIndexWriter, IOException> indexer,
Consumer<InternalSum> verify) throws IOException { Consumer<InternalSum> verify) throws IOException {
testCase(query, sum("_name").field(FIELD_NAME), indexer, verify, singleton(defaultFieldType())); AggregationBuilder aggregationBuilder = sum("_name").field(FIELD_NAME);
testAggregation( aggregationBuilder, query, indexer, verify, defaultFieldType());
} }
private void testCase(Query query, private void testAggregation(AggregationBuilder aggregationBuilder, Query query,
SumAggregationBuilder aggregationBuilder, CheckedConsumer<RandomIndexWriter, IOException> indexer,
CheckedConsumer<RandomIndexWriter, IOException> indexer, Consumer<InternalSum> verify, MappedFieldType... fieldTypes) throws IOException {
Consumer<InternalSum> verify, testCase(aggregationBuilder, query, indexer, verify, fieldTypes);
Collection<MappedFieldType> fieldTypes) throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
indexer.accept(indexWriter);
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
final MappedFieldType[] fieldTypesArray = fieldTypes.toArray(new MappedFieldType[0]);
final InternalSum internalSum = search(indexSearcher, query, aggregationBuilder, fieldTypesArray);
verify.accept(internalSum);
}
}
} }
@Override @Override

View File

@ -28,14 +28,10 @@ import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
@ -119,7 +115,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
public void testGeoField() throws IOException { public void testGeoField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.GEOPOINT, iw -> { testAggregation(new MatchAllDocsQuery(), ValueType.GEOPOINT, iw -> {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
Document document = new Document(); Document document = new Document();
document.add(new LatLonDocValuesField("field", 10, 10)); document.add(new LatLonDocValuesField("field", 10, 10));
@ -129,7 +125,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testDoubleField() throws IOException { public void testDoubleField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.DOUBLE, iw -> { testAggregation(new MatchAllDocsQuery(), ValueType.DOUBLE, iw -> {
for (int i = 0; i < 15; i++) { for (int i = 0; i < 15; i++) {
Document document = new Document(); Document document = new Document();
document.add(new DoubleDocValuesField(FIELD_NAME, 23D)); document.add(new DoubleDocValuesField(FIELD_NAME, 23D));
@ -139,7 +135,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testKeyWordField() throws IOException { public void testKeyWordField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.STRING, iw -> { testAggregation(new MatchAllDocsQuery(), ValueType.STRING, iw -> {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
Document document = new Document(); Document document = new Document();
document.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("stringValue"))); document.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("stringValue")));
@ -151,7 +147,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
for (ValueType valueType : ValueType.values()) { for (ValueType valueType : ValueType.values()) {
testCase(new MatchAllDocsQuery(), valueType, iw -> { testAggregation(new MatchAllDocsQuery(), valueType, iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, count -> { }, count -> {
assertEquals(0L, count.getValue()); assertEquals(0L, count.getValue());
@ -161,7 +157,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), ValueType.LONG, iw -> { testAggregation(new MatchAllDocsQuery(), ValueType.LONG, iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1)));
}, count -> { }, count -> {
@ -171,7 +167,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesSortedNumericDocValues() throws IOException { public void testSomeMatchesSortedNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery(FIELD_NAME), ValueType.NUMERIC, iw -> { testAggregation(new DocValuesFieldExistsQuery(FIELD_NAME), ValueType.NUMERIC, iw -> {
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 7))); iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 7)));
iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 1)));
@ -182,7 +178,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testSomeMatchesNumericDocValues() throws IOException { public void testSomeMatchesNumericDocValues() throws IOException {
testCase(new DocValuesFieldExistsQuery(FIELD_NAME), ValueType.NUMBER, iw -> { testAggregation(new DocValuesFieldExistsQuery(FIELD_NAME), ValueType.NUMBER, iw -> {
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1)));
}, count -> { }, count -> {
@ -192,7 +188,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(IntPoint.newRangeQuery("level", 0, 5), ValueType.STRING, iw -> { testAggregation(IntPoint.newRangeQuery("level", 0, 5), ValueType.STRING, iw -> {
iw.addDocument(Arrays.asList(new IntPoint("level", 0), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); iw.addDocument(Arrays.asList(new IntPoint("level", 0), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo"))));
iw.addDocument(Arrays.asList(new IntPoint("level", 1), new SortedDocValuesField(FIELD_NAME, new BytesRef("bar")))); iw.addDocument(Arrays.asList(new IntPoint("level", 1), new SortedDocValuesField(FIELD_NAME, new BytesRef("bar"))));
iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo"))));
@ -205,7 +201,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltersAll() throws IOException { public void testQueryFiltersAll() throws IOException {
testCase(IntPoint.newRangeQuery("level", -1, 0), ValueType.STRING, iw -> { testAggregation(IntPoint.newRangeQuery("level", -1, 0), ValueType.STRING, iw -> {
iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo"))));
iw.addDocument(Arrays.asList(new IntPoint("level", 5), new SortedDocValuesField(FIELD_NAME, new BytesRef("baz")))); iw.addDocument(Arrays.asList(new IntPoint("level", 5), new SortedDocValuesField(FIELD_NAME, new BytesRef("baz"))));
}, count -> { }, count -> {
@ -218,7 +214,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name") ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name")
.field("number").missing("🍌🍌🍌"); .field("number").missing("🍌🍌🍌");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -232,7 +228,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name") ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name")
.field("number").missing(1234); .field("number").missing(1234);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
@ -246,14 +242,14 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name") ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name")
.field("number").missing(new GeoPoint(42.39561, -71.13051)); .field("number").missing(new GeoPoint(42.39561, -71.13051));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 7)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 8)));
iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9))); iw.addDocument(singleton(new NumericDocValuesField("unrelatedField", 9)));
}, valueCount -> { }, valueCount -> {
assertEquals(3, valueCount.getValue(), 0); assertEquals(3, valueCount.getValue(), 0);
assertTrue(AggregationInspectionHelper.hasValue(valueCount)); assertTrue(AggregationInspectionHelper.hasValue(valueCount));
}, null); }, null);
} }
public void testRangeFieldValues() throws IOException { public void testRangeFieldValues() throws IOException {
@ -267,7 +263,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2); Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2);
multiRecord.add(range1); multiRecord.add(range1);
multiRecord.add(range2); multiRecord.add(range2);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2))))); iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2)))));
@ -287,7 +283,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 8))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 8)));
iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 9))); iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 9)));
@ -305,7 +301,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
Document doc = new Document(); Document doc = new Document();
doc.add(new SortedNumericDocValuesField(FIELD_NAME, 7)); doc.add(new SortedNumericDocValuesField(FIELD_NAME, 7));
doc.add(new SortedNumericDocValuesField(FIELD_NAME, 7)); doc.add(new SortedNumericDocValuesField(FIELD_NAME, 7));
@ -337,7 +333,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("1")))); iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("1"))));
iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("2")))); iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("2"))));
iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("3")))); iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("3"))));
@ -355,7 +351,7 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
fieldType.setName(FIELD_NAME); fieldType.setName(FIELD_NAME);
fieldType.setHasDocValues(true); fieldType.setHasDocValues(true);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
Document doc = new Document(); Document doc = new Document();
// Note: unlike numerics, lucene de-dupes strings so we increment here // Note: unlike numerics, lucene de-dupes strings so we increment here
doc.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("1"))); doc.add(new SortedSetDocValuesField(FIELD_NAME, new BytesRef("1")));
@ -379,16 +375,16 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
}, fieldType); }, fieldType);
} }
private void testCase(Query query, private void testAggregation(Query query,
ValueType valueType, ValueType valueType,
CheckedConsumer<RandomIndexWriter, IOException> indexer, CheckedConsumer<RandomIndexWriter, IOException> indexer,
Consumer<InternalValueCount> verify) throws IOException { Consumer<InternalValueCount> verify) throws IOException {
// Test both with and without the userValueTypeHint // Test both with and without the userValueTypeHint
testCase(query, valueType, indexer, verify, true); testAggregation(query, valueType, indexer, verify, true);
testCase(query, valueType, indexer, verify, false); testAggregation(query, valueType, indexer, verify, false);
} }
private void testCase(Query query, private void testAggregation(Query query,
ValueType valueType, ValueType valueType,
CheckedConsumer<RandomIndexWriter, IOException> indexer, CheckedConsumer<RandomIndexWriter, IOException> indexer,
Consumer<InternalValueCount> verify, boolean testWithHint) throws IOException { Consumer<InternalValueCount> verify, boolean testWithHint) throws IOException {
@ -402,28 +398,16 @@ public class ValueCountAggregatorTests extends AggregatorTestCase {
} }
aggregationBuilder.field(FIELD_NAME); aggregationBuilder.field(FIELD_NAME);
testCase(aggregationBuilder, query, indexer, verify, fieldType); testAggregation(aggregationBuilder, query, indexer, verify, fieldType);
} }
private void testCase(ValueCountAggregationBuilder aggregationBuilder, Query query, private void testAggregation(
CheckedConsumer<RandomIndexWriter, IOException> indexer, AggregationBuilder aggregationBuilder,
Consumer<InternalValueCount> verify, MappedFieldType fieldType) throws IOException { Query query,
try (Directory directory = newDirectory()) { CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { Consumer<InternalValueCount> verify,
indexer.accept(indexWriter); MappedFieldType fieldType) throws IOException {
} testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
ValueCountAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
verify.accept((InternalValueCount) aggregator.buildAggregation(0L));
}
}
} }
private static MappedFieldType createMappedFieldType(ValueType valueType) { private static MappedFieldType createMappedFieldType(ValueType valueType) {

View File

@ -503,7 +503,7 @@ public abstract class AggregatorTestCase extends ESTestCase {
InternalAggregationTestCase.assertMultiBucketConsumer(agg, shardBucketConsumer); InternalAggregationTestCase.assertMultiBucketConsumer(agg, shardBucketConsumer);
} }
if (aggs.isEmpty()) { if (aggs.isEmpty()) {
return null; return (A) root.buildEmptyAggregation();
} else { } else {
if (randomBoolean() && aggs.size() > 1) { if (randomBoolean() && aggs.size() > 1) {
// sometimes do an incremental reduce // sometimes do an incremental reduce
@ -543,10 +543,12 @@ public abstract class AggregatorTestCase extends ESTestCase {
InternalAggregationTestCase.assertMultiBucketConsumer(agg, bucketConsumer); InternalAggregationTestCase.assertMultiBucketConsumer(agg, bucketConsumer);
} }
protected <T extends AggregationBuilder, protected <T extends AggregationBuilder, V extends InternalAggregation> void testCase(
V extends InternalAggregation> void testCase(T aggregationBuilder, Query query, T aggregationBuilder,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Query query,
Consumer<V> verify, MappedFieldType fieldType) throws IOException { CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<V> verify,
MappedFieldType... fieldTypes) throws IOException {
try (Directory directory = newDirectory()) { try (Directory directory = newDirectory()) {
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter); buildIndex.accept(indexWriter);
@ -555,9 +557,8 @@ public abstract class AggregatorTestCase extends ESTestCase {
try (IndexReader indexReader = DirectoryReader.open(directory)) { try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true); IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
V agg = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType); V agg = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldTypes);
verify.accept(agg); verify.accept(agg);
} }
} }
} }

View File

@ -32,7 +32,6 @@ import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator;
@ -44,7 +43,6 @@ import org.elasticsearch.xpack.analytics.AnalyticsPlugin;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -53,7 +51,11 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton; import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
public class StringStatsAggregatorTests extends AggregatorTestCase { public class StringStatsAggregatorTests extends AggregatorTestCase {
@ -65,50 +67,8 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
private static final String VALUE_SCRIPT_NAME = "value_script"; private static final String VALUE_SCRIPT_NAME = "value_script";
private static final String FIELD_SCRIPT_NAME = "field_script"; private static final String FIELD_SCRIPT_NAME = "field_script";
private void testCase(Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalStringStats> verify) throws IOException {
TextFieldMapper.TextFieldType fieldType = new TextFieldMapper.TextFieldType();
fieldType.setName("text");
fieldType.setFielddata(true);
AggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name").field("text");
testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
}
/* TODO: This should just use the base test case in AggregatorTestCase. The main incompatibility is around returning a null
InternalAggregation instance when no docs are found, I think. --Tozzi
*/
@Override
protected <T extends AggregationBuilder, V extends InternalAggregation> void testCase(
T aggregationBuilder,
Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<V> verify,
MappedFieldType fieldType) throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
StringStatsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(query, aggregator);
aggregator.postCollection();
@SuppressWarnings("unchecked")
V aggregation = (V) aggregator.buildAggregation(0L);
verify.accept(aggregation);
indexReader.close();
directory.close();
}
public void testNoDocs() throws IOException { public void testNoDocs() throws IOException {
this.<InternalStringStats>testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
// Intentionally not writing any docs // Intentionally not writing any docs
}, stats -> { }, stats -> {
assertEquals(0, stats.getCount()); assertEquals(0, stats.getCount());
@ -122,11 +82,11 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
public void testUnmappedField() throws IOException { public void testUnmappedField() throws IOException {
StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name").field("text"); StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name").field("text");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
for(int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++) {
iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO)));
} }
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(0, stats.getCount()); assertEquals(0, stats.getCount());
assertEquals(Integer.MIN_VALUE, stats.getMaxLength()); assertEquals(Integer.MIN_VALUE, stats.getMaxLength());
assertEquals(Integer.MAX_VALUE, stats.getMinLength()); assertEquals(Integer.MAX_VALUE, stats.getMinLength());
@ -141,11 +101,11 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name") StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name")
.field("text") .field("text")
.missing("abca"); .missing("abca");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
for(int i=0; i < 10; i++) { for(int i=0; i < 10; i++) {
iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO)));
} }
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(10, stats.getCount()); assertEquals(10, stats.getCount());
assertEquals(4, stats.getMaxLength()); assertEquals(4, stats.getMaxLength());
assertEquals(4, stats.getMinLength()); assertEquals(4, stats.getMinLength());
@ -167,12 +127,12 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
.field(fieldType.name()) .field(fieldType.name())
.missing("b"); .missing("b");
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new TextField(fieldType.name(), "a", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "a", Field.Store.NO)));
iw.addDocument(Collections.emptySet()); iw.addDocument(emptySet());
iw.addDocument(singleton(new TextField(fieldType.name(), "a", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "a", Field.Store.NO)));
iw.addDocument(Collections.emptySet()); iw.addDocument(emptySet());
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(4, stats.getCount()); assertEquals(4, stats.getCount());
assertEquals(1, stats.getMaxLength()); assertEquals(1, stats.getMaxLength());
assertEquals(1, stats.getMinLength()); assertEquals(1, stats.getMinLength());
@ -185,7 +145,7 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
} }
public void testSingleValuedField() throws IOException { public void testSingleValuedField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
for(int i=0; i < 10; i++) { for(int i=0; i < 10; i++) {
iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO)));
} }
@ -203,7 +163,7 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
} }
public void testNoMatchingField() throws IOException { public void testNoMatchingField() throws IOException {
testCase(new MatchAllDocsQuery(), iw -> { testAggregation(new MatchAllDocsQuery(), iw -> {
for(int i=0; i < 10; i++) { for(int i=0; i < 10; i++) {
iw.addDocument(singleton(new TextField("wrong_field", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("wrong_field", "test" + i, Field.Store.NO)));
} }
@ -218,7 +178,7 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
} }
public void testQueryFiltering() throws IOException { public void testQueryFiltering() throws IOException {
testCase(new TermInSetQuery("text", new BytesRef("test0"), new BytesRef("test1")), iw -> { testAggregation(new TermInSetQuery("text", new BytesRef("test0"), new BytesRef("test1")), iw -> {
for(int i=0; i < 10; i++) { for(int i=0; i < 10; i++) {
iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO)));
} }
@ -246,11 +206,11 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
.format("0000.00") .format("0000.00")
.showDistribution(true); .showDistribution(true);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
for(int i=0; i < 10; i++) { for(int i=0; i < 10; i++) {
iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO))); iw.addDocument(singleton(new TextField("text", "test" + i, Field.Store.NO)));
} }
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals("0010.00", stats.getCountAsString()); assertEquals("0010.00", stats.getCountAsString());
assertEquals("0005.00", stats.getMaxLengthAsString()); assertEquals("0005.00", stats.getMaxLengthAsString());
assertEquals("0005.00", stats.getMinLengthAsString()); assertEquals("0005.00", stats.getMinLengthAsString());
@ -331,12 +291,12 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name") final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name")
.field(fieldType.name()) .field(fieldType.name())
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO)));
iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO)));
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(2, stats.getCount()); assertEquals(2, stats.getCount());
assertEquals(2, stats.getMaxLength()); assertEquals(2, stats.getMaxLength());
assertEquals(2, stats.getMinLength()); assertEquals(2, stats.getMinLength());
@ -355,9 +315,9 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name") final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name")
.field(fieldType.name()) .field(fieldType.name())
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, Collections.emptyMap())); .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, emptyMap()));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
Set<TextField> FieldData = new java.util.HashSet<>(); Set<TextField> FieldData = new java.util.HashSet<>();
FieldData.add(new TextField(fieldType.name(), "b", Field.Store.NO)); FieldData.add(new TextField(fieldType.name(), "b", Field.Store.NO));
FieldData.add(new TextField(fieldType.name(), "c", Field.Store.NO)); FieldData.add(new TextField(fieldType.name(), "c", Field.Store.NO));
@ -366,7 +326,7 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
FieldData2.add(new TextField(fieldType.name(), "b", Field.Store.NO)); FieldData2.add(new TextField(fieldType.name(), "b", Field.Store.NO));
FieldData2.add(new TextField(fieldType.name(), "c", Field.Store.NO)); FieldData2.add(new TextField(fieldType.name(), "c", Field.Store.NO));
iw.addDocument(FieldData2); iw.addDocument(FieldData2);
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(4, stats.getCount()); assertEquals(4, stats.getCount());
assertEquals(2, stats.getMaxLength()); assertEquals(2, stats.getMaxLength());
assertEquals(2, stats.getMinLength()); assertEquals(2, stats.getMinLength());
@ -386,12 +346,12 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name") final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME, .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME,
Collections.singletonMap("field", fieldType.name()))); singletonMap("field", fieldType.name())));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO)));
iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO))); iw.addDocument(singleton(new TextField(fieldType.name(), "b", Field.Store.NO)));
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(2, stats.getCount()); assertEquals(2, stats.getCount());
assertEquals(2, stats.getMaxLength()); assertEquals(2, stats.getMaxLength());
assertEquals(2, stats.getMinLength()); assertEquals(2, stats.getMinLength());
@ -410,9 +370,9 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name") final StringStatsAggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name")
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME, .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME,
Collections.singletonMap("field", fieldType.name()))); singletonMap("field", fieldType.name())));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
Set<TextField> doc = new java.util.HashSet<>(); Set<TextField> doc = new java.util.HashSet<>();
doc.add(new TextField(fieldType.name(), "b", Field.Store.NO)); doc.add(new TextField(fieldType.name(), "b", Field.Store.NO));
doc.add(new TextField(fieldType.name(), "c", Field.Store.NO)); doc.add(new TextField(fieldType.name(), "c", Field.Store.NO));
@ -421,7 +381,7 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
doc1.add(new TextField(fieldType.name(), "b", Field.Store.NO)); doc1.add(new TextField(fieldType.name(), "b", Field.Store.NO));
doc1.add(new TextField(fieldType.name(), "c", Field.Store.NO)); doc1.add(new TextField(fieldType.name(), "c", Field.Store.NO));
iw.addDocument(doc1); iw.addDocument(doc1);
}, (InternalStringStats stats) -> { }, stats -> {
assertEquals(4, stats.getCount()); assertEquals(4, stats.getCount());
assertEquals(2, stats.getMaxLength()); assertEquals(2, stats.getMaxLength());
assertEquals(2, stats.getMinLength()); assertEquals(2, stats.getMinLength());
@ -434,6 +394,26 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
}, fieldType); }, fieldType);
} }
private void testAggregation(Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalStringStats> verify) throws IOException {
TextFieldMapper.TextFieldType fieldType = new TextFieldMapper.TextFieldType();
fieldType.setName("text");
fieldType.setFielddata(true);
AggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name").field("text");
testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType);
}
private void testAggregation(
AggregationBuilder aggregationBuilder,
Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalStringStats> verify,
MappedFieldType fieldType) throws IOException {
testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
}
@Override @Override
protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldType, String fieldName) { protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldType, String fieldName) {
return new StringStatsAggregationBuilder("_name") return new StringStatsAggregationBuilder("_name")
@ -442,12 +422,12 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
@Override @Override
protected List<ValuesSourceType> getSupportedValuesSourceTypes() { protected List<ValuesSourceType> getSupportedValuesSourceTypes() {
return Collections.singletonList(CoreValuesSourceType.BYTES); return singletonList(CoreValuesSourceType.BYTES);
} }
@Override @Override
protected List<String> unsupportedMappedFieldTypes() { protected List<String> unsupportedMappedFieldTypes() {
return Collections.singletonList(IpFieldMapper.CONTENT_TYPE); return singletonList(IpFieldMapper.CONTENT_TYPE);
} }
@Override @Override
@ -461,8 +441,8 @@ public class StringStatsAggregatorTests extends AggregatorTestCase {
.map(value -> "a" + value) .map(value -> "a" + value)
.collect(Collectors.toList()); .collect(Collectors.toList());
}); });
final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, Collections.emptyMap()); final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap());
final Map<String, ScriptEngine> engines = Collections.singletonMap(engine.getType(), engine); final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS); return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
} }
} }

View File

@ -9,13 +9,9 @@ package org.elasticsearch.xpack.analytics.ttest;
import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.fielddata.ScriptDocValues; import org.elasticsearch.index.fielddata.ScriptDocValues;
@ -32,7 +28,6 @@ import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal; import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal;
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
@ -656,23 +651,4 @@ public class TTestAggregatorTests extends AggregatorTestCase {
} }
testCase(aggregationBuilder, query, buildIndex, verify, fieldType1, fieldType2); testCase(aggregationBuilder, query, buildIndex, verify, fieldType1, fieldType2);
} }
private <T extends AggregationBuilder, V extends InternalAggregation> void testCase(
T aggregationBuilder, Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<V> verify, MappedFieldType... fieldType) throws IOException {
try (Directory directory = newDirectory()) {
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
V agg = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType);
verify.accept(agg);
}
}
}
} }