Пример #1
0
  @Test
  public void testEmptyAggregation() throws Exception {

    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1l)
                    .emptyBuckets(true)
                    .subAggregation(min("min")))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    Histogram.Bucket bucket = histo.getByKey(1l);
    assertThat(bucket, notNullValue());

    Min min = bucket.getAggregations().get("min");
    assertThat(min, notNullValue());
    assertThat(min.getName(), equalTo("min"));
    assertThat(min.getValue(), equalTo(Double.POSITIVE_INFINITY));
  }
Пример #2
0
  @Test
  public void emptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1l)
                    .minDocCount(0)
                    .subAggregation(filters("filters").filter("all", matchAllQuery())))
            .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());

    Filters filters = bucket.getAggregations().get("filters");
    assertThat(filters, notNullValue());
    Filters.Bucket all = filters.getBucketByKey("all");
    assertThat(all, Matchers.notNullValue());
    assertThat(all.getKeyAsString(), equalTo("all"));
    assertThat(all.getDocCount(), is(0l));
  }
Пример #3
0
  @Override
  @Test
  public void testEmptyAggregation() throws Exception {

    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1l)
                    .minDocCount(0)
                    .subAggregation(max("max")))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, notNullValue());

    Max max = bucket.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    assertThat(max.getValue(), equalTo(Double.NEGATIVE_INFINITY));
  }
  @Test
  public void singleValue_WithPositiveOffset() throws Exception {
    prepareIndex(date("2014-03-11T00:00:00+00:00"), 5, 1, 0);

    SearchResponse response =
        client()
            .prepareSearch("idx2")
            .setQuery(matchAllQuery())
            .addAggregation(
                dateHistogram("date_histo")
                    .field("date")
                    .offset("2h")
                    .format(DATE_FORMAT)
                    .interval(DateHistogramInterval.DAY))
            .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));

    checkBucketFor(buckets.get(0), new DateTime(2014, 3, 10, 2, 0, DateTimeZone.UTC), 2l);
    checkBucketFor(buckets.get(1), new DateTime(2014, 3, 11, 2, 0, DateTimeZone.UTC), 3l);
  }
Пример #5
0
  public void testMultiValuedField() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                dateHistogram("histo")
                    .field("dates")
                    .dateHistogramInterval(DateHistogramInterval.MONTH)
                    .minDocCount(0)
                    .subAggregation(derivative("deriv", "_count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.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((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(true));
    SimpleValue docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, nullValue());

    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), equalTo(2.0));

    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(5L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), equalTo(2.0));

    key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(3);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), equalTo(-2.0));
  }
Пример #6
0
  public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1L)
                    .minDocCount(0)
                    .subAggregation(nested("nested", "nested")))
            .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());

    Nested nested = bucket.getAggregations().get("nested");
    assertThat(nested, Matchers.notNullValue());
    assertThat(nested.getName(), equalTo("nested"));
    assertThat(nested.getDocCount(), is(0L));
  }
Пример #7
0
  @Override
  public void testEmptyAggregation() throws Exception {

    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1l)
                    .minDocCount(0)
                    .subAggregation(avg("avg")))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, notNullValue());

    Avg avg = bucket.getAggregations().get("avg");
    assertThat(avg, notNullValue());
    assertThat(avg.getName(), equalTo("avg"));
    assertThat(Double.isNaN(avg.getValue()), is(true));
  }
  void assertBasicAggregationWorks(String indexName) {
    // histogram on a long
    SearchResponse searchRsp =
        client()
            .prepareSearch(indexName)
            .addAggregation(AggregationBuilders.histogram("histo").field("long_sort").interval(10))
            .get();
    ElasticsearchAssertions.assertSearchResponse(searchRsp);
    Histogram histo = searchRsp.getAggregations().get("histo");
    assertNotNull(histo);
    long totalCount = 0;
    for (Histogram.Bucket bucket : histo.getBuckets()) {
      totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());

    // terms on a boolean
    searchRsp =
        client()
            .prepareSearch(indexName)
            .addAggregation(AggregationBuilders.terms("bool_terms").field("bool"))
            .get();
    Terms terms = searchRsp.getAggregations().get("bool_terms");
    totalCount = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
      totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());
  }
Пример #9
0
  @Override
  public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1L)
                    .minDocCount(0)
                    .subAggregation(stats("stats").field("value")))
            .execute()
            .actionGet();

    assertShardExecutionState(searchResponse, 0);

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, notNullValue());

    Stats stats = bucket.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("stats"));
    assertThat(stats.getCount(), equalTo(0L));
    assertThat(stats.getSum(), equalTo(0.0));
    assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY));
    assertThat(Double.isNaN(stats.getAvg()), is(true));
  }
  public void testOrderBySubAggregation() {
    boolean asc = randomBoolean();
    SearchResponse searchResponse =
        client()
            .prepareSearch("idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(2L)
                    .subAggregation(
                        randomCompression(
                            percentiles("percentiles").field("value").percentiles(99)))
                    .order(Order.aggregation("percentiles", "99", asc)))
            .execute()
            .actionGet();

    assertHitCount(searchResponse, 10);

    Histogram histo = searchResponse.getAggregations().get("histo");
    double previous = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    for (Histogram.Bucket bucket : histo.getBuckets()) {
      Percentiles percentiles = bucket.getAggregations().get("percentiles");
      double p99 = percentiles.percentile(99);
      if (asc) {
        assertThat(p99, greaterThanOrEqualTo(previous));
      } else {
        assertThat(p99, lessThanOrEqualTo(previous));
      }
      previous = p99;
    }
  }
  @Override
  public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field("value")
                    .interval(1L)
                    .minDocCount(0)
                    .subAggregation(
                        randomCompression(percentiles("percentiles").field("value"))
                            .percentiles(10, 15)))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, notNullValue());

    Percentiles percentiles = bucket.getAggregations().get("percentiles");
    assertThat(percentiles, notNullValue());
    assertThat(percentiles.getName(), equalTo("percentiles"));
    assertThat(percentiles.percentile(10), equalTo(Double.NaN));
    assertThat(percentiles.percentile(15), equalTo(Double.NaN));
  }
Пример #12
0
  // Duel between histograms and scripted terms
  public void testDuelTermsHistogram() throws Exception {
    createIndex("idx");

    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());

    SearchResponse resp =
        client()
            .prepareSearch("idx")
            .addAggregation(
                terms("terms")
                    .field("values")
                    .collectMode(randomFrom(SubAggCollectionMode.values()))
                    .script("floor(_value / interval)")
                    .param("interval", interval)
                    .size(maxNumTerms))
            .addAggregation(histogram("histo").field("values").interval(interval))
            .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 (Terms.Bucket bucket : terms.getBuckets()) {
      final long key = bucket.getKeyAsNumber().longValue() * interval;
      final Histogram.Bucket histoBucket = histo.getBucketByKey(key);
      assertEquals(bucket.getDocCount(), histoBucket.getDocCount());
    }
  }
Пример #13
0
  @Test
  public void testDocCount_asSubAgg() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                terms("terms")
                    .field("tag")
                    .order(Order.term(true))
                    .subAggregation(
                        histogram("histo")
                            .field(SINGLE_VALUED_FIELD_NAME)
                            .interval(interval)
                            .extendedBounds((long) minRandomValue, (long) maxRandomValue))
                    .subAggregation(maxBucket("max_bucket").setBucketsPaths("histo>_count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));

    for (int i = 0; i < interval; ++i) {
      Terms.Bucket termsBucket = termsBuckets.get(i);
      assertThat(termsBucket, notNullValue());
      assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));

      Histogram histo = termsBucket.getAggregations().get("histo");
      assertThat(histo, notNullValue());
      assertThat(histo.getName(), equalTo("histo"));
      List<? extends Bucket> buckets = histo.getBuckets();

      List<String> maxKeys = new ArrayList<>();
      double maxValue = Double.NEGATIVE_INFINITY;
      for (int j = 0; j < numValueBuckets; ++j) {
        Histogram.Bucket bucket = buckets.get(j);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
        if (bucket.getDocCount() > maxValue) {
          maxValue = bucket.getDocCount();
          maxKeys = new ArrayList<>();
          maxKeys.add(bucket.getKeyAsString());
        } else if (bucket.getDocCount() == maxValue) {
          maxKeys.add(bucket.getKeyAsString());
        }
      }

      InternalBucketMetricValue maxBucketValue = termsBucket.getAggregations().get("max_bucket");
      assertThat(maxBucketValue, notNullValue());
      assertThat(maxBucketValue.getName(), equalTo("max_bucket"));
      assertThat(maxBucketValue.value(), equalTo(maxValue));
      assertThat(maxBucketValue.keys(), equalTo(maxKeys.toArray(new String[maxKeys.size()])));
    }
  }
Пример #14
0
  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(0L, histogram.getBuckets().get(0).getKey());
    assertEquals(1, histogram.getBuckets().get(0).getDocCount());
    assertEquals(5L, 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(0L, histogram.getBuckets().get(0).getKey());
    assertEquals(2, histogram.getBuckets().get(0).getDocCount());
  }
Пример #15
0
  public void testDateHistogram() {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                dateHistogram("my_histogram")
                    .field("date")
                    .interval(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")
                    .interval(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());
  }
Пример #16
0
  @Test
  public void testMetric_asSubAggOfSingleBucketAgg() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                filter("filter")
                    .filter(termQuery("tag", "tag0"))
                    .subAggregation(
                        histogram("histo")
                            .field(SINGLE_VALUED_FIELD_NAME)
                            .interval(interval)
                            .extendedBounds((long) minRandomValue, (long) maxRandomValue)
                            .subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME)))
                    .subAggregation(maxBucket("max_bucket").setBucketsPaths("histo>sum")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Filter filter = response.getAggregations().get("filter");
    assertThat(filter, notNullValue());
    assertThat(filter.getName(), equalTo("filter"));
    Histogram histo = filter.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();

    List<String> maxKeys = new ArrayList<>();
    double maxValue = Double.NEGATIVE_INFINITY;
    for (int j = 0; j < numValueBuckets; ++j) {
      Histogram.Bucket bucket = buckets.get(j);
      assertThat(bucket, notNullValue());
      assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
      if (bucket.getDocCount() != 0) {
        Sum sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        if (sum.value() > maxValue) {
          maxValue = sum.value();
          maxKeys = new ArrayList<>();
          maxKeys.add(bucket.getKeyAsString());
        } else if (sum.value() == maxValue) {
          maxKeys.add(bucket.getKeyAsString());
        }
      }
    }

    InternalBucketMetricValue maxBucketValue = filter.getAggregations().get("max_bucket");
    assertThat(maxBucketValue, notNullValue());
    assertThat(maxBucketValue.getName(), equalTo("max_bucket"));
    assertThat(maxBucketValue.value(), equalTo(maxValue));
    assertThat(maxBucketValue.keys(), equalTo(maxKeys.toArray(new String[maxKeys.size()])));
  }
Пример #17
0
 public void testUnmappedHistogram() {
   SearchResponse response =
       client()
           .prepareSearch("idx")
           .addAggregation(
               histogram("my_histogram").field("non-existing_field").interval(5).missing(12))
           .get();
   assertSearchResponse(response);
   Histogram histogram = response.getAggregations().get("my_histogram");
   assertEquals(1, histogram.getBuckets().size());
   assertEquals(10L, histogram.getBuckets().get(0).getKey());
   assertEquals(2, histogram.getBuckets().get(0).getDocCount());
 }
Пример #18
0
  public void testSingleValuedFieldNormalised() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                dateHistogram("histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.MONTH)
                    .minDocCount(0)
                    .subAggregation(derivative("deriv", "_count").unit(DateHistogramInterval.DAY)))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.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((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    Derivative docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, nullValue());

    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), closeTo(1d, 0.00001));
    assertThat(docCountDeriv.normalizedValue(), closeTo(1d / 31d, 0.00001));

    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), closeTo(1d, 0.00001));
    assertThat(docCountDeriv.normalizedValue(), closeTo(1d / 29d, 0.00001));
  }
Пример #19
0
  public void testUnmapped() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx_unmapped")
            .addAggregation(
                dateHistogram("histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.MONTH)
                    .minDocCount(0)
                    .subAggregation(derivative("deriv", "_count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    assertThat(deriv.getBuckets().size(), equalTo(0));
  }
Пример #20
0
 @Test
 public void histogramOnNestedField() throws Exception {
   Aggregations result =
       query(
           String.format(
               "select count(*) from %s/nestedType group by histogram('field'='message.dayOfWeek','nested'='message','interval'='2' , 'alias' = 'someAlias' )",
               TEST_INDEX));
   InternalNested nested = result.get("message@NESTED");
   Histogram histogram = nested.getAggregations().get("someAlias");
   for (Histogram.Bucket bucket : histogram.getBuckets()) {
     long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
     if (bucket.getKey().equals("0") || bucket.getKey().equals("4")) {
       Assert.assertEquals(2, count);
     } else if (bucket.getKey().equals("2")) {
       Assert.assertEquals(1, count);
     } else {
       Assert.assertTrue("only 0 2 4 keys are allowed got:" + bucket.getKey(), false);
     }
   }
 }
Пример #21
0
  @Test
  public void emptyAggregation() throws Exception {
    prepareCreate("empty_bucket_idx")
        .addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=integer")
        .execute()
        .actionGet();
    List<IndexRequestBuilder> builders = new ArrayList<IndexRequestBuilder>();
    for (int i = 0; i < 2; i++) {
      builders.add(
          client()
              .prepareIndex("empty_bucket_idx", "type", "" + i)
              .setSource(
                  jsonBuilder().startObject().field(SINGLE_VALUED_FIELD_NAME, i * 2).endObject()));
    }
    indexRandom(true, builders.toArray(new IndexRequestBuilder[builders.size()]));

    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .interval(1l)
                    .minDocCount(0)
                    .subAggregation(terms("terms")))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBucketByKey(1l);
    assertThat(bucket, Matchers.notNullValue());

    Terms terms = bucket.getAggregations().get("terms");
    assertThat(terms, Matchers.notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().isEmpty(), is(true));
  }
Пример #22
0
  public void testSingleValuedFieldWithSubAggregation() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                dateHistogram("histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.MONTH)
                    .minDocCount(0)
                    .subAggregation(sum("sum").field("value"))
                    .subAggregation(derivative("deriv", "sum")))
            .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));
    Object[] propertiesKeys = (Object[]) histo.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) histo.getProperty("_count");
    Object[] propertiesCounts = (Object[]) histo.getProperty("sum.value");

    DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    Sum sum = bucket.getAggregations().get("sum");
    assertThat(sum, notNullValue());
    assertThat(sum.getValue(), equalTo(1.0));
    SimpleValue deriv = bucket.getAggregations().get("deriv");
    assertThat(deriv, nullValue());
    assertThat((DateTime) propertiesKeys[0], equalTo(key));
    assertThat((long) propertiesDocCounts[0], equalTo(1L));
    assertThat((double) propertiesCounts[0], equalTo(1.0));

    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    sum = bucket.getAggregations().get("sum");
    assertThat(sum, notNullValue());
    assertThat(sum.getValue(), equalTo(5.0));
    deriv = bucket.getAggregations().get("deriv");
    assertThat(deriv, notNullValue());
    assertThat(deriv.value(), equalTo(4.0));
    assertThat(
        (double)
            bucket.getProperty(
                "histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()),
        equalTo(4.0));
    assertThat((DateTime) propertiesKeys[1], equalTo(key));
    assertThat((long) propertiesDocCounts[1], equalTo(2L));
    assertThat((double) propertiesCounts[1], equalTo(5.0));

    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    sum = bucket.getAggregations().get("sum");
    assertThat(sum, notNullValue());
    assertThat(sum.getValue(), equalTo(15.0));
    deriv = bucket.getAggregations().get("deriv");
    assertThat(deriv, notNullValue());
    assertThat(deriv.value(), equalTo(10.0));
    assertThat(
        (double)
            bucket.getProperty(
                "histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()),
        equalTo(10.0));
    assertThat((DateTime) propertiesKeys[2], equalTo(key));
    assertThat((long) propertiesDocCounts[2], equalTo(3L));
    assertThat((double) propertiesCounts[2], equalTo(15.0));
  }
Пример #23
0
  /**
   * also check for time zone shifts that are not one hour, e.g. "Asia/Kathmandu, 1 Jan 1986 - Time
   * Zone Change (IST → NPT), at 00:00:00 clocks were turned forward 00:15 minutes
   */
  public void testSingleValuedFieldNormalised_timeZone_AsiaKathmandu() throws Exception {
    createIndex(IDX_DST_KATHMANDU);
    DateTimeZone timezone = DateTimeZone.forID("Asia/Kathmandu");
    List<IndexRequestBuilder> builders = new ArrayList<>();

    addNTimes(1, IDX_DST_KATHMANDU, new DateTime("1985-12-31T22:30:00", timezone), builders);
    // the shift happens during the next bucket, which includes the 45min that do not start on the
    // full hour
    addNTimes(2, IDX_DST_KATHMANDU, new DateTime("1985-12-31T23:30:00", timezone), builders);
    addNTimes(3, IDX_DST_KATHMANDU, new DateTime("1986-01-01T01:30:00", timezone), builders);
    addNTimes(4, IDX_DST_KATHMANDU, new DateTime("1986-01-01T02:30:00", timezone), builders);
    indexRandom(true, builders);
    ensureSearchable();

    SearchResponse response =
        client()
            .prepareSearch(IDX_DST_KATHMANDU)
            .addAggregation(
                dateHistogram("histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.HOUR)
                    .timeZone(timezone)
                    .minDocCount(0)
                    .subAggregation(
                        derivative("deriv", "_count").unit(DateHistogramInterval.MINUTE)))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(4));

    assertBucket(
        buckets.get(0),
        new DateTime("1985-12-31T22:00:00", timezone).toDateTime(DateTimeZone.UTC),
        1L,
        nullValue(),
        null,
        null);
    assertBucket(
        buckets.get(1),
        new DateTime("1985-12-31T23:00:00", timezone).toDateTime(DateTimeZone.UTC),
        2L,
        notNullValue(),
        1d,
        1d / 60d);
    // the following is normalized using a 105min bucket width
    assertBucket(
        buckets.get(2),
        new DateTime("1986-01-01T01:00:00", timezone).toDateTime(DateTimeZone.UTC),
        3L,
        notNullValue(),
        1d,
        1d / 105d);
    assertBucket(
        buckets.get(3),
        new DateTime("1986-01-01T02:00:00", timezone).toDateTime(DateTimeZone.UTC),
        4L,
        notNullValue(),
        1d,
        1d / 60d);
  }
Пример #24
0
  /** Do a derivative on a date histogram with time zone CET at DST end */
  public void testSingleValuedFieldNormalised_timeZone_CET_DstEnd() throws Exception {
    createIndex(IDX_DST_END);
    DateTimeZone timezone = DateTimeZone.forID("CET");
    List<IndexRequestBuilder> builders = new ArrayList<>();

    addNTimes(1, IDX_DST_END, new DateTime("2012-10-27T01:00:00", timezone), builders);
    addNTimes(
        2,
        IDX_DST_END,
        new DateTime("2012-10-28T01:00:00", timezone),
        builders); // day with dst shift -1h, 25h long
    addNTimes(3, IDX_DST_END, new DateTime("2012-10-29T01:00:00", timezone), builders);
    addNTimes(4, IDX_DST_END, new DateTime("2012-10-30T01:00:00", timezone), builders);
    indexRandom(true, builders);
    ensureSearchable();

    SearchResponse response =
        client()
            .prepareSearch(IDX_DST_END)
            .addAggregation(
                dateHistogram("histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.DAY)
                    .timeZone(timezone)
                    .minDocCount(0)
                    .subAggregation(derivative("deriv", "_count").unit(DateHistogramInterval.HOUR)))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(4));

    assertBucket(
        buckets.get(0),
        new DateTime("2012-10-27", timezone).toDateTime(DateTimeZone.UTC),
        1L,
        nullValue(),
        null,
        null);
    assertBucket(
        buckets.get(1),
        new DateTime("2012-10-28", timezone).toDateTime(DateTimeZone.UTC),
        2L,
        notNullValue(),
        1d,
        1d / 24d);
    // the following is normalized using a 25h bucket width
    assertBucket(
        buckets.get(2),
        new DateTime("2012-10-29", timezone).toDateTime(DateTimeZone.UTC),
        3L,
        notNullValue(),
        1d,
        1d / 25d);
    assertBucket(
        buckets.get(3),
        new DateTime("2012-10-30", timezone).toDateTime(DateTimeZone.UTC),
        4L,
        notNullValue(),
        1d,
        1d / 24d);
  }
  public void testEmptyBuckets() {
    SearchResponse response =
        client()
            .prepareSearch("idx_with_gaps")
            .addAggregation(
                histogram("histo")
                    .field(FIELD_1_NAME)
                    .interval(1)
                    .subAggregation(
                        histogram("inner_histo")
                            .field(FIELD_1_NAME)
                            .interval(1)
                            .extendedBounds(new ExtendedBounds(1L, 4L))
                            .minDocCount(0)
                            .subAggregation(
                                derivative("derivative", "_count")
                                    .gapPolicy(GapPolicy.INSERT_ZEROS))))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    InternalHistogram<Bucket> histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(3));

    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("1"));
    Histogram innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    List<? extends Histogram.Bucket> innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
      Histogram.Bucket innerBucket = innerBuckets.get(i);
      if (i == 0) {
        assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
      } else {
        assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
      }
    }

    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("2"));
    innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
      Histogram.Bucket innerBucket = innerBuckets.get(i);
      if (i == 0) {
        assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
      } else {
        assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
      }
    }
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo("3"));
    innerHisto = bucket.getAggregations().get("inner_histo");
    assertThat(innerHisto, notNullValue());
    innerBuckets = innerHisto.getBuckets();
    assertThat(innerBuckets, notNullValue());
    assertThat(innerBuckets.size(), equalTo(4));
    for (int i = 0; i < innerBuckets.size(); i++) {
      Histogram.Bucket innerBucket = innerBuckets.get(i);
      if (i == 0) {
        assertThat(innerBucket.getAggregations().get("derivative"), nullValue());
      } else {
        assertThat(innerBucket.getAggregations().get("derivative"), notNullValue());
      }
    }
  }