Search in sources :

Example 86 with Histogram

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

the class BucketScriptIT method testStoredScript.

public void testStoredScript() {
    assertAcked(client().admin().cluster().preparePutStoredScript().setId("my_script").setLang(CustomScriptPlugin.NAME).setContent(new BytesArray("{ \"script\": \"my_script\" }"), XContentType.JSON));
    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(sum("field4Sum").field(FIELD_4_NAME)).subAggregation(bucketScript("seriesArithmetic", new Script(ScriptType.STORED, CustomScriptPlugin.NAME, "my_script", Collections.emptyMap()), "field2Sum", "field3Sum", "field4Sum"))).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();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        if (bucket.getDocCount() == 0) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
            assertThat(seriesArithmetic, nullValue());
        } else {
            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();
            Sum field4Sum = bucket.getAggregations().get("field4Sum");
            assertThat(field4Sum, notNullValue());
            double field4SumValue = field4Sum.getValue();
            SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue));
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) BytesArray(org.elasticsearch.common.bytes.BytesArray) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 87 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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)

Example 88 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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 89 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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 90 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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)

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