Search in sources :

Example 66 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class MinBucketIT method testDocCountTopLevel.

public void testDocCountTopLevel() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).addAggregation(minBucket("min_bucket", "histo>_count")).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(numValueBuckets));
    List<String> minKeys = new ArrayList<>();
    double minValue = Double.POSITIVE_INFINITY;
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
        assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
        if (bucket.getDocCount() < minValue) {
            minValue = bucket.getDocCount();
            minKeys = new ArrayList<>();
            minKeys.add(bucket.getKeyAsString());
        } else if (bucket.getDocCount() == minValue) {
            minKeys.add(bucket.getKeyAsString());
        }
    }
    InternalBucketMetricValue minBucketValue = response.getAggregations().get("min_bucket");
    assertThat(minBucketValue, notNullValue());
    assertThat(minBucketValue.getName(), equalTo("min_bucket"));
    assertThat(minBucketValue.value(), equalTo(minValue));
    assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 67 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class MinBucketIT method testMetricAsSubAgg.

public void testMetricAsSubAgg() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).subAggregation(minBucket("min_bucket", "histo>sum"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Bucket> buckets = histo.getBuckets();
        List<String> minKeys = new ArrayList<>();
        double minValue = Double.POSITIVE_INFINITY;
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            if (bucket.getDocCount() != 0) {
                Sum sum = bucket.getAggregations().get("sum");
                assertThat(sum, notNullValue());
                if (sum.value() < minValue) {
                    minValue = sum.value();
                    minKeys = new ArrayList<>();
                    minKeys.add(bucket.getKeyAsString());
                } else if (sum.value() == minValue) {
                    minKeys.add(bucket.getKeyAsString());
                }
            }
        }
        InternalBucketMetricValue minBucketValue = termsBucket.getAggregations().get("min_bucket");
        assertThat(minBucketValue, notNullValue());
        assertThat(minBucketValue.getName(), equalTo("min_bucket"));
        assertThat(minBucketValue.value(), equalTo(minValue));
        assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.minBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.minBucket)

Example 68 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class MinBucketIT method testDocCountAsSubAgg.

public void testDocCountAsSubAgg() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(minBucket("min_bucket", "histo>_count"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Bucket> buckets = histo.getBuckets();
        List<String> minKeys = new ArrayList<>();
        double minValue = Double.POSITIVE_INFINITY;
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            if (bucket.getDocCount() < minValue) {
                minValue = bucket.getDocCount();
                minKeys = new ArrayList<>();
                minKeys.add(bucket.getKeyAsString());
            } else if (bucket.getDocCount() == minValue) {
                minKeys.add(bucket.getKeyAsString());
            }
        }
        InternalBucketMetricValue minBucketValue = termsBucket.getAggregations().get("min_bucket");
        assertThat(minBucketValue, notNullValue());
        assertThat(minBucketValue.getName(), equalTo("min_bucket"));
        assertThat(minBucketValue.value(), equalTo(minValue));
        assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.minBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.minBucket)

Example 69 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class PercentilesBucketIT method testNested.

public void testNested() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Terms.Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(percentilesBucket("percentile_histo_bucket", "histo>_count").percents(PERCENTS))).addAggregation(percentilesBucket("percentile_terms_bucket", "terms>percentile_histo_bucket.50").percents(PERCENTS)).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    double[] values = new double[termsBuckets.size()];
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Histogram.Bucket> buckets = histo.getBuckets();
        double[] innerValues = new double[numValueBuckets];
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            innerValues[j] = bucket.getDocCount();
        }
        Arrays.sort(innerValues);
        PercentilesBucket percentilesBucketValue = termsBucket.getAggregations().get("percentile_histo_bucket");
        assertThat(percentilesBucketValue, notNullValue());
        assertThat(percentilesBucketValue.getName(), equalTo("percentile_histo_bucket"));
        assertPercentileBucket(PERCENTS, innerValues, percentilesBucketValue);
        values[i] = percentilesBucketValue.percentile(50.0);
    }
    Arrays.sort(values);
    PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentile_terms_bucket");
    assertThat(percentilesBucketValue, notNullValue());
    assertThat(percentilesBucketValue.getName(), equalTo("percentile_terms_bucket"));
    assertPercentileBucket(PERCENTS, values, percentilesBucketValue);
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PercentilesBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket) PipelineAggregatorBuilders.percentilesBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket) PercentilesBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 70 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class PercentilesBucketIT method testNestedWithDecimal.

public void testNestedWithDecimal() throws Exception {
    double[] percent = { 99.9 };
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Terms.Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(percentilesBucket("percentile_histo_bucket", "histo>_count").percents(percent))).addAggregation(percentilesBucket("percentile_terms_bucket", "terms>percentile_histo_bucket[99.9]").percents(percent)).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    double[] values = new double[termsBuckets.size()];
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Histogram.Bucket> buckets = histo.getBuckets();
        double[] innerValues = new double[numValueBuckets];
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            innerValues[j] = bucket.getDocCount();
        }
        Arrays.sort(innerValues);
        PercentilesBucket percentilesBucketValue = termsBucket.getAggregations().get("percentile_histo_bucket");
        assertThat(percentilesBucketValue, notNullValue());
        assertThat(percentilesBucketValue.getName(), equalTo("percentile_histo_bucket"));
        assertPercentileBucket(innerValues, percentilesBucketValue);
        values[i] = percentilesBucketValue.percentile(99.9);
    }
    Arrays.sort(values);
    PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentile_terms_bucket");
    assertThat(percentilesBucketValue, notNullValue());
    assertThat(percentilesBucketValue.getName(), equalTo("percentile_terms_bucket"));
    for (Double p : percent) {
        double expected = values[(int) ((p / 100) * values.length)];
        assertThat(percentilesBucketValue.percentile(p), equalTo(expected));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PercentilesBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) PipelineAggregatorBuilders.percentilesBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket) PercentilesBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket)

Aggregations

Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 SearchResponse (org.elasticsearch.action.search.SearchResponse)208 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)202 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)121 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)43 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)37 ArrayList (java.util.ArrayList)36 DateTime (org.joda.time.DateTime)34 Script (org.elasticsearch.script.Script)32 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)11 HashMap (java.util.HashMap)10 PipelineAggregatorBuilders.bucketScript (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript)10 PipelineAggregationHelperTests (org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 Range (org.elasticsearch.search.aggregations.bucket.range.Range)6 PercentilesBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket)6 ExtendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket)6