Search in sources :

Example 16 with Histogram

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

the class DateHistogramOffsetIT method testSingleValueWithOffsetMinDocCount.

/**
     * Set offset so day buckets start at 6am. Index first 12 hours for two days, with one day gap.
     */
public void testSingleValueWithOffsetMinDocCount() throws Exception {
    prepareIndex(date("2014-03-11T00:00:00+00:00"), 12, 1, 0);
    prepareIndex(date("2014-03-14T00:00:00+00:00"), 12, 1, 13);
    SearchResponse response = client().prepareSearch("idx2").setQuery(matchAllQuery()).addAggregation(dateHistogram("date_histo").field("date").offset("6h").minDocCount(0).format(DATE_FORMAT).dateHistogramInterval(DateHistogramInterval.DAY)).execute().actionGet();
    assertThat(response.getHits().getTotalHits(), equalTo(24L));
    Histogram histo = response.getAggregations().get("date_histo");
    List<? extends Histogram.Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(5));
    checkBucketFor(buckets.get(0), new DateTime(2014, 3, 10, 6, 0, DateTimeZone.UTC), 6L);
    checkBucketFor(buckets.get(1), new DateTime(2014, 3, 11, 6, 0, DateTimeZone.UTC), 6L);
    checkBucketFor(buckets.get(2), new DateTime(2014, 3, 12, 6, 0, DateTimeZone.UTC), 0L);
    checkBucketFor(buckets.get(3), new DateTime(2014, 3, 13, 6, 0, DateTimeZone.UTC), 6L);
    checkBucketFor(buckets.get(4), new DateTime(2014, 3, 14, 6, 0, DateTimeZone.UTC), 6L);
}
Also used : AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 17 with Histogram

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

the class DateHistogramIT method testSingleValued_timeZone_epoch.

public void testSingleValued_timeZone_epoch() throws Exception {
    String format = randomBoolean() ? "epoch_millis" : "epoch_second";
    int millisDivider = format.equals("epoch_millis") ? 1 : 1000;
    if (randomBoolean()) {
        format = format + "||date_optional_time";
    }
    DateTimeZone tz = DateTimeZone.forID("+01:00");
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(1).timeZone(tz).format(format)).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(6));
    List<DateTime> expectedKeys = new ArrayList<>();
    expectedKeys.add(new DateTime(2012, 1, 1, 23, 0, DateTimeZone.UTC));
    expectedKeys.add(new DateTime(2012, 2, 1, 23, 0, DateTimeZone.UTC));
    expectedKeys.add(new DateTime(2012, 2, 14, 23, 0, DateTimeZone.UTC));
    expectedKeys.add(new DateTime(2012, 3, 1, 23, 0, DateTimeZone.UTC));
    expectedKeys.add(new DateTime(2012, 3, 14, 23, 0, DateTimeZone.UTC));
    expectedKeys.add(new DateTime(2012, 3, 22, 23, 0, DateTimeZone.UTC));
    Iterator<DateTime> keyIterator = expectedKeys.iterator();
    for (Histogram.Bucket bucket : buckets) {
        assertThat(bucket, notNullValue());
        DateTime expectedKey = keyIterator.next();
        assertThat(bucket.getKeyAsString(), equalTo(Long.toString(expectedKey.getMillis() / millisDivider)));
        assertThat(((DateTime) bucket.getKey()), equalTo(expectedKey));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with Histogram

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

the class DateHistogramIT method testIssue6965.

public void testIssue6965() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("date").timeZone(DateTimeZone.forID("+01:00")).dateHistogramInterval(DateHistogramInterval.MONTH).minDocCount(0)).execute().actionGet();
    assertSearchResponse(response);
    DateTimeZone tz = DateTimeZone.forID("+01:00");
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(3));
    DateTime key = new DateTime(2011, 12, 31, 23, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key, tz)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    key = new DateTime(2012, 1, 31, 23, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key, tz)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    key = new DateTime(2012, 2, 29, 23, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key, tz)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with Histogram

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

the class DateHistogramIT method testScriptMultiValued.

public void testScriptMultiValued() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("fieldname", "dates");
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").script(new Script(ScriptType.INLINE, "native", DateScriptMocks.ExtractFieldScript.NAME, params)).dateHistogramInterval(DateHistogramInterval.MONTH)).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(4));
    DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(5L));
    key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(3);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with Histogram

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

the class DateHistogramIT method testSingleValuedField.

public void testSingleValuedField() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.MONTH)).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(3));
    DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) DateTime(org.joda.time.DateTime) 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