Search in sources :

Example 36 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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 37 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram 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)

Example 38 with Histogram

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

the class MissingIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(missing("missing").field("value"))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Missing missing = bucket.getAggregations().get("missing");
    assertThat(missing, Matchers.notNullValue());
    assertThat(missing.getName(), equalTo("missing"));
    assertThat(missing.getDocCount(), is(0L));
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 39 with Histogram

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

the class NaNSortingIT method testLongHistogram.

public void testLongHistogram() {
    final boolean asc = randomBoolean();
    SubAggregation agg = randomFrom(SubAggregation.values());
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field("long_value").interval(randomIntBetween(1, 2)).subAggregation(agg.builder()).order(Histogram.Order.aggregation(agg.sortKey(), asc))).execute().actionGet();
    assertSearchResponse(response);
    final Histogram histo = response.getAggregations().get("histo");
    assertCorrectlySorted(histo, asc, agg);
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 40 with Histogram

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

the class ShardReduceIT method testGeoHashGrid.

public void testGeoHashGrid() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(geohashGrid("grid").field("location").subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
    assertSearchResponse(response);
    GeoHashGrid grid = response.getAggregations().get("grid");
    Histogram histo = grid.getBuckets().iterator().next().getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) 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