Search in sources :

Example 51 with Bucket

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

the class BucketSelectorIT method testInlineScriptNoBucketsPruned.

public void testInlineScriptNoBucketsPruned() {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(_value0) ? true : (_value0 < 10000)", 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, lessThan(10000.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 52 with Bucket

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

the class BucketSelectorIT method testInlineScript2.

public void testInlineScript2() {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(_value0) ? false : (_value0 < _value1)", 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(field3SumValue - field2SumValue, greaterThan(0.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 53 with Bucket

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

the class BucketSelectorIT method testInlineScriptNamedVars.

public void testInlineScriptNamedVars() {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(my_value1) ? false : (my_value1 + my_value2 > 100)", Collections.emptyMap());
    Map<String, String> bucketPathsMap = new HashMap<>();
    bucketPathsMap.put("my_value1", "field2Sum");
    bucketPathsMap.put("my_value2", "field3Sum");
    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", bucketPathsMap, script))).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) HashMap(java.util.HashMap) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 54 with Bucket

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

the class BucketSelectorIT method testEmptyBuckets.

public void testEmptyBuckets() {
    SearchResponse response = client().prepareSearch("idx_with_gaps").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(1).subAggregation(histogram("inner_histo").field(FIELD_1_NAME).interval(1).extendedBounds(1L, 4L).minDocCount(0).subAggregation(derivative("derivative", "_count").gapPolicy(GapPolicy.INSERT_ZEROS)))).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(3));
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("1.0"));
    Histogram innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    List<? extends Histogram.Bucket> innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
        Histogram.Bucket innerBucket = innerBuckets.get(i);
        if (i == 0) {
            assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
        } else {
            assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
        }
    }
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("2.0"));
    innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
        Histogram.Bucket innerBucket = innerBuckets.get(i);
        if (i == 0) {
            assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
        } else {
            assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
        }
    }
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("3.0"));
    innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
        Histogram.Bucket innerBucket = innerBuckets.get(i);
        if (i == 0) {
            assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
        } else {
            assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
        }
    }
}
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 55 with Bucket

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

the class MovAvgIT method testHoltWintersMinimization.

public void testHoltWintersMinimization() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(histogram("histo").field(INTERVAL_FIELD).interval(interval).extendedBounds(0L, (long) (interval * (numBuckets - 1))).subAggregation(metric).subAggregation(movingAvg("movavg_counts", "_count").window(windowSize).modelBuilder(new HoltWintersModel.HoltWintersModelBuilder().period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(true)).subAggregation(movingAvg("movavg_values", "the_metric").window(windowSize).modelBuilder(new HoltWintersModel.HoltWintersModelBuilder().period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(true))).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("Size of buckets array is not correct.", buckets.size(), equalTo(mockHisto.size()));
    List<Double> expectedCounts = testValues.get(MovAvgType.HOLT_WINTERS.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.HOLT_WINTERS.name() + "_" + MetricTarget.VALUE.name());
    Iterator<? extends Histogram.Bucket> actualIter = buckets.iterator();
    Iterator<PipelineAggregationHelperTests.MockBucket> expectedBucketIter = mockHisto.iterator();
    Iterator<Double> expectedCountsIter = expectedCounts.iterator();
    Iterator<Double> expectedValueIter = expectedValues.iterator();
    // The minimizer is stochastic, so just make sure all the values coming back aren't null
    while (actualIter.hasNext()) {
        Histogram.Bucket actual = actualIter.next();
        PipelineAggregationHelperTests.MockBucket expected = expectedBucketIter.next();
        Double expectedCount = expectedCountsIter.next();
        Double expectedValue = expectedValueIter.next();
        assertThat("keys do not match", ((Number) actual.getKey()).longValue(), equalTo(expected.key));
        assertThat("doc counts do not match", actual.getDocCount(), equalTo((long) expected.count));
        SimpleValue countMovAvg = actual.getAggregations().get("movavg_counts");
        SimpleValue valuesMovAvg = actual.getAggregations().get("movavg_values");
        if (expectedCount == null) {
            //this bucket wasn't supposed to have a value (empty, skipped, etc), so
            //movavg should be null too
            assertThat(countMovAvg, nullValue());
        } else {
            // Note that we don't compare against the mock values, since those are assuming
            // a non-minimized set of coefficients.  Just check for not-nullness
            assertThat(countMovAvg, notNullValue());
        }
        if (expectedValue == null) {
            //this bucket wasn't supposed to have a value (empty, skipped, etc), so
            //movavg should be null too
            assertThat(valuesMovAvg, nullValue());
        } else {
            // Note that we don't compare against the mock values, since those are assuming
            // a non-minimized set of coefficients.  Just check for not-nullness
            assertThat(valuesMovAvg, notNullValue());
        }
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PipelineAggregationHelperTests(org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) HoltWintersModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

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