Search in sources :

Example 21 with Bucket

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

the class HistogramIT method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "idx_unmapped").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval)).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));
    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]));
    }
}
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 22 with Bucket

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

the class HistogramIT method testSingleValuedFieldWithValueScript.

public void testSingleValuedFieldWithValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value + 1", emptyMap())).interval(interval)).execute().actionGet();
    assertSearchResponse(response);
    final int numBuckets = (numDocs + 1) / interval - 2 / interval + 1;
    final long[] counts = new long[(numDocs + 1) / interval + 1];
    for (int i = 0; i < numDocs; ++i) {
        ++counts[(i + 2) / interval];
    }
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(numBuckets));
    for (int i = 0; i < numBuckets; i++) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        int key = ((2 / interval) + i) * interval;
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) key));
        assertThat(bucket.getDocCount(), equalTo(counts[key / interval]));
    }
}
Also used : Script(org.elasticsearch.script.Script) 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 23 with Bucket

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

the class HistogramIT method testDecimalIntervalAndOffset.

public void testDecimalIntervalAndOffset() throws Exception {
    assertAcked(prepareCreate("decimal_values").addMapping("type", "d", "type=float").get());
    indexRandom(true, client().prepareIndex("decimal_values", "type", "1").setSource("d", -0.6), client().prepareIndex("decimal_values", "type", "2").setSource("d", 0.1));
    SearchResponse r = client().prepareSearch("decimal_values").addAggregation(histogram("histo").field("d").interval(0.7).offset(0.05)).get();
    assertSearchResponse(r);
    Histogram histogram = r.getAggregations().get("histo");
    List<Bucket> buckets = histogram.getBuckets();
    assertEquals(2, buckets.size());
    assertEquals(-0.65, (double) buckets.get(0).getKey(), 0.01d);
    assertEquals(1, buckets.get(0).getDocCount());
    assertEquals(0.05, (double) buckets.get(1).getKey(), 0.01d);
    assertEquals(1, buckets.get(1).getDocCount());
}
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 24 with Bucket

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

the class HistogramIT method singleValuedField_withOffset.

public void singleValuedField_withOffset() throws Exception {
    int interval1 = 10;
    int offset = 5;
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval1).offset(offset)).execute().actionGet();
    // from setup we have between 6 and 20 documents, each with value 1 in test field
    int expectedNumberOfBuckets = (offset >= (numDocs % interval + 1)) ? numValueBuckets : numValueBuckets + 1;
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(expectedNumberOfBuckets));
    // first bucket should start at -5, contain 4 documents
    Histogram.Bucket bucket = histo.getBuckets().get(0);
    assertThat(bucket, notNullValue());
    assertThat(((Number) bucket.getKey()).longValue(), equalTo(-5L));
    assertThat(bucket.getDocCount(), equalTo(4L));
    // last bucket should have (numDocs % interval + 1) docs
    bucket = histo.getBuckets().get(0);
    assertThat(bucket, notNullValue());
    assertThat(((Number) bucket.getKey()).longValue(), equalTo(numDocs % interval1 + 5L));
    assertThat(bucket.getDocCount(), equalTo((numDocs % interval) + 1L));
}
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 25 with Bucket

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

the class HistogramIT method testScriptMultiValued.

public void testScriptMultiValued() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['l_values']", emptyMap())).interval(interval)).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(numValuesBuckets));
    for (int i = 0; i < numValuesBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
        assertThat(bucket.getDocCount(), equalTo(valuesCounts[i]));
    }
}
Also used : Script(org.elasticsearch.script.Script) 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)

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