Search in sources :

Example 1 with Histogram

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

the class CombiIT method testSubAggregationForTopAggregationOnUnmappedField.

/**
     * Some top aggs (eg. date_/histogram) that are executed on unmapped fields, will generate an estimate count of buckets - zero.
     * when the sub aggregator is then created, it will take this estimation into account. This used to cause
     * and an ArrayIndexOutOfBoundsException...
     */
public void testSubAggregationForTopAggregationOnUnmappedField() throws Exception {
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("name").field("type", "keyword").endObject().startObject("value").field("type", "integer").endObject().endObject().endObject().endObject()).execute().actionGet();
    ensureSearchable("idx");
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse searchResponse = client().prepareSearch("idx").addAggregation(histogram("values").field("value1").interval(1).subAggregation(terms("names").field("name").collectMode(aggCollectionMode))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), Matchers.equalTo(0L));
    Histogram values = searchResponse.getAggregations().get("values");
    assertThat(values, notNullValue());
    assertThat(values.getBuckets().isEmpty(), is(true));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Histogram

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

the class EquivalenceIT method testDuelTermsHistogram.

// Duel between histograms and scripted terms
public void testDuelTermsHistogram() throws Exception {
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("num").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final int maxNumTerms = randomIntBetween(10, 2000);
    final int interval = randomIntBetween(1, 100);
    final Integer[] values = new Integer[maxNumTerms];
    for (int i = 0; i < values.length; ++i) {
        values[i] = randomInt(maxNumTerms * 3) - maxNumTerms;
    }
    for (int i = 0; i < numDocs; ++i) {
        XContentBuilder source = jsonBuilder().startObject().field("num", randomDouble()).startArray("values");
        final int numValues = randomInt(4);
        for (int j = 0; j < numValues; ++j) {
            source = source.value(randomFrom(values));
        }
        source = source.endArray().endObject();
        client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
    }
    assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().get());
    Map<String, Object> params = new HashMap<>();
    params.put("interval", interval);
    SearchResponse resp = client().prepareSearch("idx").addAggregation(terms("terms").field("values").collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "floor(_value / interval)", params)).size(maxNumTerms)).addAggregation(histogram("histo").field("values").interval(interval).minDocCount(1)).execute().actionGet();
    assertSearchResponse(resp);
    Terms terms = resp.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    Histogram histo = resp.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(terms.getBuckets().size(), equalTo(histo.getBuckets().size()));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        final double key = ((Number) bucket.getKey()).doubleValue() / interval;
        final Terms.Bucket termsBucket = terms.getBucketByKey(String.valueOf(key));
        assertEquals(bucket.getDocCount(), termsBucket.getDocCount());
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 3 with Histogram

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

the class MissingValueIT method testHistogram.

public void testHistogram() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("my_histogram").field("long").interval(5).missing(7)).get();
    assertSearchResponse(response);
    Histogram histogram = response.getAggregations().get("my_histogram");
    assertEquals(2, histogram.getBuckets().size());
    assertEquals(0d, histogram.getBuckets().get(0).getKey());
    assertEquals(1, histogram.getBuckets().get(0).getDocCount());
    assertEquals(5d, histogram.getBuckets().get(1).getKey());
    assertEquals(1, histogram.getBuckets().get(1).getDocCount());
    response = client().prepareSearch("idx").addAggregation(histogram("my_histogram").field("long").interval(5).missing(3)).get();
    assertSearchResponse(response);
    histogram = response.getAggregations().get("my_histogram");
    assertEquals(1, histogram.getBuckets().size());
    assertEquals(0d, histogram.getBuckets().get(0).getKey());
    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 4 with Histogram

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

the class DateHistogramIT method testSingleValueWithTimeZone.

public void testSingleValueWithTimeZone() throws Exception {
    prepareCreate("idx2").addMapping("type", "date", "type=date").execute().actionGet();
    IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
    DateTime date = date("2014-03-11T00:00:00+00:00");
    for (int i = 0; i < reqs.length; i++) {
        reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", date).endObject());
        date = date.plusHours(1);
    }
    indexRandom(true, reqs);
    SearchResponse response = client().prepareSearch("idx2").setQuery(matchAllQuery()).addAggregation(dateHistogram("date_histo").field("date").timeZone(DateTimeZone.forID("-02:00")).dateHistogramInterval(DateHistogramInterval.DAY).format("yyyy-MM-dd:HH-mm-ssZZ")).execute().actionGet();
    assertThat(response.getHits().getTotalHits(), equalTo(5L));
    Histogram histo = response.getAggregations().get("date_histo");
    List<? extends Histogram.Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(2));
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("2014-03-10:00-00-00-02:00"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("2014-03-11:00-00-00-02:00"));
    assertThat(bucket.getDocCount(), equalTo(3L));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) 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 5 with Histogram

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

the class DateHistogramIT method testScriptSingleValue.

/**
     * Jan 2
     * Feb 2
     * Feb 15
     * Mar 2
     * Mar 15
     * Mar 23
     */
public void testScriptSingleValue() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("fieldname", "date");
    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(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 : 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)

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