diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java index 28b15145455..42804780727 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java @@ -22,30 +22,62 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.MultiReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.util.NumericUtils; import org.elasticsearch.common.CheckedConsumer; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; +import org.elasticsearch.script.MockScriptEngine; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptEngine; +import org.elasticsearch.script.ScriptModule; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; +import org.elasticsearch.search.lookup.LeafDocLookup; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.function.Function; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; import static java.util.Collections.singleton; +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; +import static org.elasticsearch.search.aggregations.AggregationBuilders.stats; public class StatsAggregatorTests extends AggregatorTestCase { - static final double TOLERANCE = 1e-10; + + private static final double TOLERANCE = 1e-10; + private static final String VALUE_SCRIPT_NAME = "value_script"; + private static final String FIELD_SCRIPT_NAME = "field_script"; public void testEmpty() throws IOException { - MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); ft.setName("field"); - testCase(ft, iw -> {}, + testCase( + stats("_name").field(ft.name()), + iw -> {}, stats -> { assertEquals(0d, stats.getCount(), 0); assertEquals(0d, stats.getSum(), 0); @@ -53,16 +85,17 @@ public class StatsAggregatorTests extends AggregatorTestCase { assertEquals(Double.POSITIVE_INFINITY, stats.getMin(), 0); assertEquals(Double.NEGATIVE_INFINITY, stats.getMax(), 0); assertFalse(AggregationInspectionHelper.hasValue(stats)); - } + }, + singleton(ft) ); } public void testRandomDoubles() throws IOException { - MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.DOUBLE); ft.setName("field"); final SimpleStatsAggregator expected = new SimpleStatsAggregator(); - testCase(ft, + testCase( + stats("_name").field(ft.name()), iw -> { int numDocs = randomIntBetween(10, 50); for (int i = 0; i < numDocs; i++) { @@ -71,7 +104,7 @@ public class StatsAggregatorTests extends AggregatorTestCase { for (int j = 0; j < numValues; j++) { double value = randomDoubleBetween(-100d, 100d, true); long valueAsLong = NumericUtils.doubleToSortableLong(value); - doc.add(new SortedNumericDocValuesField("field", valueAsLong)); + doc.add(new SortedNumericDocValuesField(ft.name(), valueAsLong)); expected.add(value); } iw.addDocument(doc); @@ -84,30 +117,16 @@ public class StatsAggregatorTests extends AggregatorTestCase { assertEquals(expected.max, stats.getMax(), 0); assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE); assertTrue(AggregationInspectionHelper.hasValue(stats)); - } + }, + singleton(ft) ); } public void testRandomLongs() throws IOException { - MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); - final SimpleStatsAggregator expected = new SimpleStatsAggregator(); - testCase(ft, - iw -> { - int numDocs = randomIntBetween(10, 50); - for (int i = 0; i < numDocs; i++) { - Document doc = new Document(); - int numValues = randomIntBetween(1, 5); - for (int j = 0; j < numValues; j++) { - long value = randomIntBetween(-100, 100); - doc.add(new SortedNumericDocValuesField("field", value)); - expected.add(value); - } - iw.addDocument(doc); - } - }, - stats -> { + randomLongsTestCase( + randomIntBetween(1, 5), + stats("_name").field("field"), + (expected, stats) -> { assertEquals(expected.count, stats.getCount(), 0); assertEquals(expected.sum, stats.getSum(), TOLERANCE); assertEquals(expected.min, stats.getMin(), 0); @@ -151,7 +170,7 @@ public class StatsAggregatorTests extends AggregatorTestCase { private void verifySummationOfDoubles(double[] values, double expectedSum, double expectedAvg, double delta) throws IOException { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.DOUBLE); ft.setName("field"); double max = Double.NEGATIVE_INFINITY; @@ -162,10 +181,11 @@ public class StatsAggregatorTests extends AggregatorTestCase { } double expectedMax = max; double expectedMin = min; - testCase(ft, + testCase( + stats("_name").field(ft.name()), iw -> { for (double value : values) { - iw.addDocument(singleton(new NumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)))); + iw.addDocument(singleton(new NumericDocValuesField(ft.name(), NumericUtils.doubleToSortableLong(value)))); } }, stats -> { @@ -174,20 +194,251 @@ public class StatsAggregatorTests extends AggregatorTestCase { assertEquals(expectedSum, stats.getSum(), delta); assertEquals(expectedMax, stats.getMax(), 0d); assertEquals(expectedMin, stats.getMin(), 0d); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + }, + singleton(ft) + ); + } + + public void testUnmapped() throws IOException { + randomLongsTestCase( + randomIntBetween(1, 5), + stats("_name").field("unmapped_field"), + (expected, stats) -> { + assertEquals(0d, stats.getCount(), 0); + assertEquals(0d, stats.getSum(), 0); + assertEquals(Float.NaN, stats.getAvg(), 0); + assertEquals(Double.POSITIVE_INFINITY, stats.getMin(), 0); + assertEquals(Double.NEGATIVE_INFINITY, stats.getMax(), 0); + assertFalse(AggregationInspectionHelper.hasValue(stats)); } ); } - public void testCase(MappedFieldType ft, - CheckedConsumer buildIndex, - Consumer verify) throws IOException { + public void testPartiallyUnmapped() throws IOException { + try (Directory mappedDirectory = newDirectory(); + Directory unmappedDirectory = newDirectory(); + RandomIndexWriter mappedWriter = new RandomIndexWriter(random(), mappedDirectory); + RandomIndexWriter unmappedWriter = new RandomIndexWriter(random(), unmappedDirectory)) { + + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); + ft.setName("field"); + final SimpleStatsAggregator expected = new SimpleStatsAggregator(); + final int numDocs = randomIntBetween(10, 50); + for (int i = 0; i < numDocs; i++) { + final long value = randomLongBetween(-100, 100); + mappedWriter.addDocument(singleton(new SortedNumericDocValuesField(ft.name(), value))); + expected.add(value); + } + final StatsAggregationBuilder builder = stats("_name").field(ft.name()); + + try (IndexReader mappedReader = mappedWriter.getReader(); + IndexReader unmappedReader = unmappedWriter.getReader(); + MultiReader multiReader = new MultiReader(mappedReader, unmappedReader)) { + + final IndexSearcher searcher = new IndexSearcher(multiReader); + final InternalStats stats = search(searcher, new MatchAllDocsQuery(), builder, ft); + + assertEquals(expected.count, stats.getCount(), 0); + assertEquals(expected.sum, stats.getSum(), TOLERANCE); + assertEquals(expected.max, stats.getMax(), 0); + assertEquals(expected.min, stats.getMin(), 0); + assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + } + } + + public void testValueScriptSingleValuedField() throws IOException { + randomLongsTestCase( + 1, + stats("_name") + .field("field") + .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, emptyMap())), + (expected, stats) -> { + final SimpleStatsAggregator adjusted = new SimpleStatsAggregator( + expected.count, + expected.min + 1, + expected.max + 1, + expected.sum + expected.count + ); + + assertEquals(adjusted.count, stats.getCount(), 0); + assertEquals(adjusted.sum, stats.getSum(), TOLERANCE); + assertEquals(adjusted.max, stats.getMax(), 0); + assertEquals(adjusted.min, stats.getMin(), 0); + assertEquals(adjusted.sum / adjusted.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + ); + } + + public void testValueScriptMultiValuedField() throws IOException { + final int valuesPerField = randomIntBetween(2, 5); + randomLongsTestCase( + valuesPerField, + stats("_name") + .field("field") + .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT_NAME, emptyMap())), + (expected, stats) -> { + final SimpleStatsAggregator adjusted = new SimpleStatsAggregator( + expected.count, + expected.min + 1, + expected.max + 1, + expected.sum + expected.count + ); + + assertEquals(adjusted.count, stats.getCount(), 0); + assertEquals(adjusted.sum, stats.getSum(), TOLERANCE); + assertEquals(adjusted.max, stats.getMax(), 0); + assertEquals(adjusted.min, stats.getMin(), 0); + assertEquals(adjusted.sum / adjusted.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + ); + } + + public void testFieldScriptSingleValuedField() throws IOException { + randomLongsTestCase( + 1, + stats("_name") + .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME, singletonMap("field", "field"))), + (expected, stats) -> { + final SimpleStatsAggregator adjusted = new SimpleStatsAggregator( + expected.count, + expected.min + 1, + expected.max + 1, + expected.sum + expected.count + ); + + assertEquals(adjusted.count, stats.getCount(), 0); + assertEquals(adjusted.sum, stats.getSum(), TOLERANCE); + assertEquals(adjusted.max, stats.getMax(), 0); + assertEquals(adjusted.min, stats.getMin(), 0); + assertEquals(adjusted.sum / adjusted.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + ); + } + + public void testFieldScriptMultiValuedField() throws IOException { + final int valuesPerField = randomIntBetween(2, 5); + randomLongsTestCase( + valuesPerField, + stats("_name") + .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, FIELD_SCRIPT_NAME, singletonMap("field", "field"))), + (expected, stats) -> { + final SimpleStatsAggregator adjusted = new SimpleStatsAggregator( + expected.count, + expected.min + 1, + expected.max + 1, + expected.sum + expected.count + ); + + assertEquals(adjusted.count, stats.getCount(), 0); + assertEquals(adjusted.sum, stats.getSum(), TOLERANCE); + assertEquals(adjusted.max, stats.getMax(), 0); + assertEquals(adjusted.min, stats.getMin(), 0); + assertEquals(adjusted.sum / adjusted.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + ); + } + + public void testMissing() throws IOException { + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); + ft.setName("field"); + + final long missingValue = randomIntBetween(-100, 100); + + final int numDocs = randomIntBetween(100, 200); + final List> docs = new ArrayList<>(numDocs); + final SimpleStatsAggregator expected = new SimpleStatsAggregator(); + for (int i = 0; i < numDocs; i++) { + if (randomBoolean()) { + final long value = randomLongBetween(-100, 100); + docs.add(singleton(new SortedNumericDocValuesField(ft.name(), value))); + expected.add(value); + } else { + docs.add(emptySet()); + expected.add(missingValue); + } + } + + testCase( + stats("_name") + .field(ft.name()) + .missing(missingValue), + iw -> iw.addDocuments(docs), + stats -> { + assertEquals(expected.count, stats.getCount(), 0); + assertEquals(expected.sum, stats.getSum(), TOLERANCE); + assertEquals(expected.max, stats.getMax(), 0); + assertEquals(expected.min, stats.getMin(), 0); + assertEquals(expected.sum / expected.count, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + }, + singleton(ft) + ); + } + + public void testMissingUnmapped() throws IOException { + final int valuesPerField = randomIntBetween(1, 5); + final long missingValue = randomLongBetween(-100, 100); + randomLongsTestCase( + valuesPerField, + stats("_name") + .field("unknown_field") + .missing(missingValue), + (expected, stats) -> { + final long numDocs = expected.count / valuesPerField; + assertEquals(numDocs, stats.getCount()); + assertEquals(numDocs * missingValue, stats.getSum(), TOLERANCE); + assertEquals(missingValue, stats.getMax(), 0); + assertEquals(missingValue, stats.getMin(), 0); + assertEquals(missingValue, stats.getAvg(), TOLERANCE); + assertTrue(AggregationInspectionHelper.hasValue(stats)); + } + ); + } + + private void randomLongsTestCase(int valuesPerField, + StatsAggregationBuilder builder, + BiConsumer verify) throws IOException { + + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); + ft.setName("field"); + + final int numDocs = randomIntBetween(10, 50); + final List> docs = new ArrayList<>(numDocs); + final SimpleStatsAggregator expected = new SimpleStatsAggregator(); + for (int iDoc = 0; iDoc < numDocs; iDoc++) { + List values = randomList(valuesPerField, valuesPerField, () -> randomLongBetween(-100, 100)); + docs.add(values.stream() + .map(value -> new SortedNumericDocValuesField(ft.name(), value)) + .collect(toSet())); + values.forEach(expected::add); + } + + testCase( + builder, + iw -> iw.addDocuments(docs), + stats -> verify.accept(expected, stats), + singleton(ft) + ); + } + + private void testCase(StatsAggregationBuilder builder, + CheckedConsumer buildIndex, + Consumer verify, + Collection fieldTypes) throws IOException { try (Directory directory = newDirectory(); - RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { + RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { buildIndex.accept(indexWriter); try (IndexReader reader = indexWriter.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - StatsAggregationBuilder aggBuilder = new StatsAggregationBuilder("my_agg").field("field"); - InternalStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ft); + final MappedFieldType[] fieldTypesArray = fieldTypes.toArray(new MappedFieldType[0]); + final InternalStats stats = search(searcher, new MatchAllDocsQuery(), builder, fieldTypesArray); verify.accept(stats); } } @@ -199,6 +450,15 @@ public class StatsAggregatorTests extends AggregatorTestCase { double max = Long.MIN_VALUE; double sum = 0; + SimpleStatsAggregator() {} + + SimpleStatsAggregator(long count, double min, double max, double sum) { + this.count = count; + this.min = min; + this.max = max; + this.sum = sum; + } + void add(double value) { count ++; if (Double.compare(value, min) < 0) { @@ -210,4 +470,32 @@ public class StatsAggregatorTests extends AggregatorTestCase { sum += value; } } + + @Override + protected List getSupportedValuesSourceTypes() { + return singletonList(CoreValuesSourceType.NUMERIC); + } + + @Override + protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldType, String fieldName) { + return new StatsAggregationBuilder("_name") + .field(fieldName); + } + + @Override + protected ScriptService getMockScriptService() { + final Map, Object>> scripts = org.elasticsearch.common.collect.Map.of( + VALUE_SCRIPT_NAME, vars -> ((Number) vars.get("_value")).doubleValue() + 1, + FIELD_SCRIPT_NAME, vars -> { + final String fieldName = (String) vars.get("field"); + final LeafDocLookup lookup = (LeafDocLookup) vars.get("doc"); + return lookup.get(fieldName).stream() + .map(value -> ((Number) value).longValue() + 1) + .collect(toList()); + } + ); + final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap()); + final Map engines = singletonMap(engine.getType(), engine); + return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS); + } } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsIT.java index 036c38d3776..8327a55cd36 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsIT.java @@ -35,11 +35,8 @@ import org.elasticsearch.search.aggregations.BucketOrder; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import static java.util.Collections.emptyMap; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.search.aggregations.AggregationBuilders.filter; @@ -86,40 +83,6 @@ public class StatsIT extends AbstractNumericTestCase { assertThat(Double.isNaN(stats.getAvg()), is(true)); } - @Override - public void testUnmapped() throws Exception { - SearchResponse searchResponse = client().prepareSearch("idx_unmapped") - .setQuery(matchAllQuery()) - .addAggregation(stats("stats").field("value")) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo(Double.NaN)); - assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY)); - assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY)); - assertThat(stats.getSum(), equalTo(0.0)); - assertThat(stats.getCount(), equalTo(0L)); - } - - public void testPartiallyUnmapped() { - Stats s1 = client().prepareSearch("idx") - .addAggregation(stats("stats").field("value")).get() - .getAggregations().get("stats"); - Stats s2 = client().prepareSearch("idx", "idx_unmapped") - .addAggregation(stats("stats").field("value")).get() - .getAggregations().get("stats"); - assertEquals(s1.getAvg(), s2.getAvg(), 1e-10); - assertEquals(s1.getCount(), s2.getCount()); - assertEquals(s1.getMin(), s2.getMin(), 0d); - assertEquals(s1.getMax(), s2.getMax(), 0d); - } - @Override public void testSingleValuedField() throws Exception { SearchResponse searchResponse = client().prepareSearch("idx") @@ -199,77 +162,6 @@ public class StatsIT extends AbstractNumericTestCase { assertThat((double) ((InternalAggregation)global).getProperty("stats.count"), equalTo((double) expectedCountValue)); } - @Override - public void testSingleValuedFieldPartiallyUnmapped() throws Exception { - SearchResponse searchResponse = client().prepareSearch("idx", "idx_unmapped") - .setQuery(matchAllQuery()) - .addAggregation(stats("stats").field("value")) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10)); - assertThat(stats.getMin(), equalTo(1.0)); - assertThat(stats.getMax(), equalTo(10.0)); - assertThat(stats.getSum(), equalTo((double) 1+2+3+4+5+6+7+8+9+10)); - assertThat(stats.getCount(), equalTo(10L)); - } - - @Override - public void testSingleValuedFieldWithValueScript() throws Exception { - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .field("value") - .script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value + 1", emptyMap()))) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (2+3+4+5+6+7+8+9+10+11) / 10)); - assertThat(stats.getMin(), equalTo(2.0)); - assertThat(stats.getMax(), equalTo(11.0)); - assertThat(stats.getSum(), equalTo((double) 2+3+4+5+6+7+8+9+10+11)); - assertThat(stats.getCount(), equalTo(10L)); - } - - @Override - public void testSingleValuedFieldWithValueScriptWithParams() throws Exception { - Map params = new HashMap<>(); - params.put("inc", 1); - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .field("value") - .script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value + inc", params))) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (2+3+4+5+6+7+8+9+10+11) / 10)); - assertThat(stats.getMin(), equalTo(2.0)); - assertThat(stats.getMax(), equalTo(11.0)); - assertThat(stats.getSum(), equalTo((double) 2+3+4+5+6+7+8+9+10+11)); - assertThat(stats.getCount(), equalTo(10L)); - } - @Override public void testMultiValuedField() throws Exception { SearchResponse searchResponse = client().prepareSearch("idx") @@ -291,157 +183,6 @@ public class StatsIT extends AbstractNumericTestCase { assertThat(stats.getCount(), equalTo(20L)); } - @Override - public void testMultiValuedFieldWithValueScript() throws Exception { - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .field("values") - .script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - 1", emptyMap()))) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (1+2+3+4+5+6+7+8+9+10+2+3+4+5+6+7+8+9+10+11) / 20)); - assertThat(stats.getMin(), equalTo(1.0)); - assertThat(stats.getMax(), equalTo(11.0)); - assertThat(stats.getSum(), equalTo((double) 1+2+3+4+5+6+7+8+9+10+2+3+4+5+6+7+8+9+10+11)); - assertThat(stats.getCount(), equalTo(20L)); - } - - @Override - public void testMultiValuedFieldWithValueScriptWithParams() throws Exception { - Map params = new HashMap<>(); - params.put("dec", 1); - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .field("values") - .script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params))) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (1+2+3+4+5+6+7+8+9+10+2+3+4+5+6+7+8+9+10+11) / 20)); - assertThat(stats.getMin(), equalTo(1.0)); - assertThat(stats.getMax(), equalTo(11.0)); - assertThat(stats.getSum(), equalTo((double) 1+2+3+4+5+6+7+8+9+10+2+3+4+5+6+7+8+9+10+11)); - assertThat(stats.getCount(), equalTo(20L)); - } - - @Override - public void testScriptSingleValued() throws Exception { - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value", emptyMap()))) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10)); - assertThat(stats.getMin(), equalTo(1.0)); - assertThat(stats.getMax(), equalTo(10.0)); - assertThat(stats.getSum(), equalTo((double) 1+2+3+4+5+6+7+8+9+10)); - assertThat(stats.getCount(), equalTo(10L)); - } - - @Override - public void testScriptSingleValuedWithParams() throws Exception { - Map params = new HashMap<>(); - params.put("inc", 1); - - Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value + inc", params); - - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation(stats("stats").script(script)) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (2+3+4+5+6+7+8+9+10+11) / 10)); - assertThat(stats.getMin(), equalTo(2.0)); - assertThat(stats.getMax(), equalTo(11.0)); - assertThat(stats.getSum(), equalTo((double) 2+3+4+5+6+7+8+9+10+11)); - assertThat(stats.getCount(), equalTo(10L)); - } - - @Override - public void testScriptMultiValued() throws Exception { - Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['values']", emptyMap()); - - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation(stats("stats").script(script)) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (2+3+4+5+6+7+8+9+10+11+3+4+5+6+7+8+9+10+11+12) / 20)); - assertThat(stats.getMin(), equalTo(2.0)); - assertThat(stats.getMax(), equalTo(12.0)); - assertThat(stats.getSum(), equalTo((double) 2+3+4+5+6+7+8+9+10+11+3+4+5+6+7+8+9+10+11+12)); - assertThat(stats.getCount(), equalTo(20L)); - } - - @Override - public void testScriptMultiValuedWithParams() throws Exception { - Map params = new HashMap<>(); - params.put("dec", 1); - - Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "[ doc['value'].value, doc['value'].value - dec ]", - params); - - SearchResponse searchResponse = client().prepareSearch("idx") - .setQuery(matchAllQuery()) - .addAggregation( - stats("stats") - .script(script)) - .get(); - - assertShardExecutionState(searchResponse, 0); - - assertHitCount(searchResponse, 10); - - Stats stats = searchResponse.getAggregations().get("stats"); - assertThat(stats, notNullValue()); - assertThat(stats.getName(), equalTo("stats")); - assertThat(stats.getAvg(), equalTo((double) (1+2+3+4+5+6+7+8+9+10+0+1+2+3+4+5+6+7+8+9) / 20)); - assertThat(stats.getMin(), equalTo(0.0)); - assertThat(stats.getMax(), equalTo(10.0)); - assertThat(stats.getSum(), equalTo((double) 1+2+3+4+5+6+7+8+9+10+0+1+2+3+4+5+6+7+8+9)); - assertThat(stats.getCount(), equalTo(20L)); - } - @Override public void testOrderByEmptyAggregation() throws Exception { SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java index 1b8d0df10ad..088b35b8b32 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java @@ -164,34 +164,6 @@ public class SumIT extends AbstractNumericTestCase { assertThat((double) ((InternalAggregation)sum).getProperty("value"), equalTo(expectedSumValue)); } - /** This test has been moved to {@link SumAggregatorTests#testPartiallyUnmapped()} */ - @Override - public void testSingleValuedFieldPartiallyUnmapped() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testValueScriptSingleValuedField()} */ - @Override - public void testSingleValuedFieldWithValueScript() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testValueScriptSingleValuedField()} */ - @Override - public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testFieldScriptSingleValuedField()} */ - @Override - public void testScriptSingleValued() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testFieldScriptSingleValuedField()} */ - @Override - public void testScriptSingleValuedWithParams() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testFieldScriptMultiValuedField()} */ - @Override - public void testScriptMultiValued() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testFieldScriptMultiValuedField()} */ - @Override - public void testScriptMultiValuedWithParams() throws Exception {} - @Override public void testMultiValuedField() throws Exception { @@ -208,14 +180,6 @@ public class SumIT extends AbstractNumericTestCase { assertThat(sum.getValue(), equalTo((double) 2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12)); } - /** this test has been moved to {@link SumAggregatorTests#testValueScriptMultiValuedField()} */ - @Override - public void testMultiValuedFieldWithValueScript() throws Exception {} - - /** this test has been moved to {@link SumAggregatorTests#testValueScriptMultiValuedField()} */ - @Override - public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {} - @Override public void testOrderByEmptyAggregation() throws Exception { SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()) diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractNumericTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractNumericTestCase.java index 5ce4de82c2a..85421978938 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractNumericTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/metrics/AbstractNumericTestCase.java @@ -67,33 +67,33 @@ public abstract class AbstractNumericTestCase extends ESIntegTestCase { ensureSearchable(); } - public abstract void testEmptyAggregation() throws Exception; + public void testEmptyAggregation() throws Exception {} - public abstract void testUnmapped() throws Exception; + public void testUnmapped() throws Exception {} - public abstract void testSingleValuedField() throws Exception; + public void testSingleValuedField() throws Exception {} - public abstract void testSingleValuedFieldGetProperty() throws Exception; + public void testSingleValuedFieldGetProperty() throws Exception {} - public abstract void testSingleValuedFieldPartiallyUnmapped() throws Exception; + public void testSingleValuedFieldPartiallyUnmapped() throws Exception {} - public abstract void testSingleValuedFieldWithValueScript() throws Exception; + public void testSingleValuedFieldWithValueScript() throws Exception {} - public abstract void testSingleValuedFieldWithValueScriptWithParams() throws Exception; + public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {} - public abstract void testMultiValuedField() throws Exception; + public void testMultiValuedField() throws Exception {} - public abstract void testMultiValuedFieldWithValueScript() throws Exception; + public void testMultiValuedFieldWithValueScript() throws Exception {} - public abstract void testMultiValuedFieldWithValueScriptWithParams() throws Exception; + public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {} - public abstract void testScriptSingleValued() throws Exception; + public void testScriptSingleValued() throws Exception {} - public abstract void testScriptSingleValuedWithParams() throws Exception; + public void testScriptSingleValuedWithParams() throws Exception {} - public abstract void testScriptMultiValued() throws Exception; + public void testScriptMultiValued() throws Exception {} - public abstract void testScriptMultiValuedWithParams() throws Exception; + public void testScriptMultiValuedWithParams() throws Exception {} - public abstract void testOrderByEmptyAggregation() throws Exception; + public void testOrderByEmptyAggregation() throws Exception {} }