Search in sources :

Example 6 with InternalBucketMetricValue

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

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

the class MinBucketIT method testNoBuckets.

public void testNoBuckets() 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(minBucket("min_bucket", "terms>sum")).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));
    InternalBucketMetricValue minBucketValue = response.getAggregations().get("min_bucket");
    assertThat(minBucketValue, notNullValue());
    assertThat(minBucketValue.getName(), equalTo("min_bucket"));
    assertThat(minBucketValue.value(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(minBucketValue.keys(), equalTo(new String[0]));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.minBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.minBucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with InternalBucketMetricValue

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue 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 9 with InternalBucketMetricValue

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue 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 10 with InternalBucketMetricValue

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

the class MetaDataIT method testMetaDataSetOnAggregationResult.

public void testMetaDataSetOnAggregationResult() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("idx").addMapping("type", "name", "type=keyword").get());
    IndexRequestBuilder[] builders = new IndexRequestBuilder[randomInt(30)];
    for (int i = 0; i < builders.length; i++) {
        String name = "name_" + randomIntBetween(1, 10);
        builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).field("value", randomInt()).endObject());
    }
    indexRandom(true, builders);
    ensureSearchable();
    final Map<String, Object> nestedMetaData = new HashMap<String, Object>() {

        {
            put("nested", "value");
        }
    };
    Map<String, Object> metaData = new HashMap<String, Object>() {

        {
            put("key", "value");
            put("numeric", 1.2);
            put("bool", true);
            put("complex", nestedMetaData);
        }
    };
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("the_terms").setMetaData(metaData).field("name").subAggregation(sum("the_sum").setMetaData(metaData).field("value"))).addAggregation(maxBucket("the_max_bucket", "the_terms>the_sum").setMetaData(metaData)).execute().actionGet();
    assertSearchResponse(response);
    Aggregations aggs = response.getAggregations();
    assertNotNull(aggs);
    Terms terms = aggs.get("the_terms");
    assertNotNull(terms);
    assertMetaData(terms.getMetaData());
    List<? extends Terms.Bucket> buckets = terms.getBuckets();
    for (Terms.Bucket bucket : buckets) {
        Aggregations subAggs = bucket.getAggregations();
        assertNotNull(subAggs);
        Sum sum = subAggs.get("the_sum");
        assertNotNull(sum);
        assertMetaData(sum.getMetaData());
    }
    InternalBucketMetricValue maxBucket = aggs.get("the_max_bucket");
    assertNotNull(maxBucket);
    assertMetaData(maxBucket.getMetaData());
}
Also used : HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) 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) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)17 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)17 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)15 ArrayList (java.util.ArrayList)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)13 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)11 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)8 PipelineAggregatorBuilders.maxBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)6 PipelineAggregatorBuilders.minBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.minBucket)6 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1