Search in sources :

Example 6 with Histogram

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

the class DateHistogramIT method testSingleValuedFieldWithValueScript.

public void testSingleValuedFieldWithValueScript() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("fieldname", "date");
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("date").script(new Script(ScriptType.INLINE, "native", DateScriptMocks.PlusOneMonthScript.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(3));
    DateTime key = new DateTime(2012, 2, 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, 3, 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, 4, 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 : 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 7 with Histogram

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

the class DateHistogramIT method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "idx_unmapped").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)

Example 8 with Histogram

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

the class MissingValueIT method testDateHistogram.

public void testDateHistogram() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("my_histogram").field("date").dateHistogramInterval(DateHistogramInterval.YEAR).missing("2014-05-07")).get();
    assertSearchResponse(response);
    Histogram histogram = response.getAggregations().get("my_histogram");
    assertEquals(2, histogram.getBuckets().size());
    assertEquals("2014-01-01T00:00:00.000Z", histogram.getBuckets().get(0).getKeyAsString());
    assertEquals(1, histogram.getBuckets().get(0).getDocCount());
    assertEquals("2015-01-01T00:00:00.000Z", histogram.getBuckets().get(1).getKeyAsString());
    assertEquals(1, histogram.getBuckets().get(1).getDocCount());
    response = client().prepareSearch("idx").addAggregation(dateHistogram("my_histogram").field("date").dateHistogramInterval(DateHistogramInterval.YEAR).missing("2015-05-07")).get();
    assertSearchResponse(response);
    histogram = response.getAggregations().get("my_histogram");
    assertEquals(1, histogram.getBuckets().size());
    assertEquals("2015-01-01T00:00:00.000Z", histogram.getBuckets().get(0).getKeyAsString());
    assertEquals(2, histogram.getBuckets().get(0).getDocCount());
}
Also used : AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with Histogram

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

the class AdjacencyMatrixIT method testAsSubAggregation.

public void testAsSubAggregation() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field("value").interval(2L).subAggregation(adjacencyMatrix("matrix", newMap("all", matchAllQuery())))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getBuckets().size(), greaterThanOrEqualTo(1));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        AdjacencyMatrix matrix = bucket.getAggregations().get("matrix");
        assertThat(matrix, notNullValue());
        assertThat(matrix.getBuckets().size(), equalTo(1));
        AdjacencyMatrix.Bucket filterBucket = matrix.getBuckets().get(0);
        assertEquals(bucket.getDocCount(), filterBucket.getDocCount());
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) AdjacencyMatrix(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 10 with Histogram

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

the class FilterIT method testAsSubAggregation.

public void testAsSubAggregation() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field("value").interval(2L).subAggregation(filter("filter", matchAllQuery()))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getBuckets().size(), greaterThanOrEqualTo(1));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        Filter filter = bucket.getAggregations().get("filter");
        assertThat(filter, notNullValue());
        assertEquals(bucket.getDocCount(), filter.getDocCount());
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) 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