Search in sources :

Example 16 with Bucket

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

the class HistogramIT method testSingleValuedFieldWithExtendedBounds.

public void testSingleValuedFieldWithExtendedBounds() throws Exception {
    int lastDataBucketKey = (numValueBuckets - 1) * interval;
    // randomizing the number of buckets on the min bound
    // (can sometimes fall within the data range, but more frequently will fall before the data range)
    int addedBucketsLeft = randomIntBetween(0, numValueBuckets);
    long boundsMinKey = addedBucketsLeft * interval;
    if (frequently()) {
        boundsMinKey = -boundsMinKey;
    } else {
        addedBucketsLeft = 0;
    }
    long boundsMin = boundsMinKey + randomIntBetween(0, interval - 1);
    // randomizing the number of buckets on the max bound
    // (can sometimes fall within the data range, but more frequently will fall after the data range)
    int addedBucketsRight = randomIntBetween(0, numValueBuckets);
    long boundsMaxKeyDelta = addedBucketsRight * interval;
    if (rarely()) {
        addedBucketsRight = 0;
        boundsMaxKeyDelta = -boundsMaxKeyDelta;
    }
    long boundsMaxKey = lastDataBucketKey + boundsMaxKeyDelta;
    long boundsMax = boundsMaxKey + randomIntBetween(0, interval - 1);
    // it could be that the random bounds.min we chose ended up greater than bounds.max - this should cause an
    // error
    boolean invalidBoundsError = boundsMin > boundsMax;
    // constructing the newly expected bucket list
    int bucketsCount = numValueBuckets + addedBucketsLeft + addedBucketsRight;
    long[] extendedValueCounts = new long[bucketsCount];
    System.arraycopy(valueCounts, 0, extendedValueCounts, addedBucketsLeft, valueCounts.length);
    SearchResponse response = null;
    try {
        response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).minDocCount(0).extendedBounds(boundsMin, boundsMax)).execute().actionGet();
        if (invalidBoundsError) {
            fail("Expected an exception to be thrown when bounds.min is greater than bounds.max");
            return;
        }
    } catch (IllegalArgumentException e) {
        if (invalidBoundsError) {
            // expected
            return;
        } else {
            throw e;
        }
    }
    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(bucketsCount));
    long key = Math.min(boundsMinKey, 0);
    for (int i = 0; i < bucketsCount; i++) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo(key));
        assertThat(bucket.getDocCount(), equalTo(extendedValueCounts[i]));
        key += interval;
    }
}
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 17 with Bucket

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

the class HistogramIT method testSingleValuedFieldOrderedByCountDesc.

public void testSingleValuedFieldOrderedByCountDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.COUNT_DESC)).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
    LongHashSet buckets = new LongHashSet();
    // TODO: use diamond once JI-9019884 is fixed
    List<Histogram.Bucket> histoBuckets = new ArrayList<>(histo.getBuckets());
    long previousCount = Long.MAX_VALUE;
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = histoBuckets.get(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertEquals(0, key % interval);
        assertTrue(buckets.add(key));
        assertThat(bucket.getDocCount(), equalTo(valueCounts[(int) (key / interval)]));
        assertThat(bucket.getDocCount(), lessThanOrEqualTo(previousCount));
        previousCount = bucket.getDocCount();
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with Bucket

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

the class HistogramIT method testsingleValuedFieldOrderedByKeyDesc.

public void testsingleValuedFieldOrderedByKeyDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.KEY_DESC)).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
    // TODO: use diamond once JI-9019884 is fixed
    List<Histogram.Bucket> buckets = new ArrayList<>(histo.getBuckets());
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(numValueBuckets - i - 1);
        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) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with Bucket

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

the class HistogramIT method testMultiValuedFieldWithValueScript.

public void testMultiValuedFieldWithValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(MULTI_VALUED_FIELD_NAME).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value + 1", emptyMap())).interval(interval)).execute().actionGet();
    assertSearchResponse(response);
    final int numBuckets = (numDocs + 2) / interval - 2 / interval + 1;
    final long[] counts = new long[(numDocs + 2) / interval + 1];
    for (int i = 0; i < numDocs; ++i) {
        final int bucket1 = (i + 2) / interval;
        final int bucket2 = (i + 3) / interval;
        ++counts[bucket1];
        if (bucket1 != bucket2) {
            ++counts[bucket2];
        }
    }
    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 20 with Bucket

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

the class HistogramIT method testScriptSingleValue.

public void testScriptSingleValue() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['l_value'].value", 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(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 : 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