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));
  }
 private static void assertBucket(
     Histogram.Bucket bucket,
     DateTime expectedKey,
     long expectedDocCount,
     Matcher<Object> derivativeMatcher,
     Double derivative,
     Double normalizedDerivative) {
   assertThat(bucket, notNullValue());
   assertThat((DateTime) bucket.getKey(), equalTo(expectedKey));
   assertThat(bucket.getDocCount(), equalTo(expectedDocCount));
   Derivative docCountDeriv = bucket.getAggregations().get("deriv");
   assertThat(docCountDeriv, derivativeMatcher);
   if (docCountDeriv != null) {
     assertThat(docCountDeriv.value(), closeTo(derivative, 0.00001));
     assertThat(docCountDeriv.normalizedValue(), closeTo(normalizedDerivative, 0.00001));
   }
 }