Search in sources :

Example 46 with Bucket

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

the class BucketSelectorIT method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws Exception {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(_value0) ? false : (_value0 + _value1 > 100)", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx", "idx_unmapped").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(interval).subAggregation(sum("field2Sum").field(FIELD_2_NAME)).subAggregation(sum("field3Sum").field(FIELD_3_NAME)).subAggregation(bucketSelector("bucketSelector", script, "field2Sum", "field3Sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        Sum field2Sum = bucket.getAggregations().get("field2Sum");
        assertThat(field2Sum, notNullValue());
        double field2SumValue = field2Sum.getValue();
        Sum field3Sum = bucket.getAggregations().get("field3Sum");
        assertThat(field3Sum, notNullValue());
        double field3SumValue = field3Sum.getValue();
        assertThat(field2SumValue + field3SumValue, greaterThan(100.0));
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 47 with Bucket

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

the class BucketSelectorIT method testStoredScript.

public void testStoredScript() {
    assertAcked(client().admin().cluster().preparePutStoredScript().setId("my_script").setLang(CustomScriptPlugin.NAME).setContent(new BytesArray("{ \"script\": \"Double.isNaN(_value0) ? false : (_value0 + _value1 > 100)\" }"), XContentType.JSON));
    Script script = new Script(ScriptType.STORED, CustomScriptPlugin.NAME, "my_script", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(interval).subAggregation(sum("field2Sum").field(FIELD_2_NAME)).subAggregation(sum("field3Sum").field(FIELD_3_NAME)).subAggregation(bucketSelector("bucketSelector", script, "field2Sum", "field3Sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        Sum field2Sum = bucket.getAggregations().get("field2Sum");
        assertThat(field2Sum, notNullValue());
        double field2SumValue = field2Sum.getValue();
        Sum field3Sum = bucket.getAggregations().get("field3Sum");
        assertThat(field3Sum, notNullValue());
        double field3SumValue = field3Sum.getValue();
        assertThat(field2SumValue + field3SumValue, greaterThan(100.0));
    }
}
Also used : Script(org.elasticsearch.script.Script) BytesArray(org.elasticsearch.common.bytes.BytesArray) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 48 with Bucket

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

the class CumulativeSumIT method testDocCount.

public void testDocCount() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(cumulativeSum("cumulative_sum", "_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));
    double sum = 0;
    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]));
        sum += bucket.getDocCount();
        InternalSimpleValue cumulativeSumValue = bucket.getAggregations().get("cumulative_sum");
        assertThat(cumulativeSumValue, notNullValue());
        assertThat(cumulativeSumValue.getName(), equalTo("cumulative_sum"));
        assertThat(cumulativeSumValue.value(), equalTo(sum));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 49 with Bucket

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

the class DateDerivativeIT method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "idx_unmapped").addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.MONTH).minDocCount(0).subAggregation(derivative("deriv", "_count"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(3));
    DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(true));
    SimpleValue docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, nullValue());
    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), equalTo(1.0));
    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), equalTo(1.0));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 50 with Bucket

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

the class BucketSelectorIT method testInlineScriptWithParams.

public void testInlineScriptWithParams() {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(_value0) ? false : (_value0 + _value1 > threshold)", Collections.singletonMap("threshold", 100));
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(interval).subAggregation(sum("field2Sum").field(FIELD_2_NAME)).subAggregation(sum("field3Sum").field(FIELD_3_NAME)).subAggregation(bucketSelector("bucketSelector", script, "field2Sum", "field3Sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        Sum field2Sum = bucket.getAggregations().get("field2Sum");
        assertThat(field2Sum, notNullValue());
        double field2SumValue = field2Sum.getValue();
        Sum field3Sum = bucket.getAggregations().get("field3Sum");
        assertThat(field3Sum, notNullValue());
        double field3SumValue = field3Sum.getValue();
        assertThat(field2SumValue + field3SumValue, greaterThan(100.0));
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)127 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)127 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)127 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)121 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)38 ArrayList (java.util.ArrayList)31 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)30 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)30 DateTime (org.joda.time.DateTime)27 Script (org.elasticsearch.script.Script)19 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13 PipelineAggregationHelperTests (org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 HashMap (java.util.HashMap)7 ExtendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket)7 LongHashSet (com.carrotsearch.hppc.LongHashSet)6 StatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket)6 PipelineAggregatorBuilders.avgBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.avgBucket)5 PipelineAggregatorBuilders.extendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.extendedStatsBucket)5 PipelineAggregatorBuilders.maxBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)5