Search in sources :

Example 16 with ExtendedStats

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

the class ExtendedStatsIT method testEmptyAggregation.

@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(extendedStats("stats").field("value"))).execute().actionGet();
    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());
    ExtendedStats stats = bucket.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("stats"));
    assertThat(stats.getSumOfSquares(), equalTo(0.0));
    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.getStdDeviation()), is(true));
    assertThat(Double.isNaN(stats.getAvg()), is(true));
    assertThat(Double.isNaN(stats.getStdDeviationBound(ExtendedStats.Bounds.UPPER)), is(true));
    assertThat(Double.isNaN(stats.getStdDeviationBound(ExtendedStats.Bounds.LOWER)), is(true));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with ExtendedStats

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

the class ExtendedStatsIT method testScriptMultiValuedWithParams.

@Override
public void testScriptMultiValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "[ doc['value'].value, doc['value'].value - dec ]", 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) (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));
    assertThat(stats.getSumOfSquares(), equalTo((double) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81));
    assertThat(stats.getVariance(), equalTo(variance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
    assertThat(stats.getStdDeviation(), equalTo(stdDev(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
    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)

Example 18 with ExtendedStats

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

the class ExtendedStatsIT method testMultiValuedFieldWithValueScriptWithParams.

@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    double sigma = randomDouble() * randomIntBetween(1, 10);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params)).sigma(sigma)).get();
    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 + 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));
    assertThat(stats.getSumOfSquares(), equalTo((double) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 121));
    assertThat(stats.getVariance(), equalTo(variance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)));
    assertThat(stats.getStdDeviation(), equalTo(stdDev(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 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)

Example 19 with ExtendedStats

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

the class ExtendedStatsIT method testSingleValuedField.

@Override
public void testSingleValuedField() throws Exception {
    double sigma = randomDouble() * randomIntBetween(1, 10);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(extendedStats("stats").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.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, 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 20 with ExtendedStats

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

the class ExtendedStatsIT method testOrderByEmptyAggregation.

@Override
public void testOrderByEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(terms("terms").field("value").order(Order.compound(Order.aggregation("filter>extendedStats.avg", true))).subAggregation(filter("filter", termQuery("value", 100)).subAggregation(extendedStats("extendedStats").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));
        ExtendedStats extendedStats = filter.getAggregations().get("extendedStats");
        assertThat(extendedStats, notNullValue());
        assertThat(extendedStats.getMin(), equalTo(Double.POSITIVE_INFINITY));
        assertThat(extendedStats.getMax(), equalTo(Double.NEGATIVE_INFINITY));
        assertThat(extendedStats.getAvg(), equalTo(Double.NaN));
        assertThat(extendedStats.getSum(), equalTo(0.0));
        assertThat(extendedStats.getCount(), equalTo(0L));
        assertThat(extendedStats.getStdDeviation(), equalTo(Double.NaN));
        assertThat(extendedStats.getSumOfSquares(), equalTo(0.0));
        assertThat(extendedStats.getVariance(), equalTo(Double.NaN));
        assertThat(extendedStats.getStdDeviationBound(Bounds.LOWER), equalTo(Double.NaN));
        assertThat(extendedStats.getStdDeviationBound(Bounds.UPPER), equalTo(Double.NaN));
    }
}
Also used : Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) 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