Search in sources :

Example 86 with Bucket

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

the class HistogramIT method testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc.

public void testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.aggregation("stats.sum", false)).subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))).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 visited = new LongHashSet();
    double previousSum = Double.POSITIVE_INFINITY;
    // 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(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertTrue(visited.add(key));
        int b = (int) (key / interval);
        assertThat(bucket.getDocCount(), equalTo(valueCounts[b]));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
        Stats stats = bucket.getAggregations().get("stats");
        assertThat(stats, notNullValue());
        long s = 0;
        for (int j = 0; j < numDocs; ++j) {
            if ((j + 1) / interval == b) {
                s += j + 1;
            }
        }
        assertThat(stats.getSum(), equalTo((double) s));
        assertThat(stats.getSum(), lessThanOrEqualTo(previousSum));
        previousSum = s;
    }
}
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) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 87 with Bucket

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

the class HistogramIT method testEmptyWithExtendedBounds.

public void testEmptyWithExtendedBounds() 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 = (int) ((boundsMaxKey - boundsMinKey) / interval) + 1;
    long[] extendedValueCounts = new long[valueCounts.length + addedBucketsLeft + addedBucketsRight];
    System.arraycopy(valueCounts, 0, extendedValueCounts, addedBucketsLeft, valueCounts.length);
    SearchResponse response = null;
    try {
        response = client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("foo", "bar")).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 = boundsMinKey;
    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(0L));
        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 88 with Bucket

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

the class HistogramIT method testSingleValuedFieldOrderedByCountAsc.

public void testSingleValuedFieldOrderedByCountAsc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.COUNT_ASC)).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.MIN_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(), greaterThanOrEqualTo(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 89 with Bucket

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

the class HistogramIT method testSingleValuedFieldOrderedBySubAggregationDescDeepOrderPath.

public void testSingleValuedFieldOrderedBySubAggregationDescDeepOrderPath() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.aggregation("filter>max", asc)).subAggregation(filter("filter", matchAllQuery()).subAggregation(max("max").field(SINGLE_VALUED_FIELD_NAME)))).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 visited = new LongHashSet();
    double prevMax = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    // 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(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertTrue(visited.add(key));
        int b = (int) (key / interval);
        assertThat(bucket.getDocCount(), equalTo(valueCounts[b]));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
        Filter filter = bucket.getAggregations().get("filter");
        assertThat(filter, notNullValue());
        assertThat(bucket.getDocCount(), equalTo(filter.getDocCount()));
        Max max = filter.getAggregations().get("max");
        assertThat(max, Matchers.notNullValue());
        assertThat(max.getValue(), asc ? greaterThanOrEqualTo(prevMax) : lessThanOrEqualTo(prevMax));
        prevMax = max.getValue();
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Max(org.elasticsearch.search.aggregations.metrics.max.Max) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) LongHashSet(com.carrotsearch.hppc.LongHashSet) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter)

Example 90 with Bucket

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

the class SerialDiffIT method testBasicDiff.

public void testBasicDiff() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(histogram("histo").field(INTERVAL_FIELD).interval(interval).extendedBounds(0L, (long) (interval * (numBuckets - 1))).subAggregation(metric).subAggregation(diff("diff_counts", "_count").lag(lag).gapPolicy(gapPolicy)).subAggregation(diff("diff_values", "the_metric").lag(lag).gapPolicy(gapPolicy))).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(MetricTarget.COUNT.toString());
    List<Double> expectedValues = testValues.get(MetricTarget.VALUE.toString());
    Iterator<? extends Histogram.Bucket> actualIter = buckets.iterator();
    Iterator<PipelineAggregationHelperTests.MockBucket> expectedBucketIter = mockHisto.iterator();
    Iterator<Double> expectedCountsIter = expectedCounts.iterator();
    Iterator<Double> expectedValuesIter = expectedValues.iterator();
    while (actualIter.hasNext()) {
        assertValidIterators(expectedBucketIter, expectedCountsIter, expectedValuesIter);
        Histogram.Bucket actual = actualIter.next();
        PipelineAggregationHelperTests.MockBucket expected = expectedBucketIter.next();
        Double expectedCount = expectedCountsIter.next();
        Double expectedValue = expectedValuesIter.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));
        assertBucketContents(actual, expectedCount, expectedValue);
    }
}
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) 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