Search in sources :

Example 1 with PercentilesBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket 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 2 with PercentilesBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket 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)

Example 3 with PercentilesBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket in project elasticsearch by elastic.

the class PercentilesBucketIT method testDocCountAsSubAgg.

public void testDocCountAsSubAgg() 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("percentiles_bucket", "histo>_count").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));
    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[] values = 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));
            values[j] = bucket.getDocCount();
        }
        Arrays.sort(values);
        PercentilesBucket percentilesBucketValue = termsBucket.getAggregations().get("percentiles_bucket");
        assertThat(percentilesBucketValue, notNullValue());
        assertThat(percentilesBucketValue.getName(), equalTo("percentiles_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 4 with PercentilesBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket in project elasticsearch by elastic.

the class PercentilesBucketIT method testWrongPercents.

public void testWrongPercents() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").includeExclude(new IncludeExclude(null, "tag.*")).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).addAggregation(percentilesBucket("percentiles_bucket", "terms>sum").percents(PERCENTS)).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(0));
    PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentiles_bucket");
    assertThat(percentilesBucketValue, notNullValue());
    assertThat(percentilesBucketValue.getName(), equalTo("percentiles_bucket"));
    try {
        percentilesBucketValue.percentile(2.0);
        fail("2.0 was not a valid percent, should have thrown exception");
    } catch (IllegalArgumentException exception) {
    // All good
    }
}
Also used : 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) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with PercentilesBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket in project elasticsearch by elastic.

the class PercentilesBucketIT method testDocCountopLevel.

public void testDocCountopLevel() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).addAggregation(percentilesBucket("percentiles_bucket", "histo>_count").percents(PERCENTS)).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Histogram.Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(numValueBuckets));
    double[] values = new double[numValueBuckets];
    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]));
        values[i] = bucket.getDocCount();
    }
    Arrays.sort(values);
    PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentiles_bucket");
    assertThat(percentilesBucketValue, notNullValue());
    assertThat(percentilesBucketValue.getName(), equalTo("percentiles_bucket"));
    assertPercentileBucket(PERCENTS, values, percentilesBucketValue);
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PercentilesBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)10 PercentilesBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket)10 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)9 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)6 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)4 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)2 ArrayList (java.util.ArrayList)1