@Test public void singleValuedField_OrderedByMultiValueSubAggregationDesc() throws Exception { boolean asc = false; SearchResponse response = client() .prepareSearch("idx") .setTypes("type") .addAggregation( terms("terms") .executionHint(randomExecutionHint()) .field(SINGLE_VALUED_FIELD_NAME) .order(Terms.Order.aggregation("stats.avg", asc)) .subAggregation(stats("stats").field("i"))) .execute() .actionGet(); assertSearchResponse(response); Terms terms = response.getAggregations().get("terms"); assertThat(terms, notNullValue()); assertThat(terms.getName(), equalTo("terms")); assertThat(terms.getBuckets().size(), equalTo(5)); int i = 4; for (Terms.Bucket bucket : terms.getBuckets()) { assertThat(bucket, notNullValue()); assertThat(key(bucket), equalTo("val" + i)); assertThat(bucket.getDocCount(), equalTo(1l)); Stats stats = bucket.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getMax(), equalTo((double) i)); i--; } }
public void testStats() { SearchResponse response = client().prepareSearch("idx").addAggregation(stats("stats").field("long").missing(5)).get(); assertSearchResponse(response); Stats stats = response.getAggregations().get("stats"); assertEquals(2, stats.getCount()); assertEquals(4, stats.getAvg(), 0); }
@Test public void multiValueAggDerivative() throws Exception { SearchResponse response = client() .prepareSearch("idx") .addAggregation( histogram("histo") .field(SINGLE_VALUED_FIELD_NAME) .interval(interval) .subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME)) .subAggregation(derivative("deriv").setBucketsPaths("stats.sum"))) .execute() .actionGet(); assertSearchResponse(response); InternalHistogram<Bucket> deriv = response.getAggregations().get("histo"); assertThat(deriv, notNullValue()); assertThat(deriv.getName(), equalTo("histo")); assertThat(deriv.getBuckets().size(), equalTo(numValueBuckets)); Object[] propertiesKeys = (Object[]) deriv.getProperty("_key"); Object[] propertiesDocCounts = (Object[]) deriv.getProperty("_count"); Object[] propertiesSumCounts = (Object[]) deriv.getProperty("stats.sum"); List<Bucket> buckets = new ArrayList<Bucket>(deriv.getBuckets()); Long expectedSumPreviousBucket = Long.MIN_VALUE; // start value, gets // overwritten for (int i = 0; i < numValueBuckets; ++i) { Histogram.Bucket bucket = buckets.get(i); checkBucketKeyAndDocCount("Bucket " + i, bucket, i * interval, valueCounts[i]); Stats stats = bucket.getAggregations().get("stats"); assertThat(stats, notNullValue()); long expectedSum = valueCounts[i] * (i * interval); assertThat(stats.getSum(), equalTo((double) expectedSum)); SimpleValue sumDeriv = bucket.getAggregations().get("deriv"); if (i > 0) { assertThat(sumDeriv, notNullValue()); long sumDerivValue = expectedSum - expectedSumPreviousBucket; assertThat(sumDeriv.value(), equalTo((double) sumDerivValue)); assertThat( (double) bucket.getProperty( "histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()), equalTo((double) sumDerivValue)); } else { assertThat(sumDeriv, nullValue()); } expectedSumPreviousBucket = expectedSum; assertThat((long) propertiesKeys[i], equalTo((long) i * interval)); assertThat((long) propertiesDocCounts[i], equalTo(valueCounts[i])); assertThat((double) propertiesSumCounts[i], equalTo((double) expectedSum)); } }
@Override public void testEmptyAggregation() throws Exception { SearchResponse searchResponse = client() .prepareSearch("empty_bucket_idx") .setQuery(matchAllQuery()) .addAggregation( histogram("histo") .field("value") .interval(1L) .minDocCount(0) .subAggregation(stats("stats").field("value"))) .execute() .actionGet(); assertShardExecutionState(searchResponse, 0); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); Histogram histo = searchResponse.getAggregations().get("histo"); assertThat(histo, notNullValue()); Histogram.Bucket bucket = histo.getBuckets().get(1); assertThat(bucket, notNullValue()); Stats stats = bucket.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getName(), equalTo("stats")); assertThat(stats.getCount(), equalTo(0L)); assertThat(stats.getSum(), equalTo(0.0)); assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY)); assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY)); assertThat(Double.isNaN(stats.getAvg()), is(true)); }
@Override public void testScriptSingleValuedWithParams() throws Exception { Map<String, Object> params = new HashMap<>(); params.put("inc", 1); Script script = new Script( "doc['value'].value + inc", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, 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)); }
@Test public void withStatsSubAggregator() throws Exception { SearchResponse response = client() .prepareSearch("idx") .setQuery(QueryBuilders.termQuery("tag", "tag1")) .addAggregation(global("global").subAggregation(stats("value_stats").field("value"))) .execute() .actionGet(); assertSearchResponse(response); Global global = response.getAggregations().get("global"); assertThat(global, notNullValue()); assertThat(global.getName(), equalTo("global")); assertThat(global.getDocCount(), equalTo((long) numDocs)); assertThat(global.getAggregations().asList().isEmpty(), is(false)); Stats stats = global.getAggregations().get("value_stats"); assertThat(stats, notNullValue()); assertThat(stats.getName(), equalTo("value_stats")); long sum = 0; for (int i = 0; i < numDocs; ++i) { sum += i + 1; } assertThat(stats.getAvg(), equalTo((double) sum / numDocs)); assertThat(stats.getMin(), equalTo(1.0)); assertThat(stats.getMax(), equalTo((double) numDocs)); assertThat(stats.getCount(), equalTo((long) numDocs)); assertThat(stats.getSum(), equalTo((double) sum)); }
@Override public void testScriptSingleValued() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx") .setQuery(matchAllQuery()) .addAggregation( stats("stats") .script( new Script( "doc['value'].value", ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, 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)); }
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); }
public void testSpecialValueVariable() throws Exception { // i.e. _value for aggregations createIndex("test"); ensureGreen("test"); indexRandom( true, client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2), client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4), client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8)); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.matchAllQuery()) .addAggregation( AggregationBuilders.stats("int_agg") .field("x") .script( new Script( "_value * 3", ScriptType.INLINE, ExpressionScriptEngineService.NAME, null))) .addAggregation( AggregationBuilders.stats("double_agg") .field("y") .script( new Script( "_value - 1.1", ScriptType.INLINE, ExpressionScriptEngineService.NAME, null))); SearchResponse rsp = req.get(); assertEquals(3, rsp.getHits().getTotalHits()); Stats stats = rsp.getAggregations().get("int_agg"); assertEquals(39.0, stats.getMax(), 0.0001); assertEquals(15.0, stats.getMin(), 0.0001); stats = rsp.getAggregations().get("double_agg"); assertEquals(0.7, stats.getMax(), 0.0001); assertEquals(0.1, stats.getMin(), 0.0001); }
@Override public void testMultiValuedField() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx") .setQuery(matchAllQuery()) .addAggregation(stats("stats").field("values")) .execute() .actionGet(); 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)); }
@Test public void statsTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT STATS(age) FROM %s/account", TEST_INDEX)); Stats stats = result.get("STATS(age)"); Assert.assertEquals(1000, stats.getCount()); assertThat(stats.getSum(), equalTo(30171.0)); assertThat(stats.getMin(), equalTo(20.0)); assertThat(stats.getMax(), equalTo(40.0)); assertThat(stats.getAvg(), equalTo(30.171)); }
@Override public void testSingleValuedFieldGetProperty() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx") .setQuery(matchAllQuery()) .addAggregation(global("global").subAggregation(stats("stats").field("value"))) .execute() .actionGet(); assertHitCount(searchResponse, 10); Global global = searchResponse.getAggregations().get("global"); assertThat(global, notNullValue()); assertThat(global.getName(), equalTo("global")); assertThat(global.getDocCount(), equalTo(10L)); assertThat(global.getAggregations(), notNullValue()); assertThat(global.getAggregations().asMap().size(), equalTo(1)); Stats stats = global.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getName(), equalTo("stats")); Stats statsFromProperty = (Stats) global.getProperty("stats"); assertThat(statsFromProperty, notNullValue()); assertThat(statsFromProperty, sameInstance(stats)); double expectedAvgValue = (double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10; assertThat(stats.getAvg(), equalTo(expectedAvgValue)); assertThat((double) global.getProperty("stats.avg"), equalTo(expectedAvgValue)); double expectedMinValue = 1.0; assertThat(stats.getMin(), equalTo(expectedMinValue)); assertThat((double) global.getProperty("stats.min"), equalTo(expectedMinValue)); double expectedMaxValue = 10.0; assertThat(stats.getMax(), equalTo(expectedMaxValue)); assertThat((double) global.getProperty("stats.max"), equalTo(expectedMaxValue)); double expectedSumValue = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; assertThat(stats.getSum(), equalTo(expectedSumValue)); assertThat((double) global.getProperty("stats.sum"), equalTo(expectedSumValue)); long expectedCountValue = 10; assertThat(stats.getCount(), equalTo(expectedCountValue)); assertThat((double) global.getProperty("stats.count"), equalTo((double) expectedCountValue)); }
@Override public void testUnmapped() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx_unmapped") .setQuery(matchAllQuery()) .addAggregation(stats("stats").field("value")) .execute() .actionGet(); assertShardExecutionState(searchResponse, 0); assertThat(searchResponse.getHits().getTotalHits(), 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)); }
@Override public void testSingleValuedFieldPartiallyUnmapped() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx", "idx_unmapped") .setQuery(matchAllQuery()) .addAggregation(stats("stats").field("value")) .execute() .actionGet(); 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)); }
public void testSimple() throws Exception { SearchResponse response = client() .prepareSearch("idx") .addAggregation( nested("nested", "nested") .subAggregation(stats("nested_value_stats").field("nested.value"))) .execute() .actionGet(); assertSearchResponse(response); double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; long sum = 0; long count = 0; for (int i = 0; i < numParents; ++i) { for (int j = 0; j < numChildren[i]; ++j) { final long value = i + 1 + j; min = Math.min(min, value); max = Math.max(max, value); sum += value; ++count; } } Nested nested = response.getAggregations().get("nested"); assertThat(nested, notNullValue()); assertThat(nested.getName(), equalTo("nested")); assertThat(nested.getDocCount(), equalTo(count)); assertThat(nested.getAggregations().asList().isEmpty(), is(false)); Stats stats = nested.getAggregations().get("nested_value_stats"); assertThat(stats, notNullValue()); assertThat(stats.getMin(), equalTo(min)); assertThat(stats.getMax(), equalTo(max)); assertThat(stats.getCount(), equalTo(count)); assertThat(stats.getSum(), equalTo((double) sum)); assertThat(stats.getAvg(), equalTo((double) sum / count)); }
@Override public void testOrderByEmptyAggregation() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx") .setQuery(matchAllQuery()) .addAggregation( terms("terms") .field("value") .order(Order.compound(Order.aggregation("filter>stats.avg", true))) .subAggregation( filter("filter", termQuery("value", 100)) .subAggregation(stats("stats").field("value")))) .get(); assertHitCount(searchResponse, 10); Terms terms = searchResponse.getAggregations().get("terms"); assertThat(terms, notNullValue()); List<Terms.Bucket> buckets = terms.getBuckets(); assertThat(buckets, notNullValue()); assertThat(buckets.size(), equalTo(10)); for (int i = 0; i < 10; i++) { Terms.Bucket bucket = buckets.get(i); assertThat(bucket, notNullValue()); assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1)); assertThat(bucket.getDocCount(), equalTo(1L)); Filter filter = bucket.getAggregations().get("filter"); assertThat(filter, notNullValue()); assertThat(filter.getDocCount(), equalTo(0L)); Stats stats = filter.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY)); assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY)); assertThat(stats.getAvg(), equalTo(Double.NaN)); assertThat(stats.getSum(), equalTo(0.0)); assertThat(stats.getCount(), equalTo(0L)); } }
@Test public void singleValuedField_OrderedBySubAggregationAsc_MultiHierarchyLevels() throws Exception { boolean asc = randomBoolean(); SearchResponse response = client() .prepareSearch("idx") .setTypes("type") .addAggregation( terms("tags") .executionHint(randomExecutionHint()) .field("tag") .order(Terms.Order.aggregation("filter1>filter2>stats.max", asc)) .subAggregation( filter("filter1") .filter(FilterBuilders.matchAllFilter()) .subAggregation( filter("filter2") .filter(FilterBuilders.matchAllFilter()) .subAggregation(stats("stats").field("i"))))) .execute() .actionGet(); assertSearchResponse(response); Terms tags = response.getAggregations().get("tags"); assertThat(tags, notNullValue()); assertThat(tags.getName(), equalTo("tags")); assertThat(tags.getBuckets().size(), equalTo(2)); Iterator<Terms.Bucket> iters = tags.getBuckets().iterator(); // the max for "more" is 2 // the max for "less" is 4 Terms.Bucket tag = iters.next(); assertThat(tag, notNullValue()); assertThat(key(tag), equalTo(asc ? "more" : "less")); assertThat(tag.getDocCount(), equalTo(asc ? 3l : 2l)); Filter filter1 = tag.getAggregations().get("filter1"); assertThat(filter1, notNullValue()); assertThat(filter1.getDocCount(), equalTo(asc ? 3l : 2l)); Filter filter2 = filter1.getAggregations().get("filter2"); assertThat(filter2, notNullValue()); assertThat(filter2.getDocCount(), equalTo(asc ? 3l : 2l)); Stats stats = filter2.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0)); tag = iters.next(); assertThat(tag, notNullValue()); assertThat(key(tag), equalTo(asc ? "less" : "more")); assertThat(tag.getDocCount(), equalTo(asc ? 2l : 3l)); filter1 = tag.getAggregations().get("filter1"); assertThat(filter1, notNullValue()); assertThat(filter1.getDocCount(), equalTo(asc ? 2l : 3l)); filter2 = filter1.getAggregations().get("filter2"); assertThat(filter2, notNullValue()); assertThat(filter2.getDocCount(), equalTo(asc ? 2l : 3l)); stats = filter2.getAggregations().get("stats"); assertThat(stats, notNullValue()); assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0)); }
public void testSingleValuedField_WithFormatter() throws Exception { SearchResponse searchResponse = client() .prepareSearch("idx") .setQuery(matchAllQuery()) .addAggregation(stats("stats").format("0000.0").field("value")) .execute() .actionGet(); 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.getAvgAsString(), equalTo("0005.5")); assertThat(stats.getMin(), equalTo(1.0)); assertThat(stats.getMinAsString(), equalTo("0001.0")); assertThat(stats.getMax(), equalTo(10.0)); assertThat(stats.getMaxAsString(), equalTo("0010.0")); assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)); assertThat(stats.getSumAsString(), equalTo("0055.0")); assertThat(stats.getCount(), equalTo(10L)); assertThat(stats.getCountAsString(), equalTo("0010.0")); }