Search in sources :

Example 11 with ExtendedStats

use of org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats in project elasticsearch by elastic.

the class ExtendedStatsIT method testSingleValuedField_WithFormatter.

public void testSingleValuedField_WithFormatter() throws Exception {
    double sigma = randomDouble() * randomIntBetween(1, 10);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").format("0000.0").field("value").sigma(sigma)).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ExtendedStats 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"));
    assertThat(stats.getSumOfSquares(), equalTo((double) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100));
    assertThat(stats.getSumOfSquaresAsString(), equalTo("0385.0"));
    assertThat(stats.getVariance(), equalTo(variance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
    assertThat(stats.getVarianceAsString(), equalTo("0008.2"));
    assertThat(stats.getStdDeviation(), equalTo(stdDev(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
    assertThat(stats.getStdDeviationAsString(), equalTo("0002.9"));
    checkUpperLowerBounds(stats, sigma);
}
Also used : ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 12 with ExtendedStats

use of org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats in project elasticsearch by elastic.

the class ExtendedStatsIT method testSingleValuedFieldGetProperty.

@Override
public void testSingleValuedFieldGetProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(extendedStats("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));
    ExtendedStats stats = global.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("stats"));
    ExtendedStats statsFromProperty = (ExtendedStats) 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));
    double expectedSumOfSquaresValue = (double) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100;
    assertThat(stats.getSumOfSquares(), equalTo(expectedSumOfSquaresValue));
    assertThat((double) global.getProperty("stats.sum_of_squares"), equalTo(expectedSumOfSquaresValue));
    double expectedVarianceValue = variance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertThat(stats.getVariance(), equalTo(expectedVarianceValue));
    assertThat((double) global.getProperty("stats.variance"), equalTo(expectedVarianceValue));
    double expectedStdDevValue = stdDev(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertThat(stats.getStdDeviation(), equalTo(expectedStdDevValue));
    assertThat((double) global.getProperty("stats.std_deviation"), equalTo(expectedStdDevValue));
}
Also used : ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 13 with ExtendedStats

use of org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats in project elasticsearch by elastic.

the class ExtendedStatsIT method testSingleValuedFieldDefaultSigma.

public void testSingleValuedFieldDefaultSigma() throws Exception {
    // Same as previous test, but uses a default value for sigma
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").field("value")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ExtendedStats 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));
    assertThat(stats.getSumOfSquares(), equalTo((double) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100));
    assertThat(stats.getVariance(), equalTo(variance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
    assertThat(stats.getStdDeviation(), equalTo(stdDev(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
    checkUpperLowerBounds(stats, 2);
}
Also used : ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 14 with ExtendedStats

use of org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats in project elasticsearch by elastic.

the class ExtendedStatsIT method testUnmapped.

@Override
public void testUnmapped() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx_unmapped").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").field("value")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    ExtendedStats 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));
    assertThat(stats.getSumOfSquares(), equalTo(0.0));
    assertThat(stats.getVariance(), equalTo(Double.NaN));
    assertThat(stats.getStdDeviation(), equalTo(Double.NaN));
    assertThat(Double.isNaN(stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER)), is(true));
    assertThat(Double.isNaN(stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER)), is(true));
}
Also used : ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 15 with ExtendedStats

use of org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats in project elasticsearch by elastic.

the class ExtendedStatsIT method testScriptSingleValuedWithParams.

@Override
public void testScriptSingleValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value + inc", params);
    double sigma = randomDouble() * randomIntBetween(1, 10);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").script(script).sigma(sigma)).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ExtendedStats 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));
    assertThat(stats.getSumOfSquares(), equalTo((double) 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 121));
    assertThat(stats.getVariance(), equalTo(variance(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)));
    assertThat(stats.getStdDeviation(), equalTo(stdDev(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)));
    checkUpperLowerBounds(stats, sigma);
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)24 SearchResponse (org.elasticsearch.action.search.SearchResponse)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)22 Script (org.elasticsearch.script.Script)8 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)7 HashMap (java.util.HashMap)4 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)4 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)2 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)1 Repository (com.atlassian.stash.repository.Repository)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 GlobalSettings (com.palantir.stash.codesearch.admin.GlobalSettings)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1