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));
  }
示例#2
0
  @Test
  public void singleValueAggDerivativeWithGaps_random() throws Exception {
    GapPolicy gapPolicy = randomFrom(GapPolicy.values());
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx_rnd")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .interval(1)
                    .extendedBounds(0l, (long) numBuckets_empty_rnd - 1)
                    .subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))
                    .subAggregation(
                        derivative("deriv").setBucketsPaths("sum").gapPolicy(gapPolicy)))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocsEmptyIdx_rnd));

    InternalHistogram<Bucket> deriv = searchResponse.getAggregations().get("histo");
    assertThat(deriv, Matchers.notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(numBuckets_empty_rnd));

    double lastSumValue = Double.NaN;
    for (int i = 0; i < valueCounts_empty_rnd.length; i++) {
      Histogram.Bucket bucket = buckets.get(i);
      checkBucketKeyAndDocCount("Bucket " + i, bucket, i, valueCounts_empty_rnd[i]);
      Sum sum = bucket.getAggregations().get("sum");
      double thisSumValue = sum.value();
      if (bucket.getDocCount() == 0) {
        thisSumValue = gapPolicy == GapPolicy.INSERT_ZEROS ? 0 : Double.NaN;
      }
      SimpleValue sumDeriv = bucket.getAggregations().get("deriv");
      if (i == 0) {
        assertThat(sumDeriv, nullValue());
      } else {
        double expectedDerivative = thisSumValue - lastSumValue;
        if (Double.isNaN(expectedDerivative)) {
          assertThat(sumDeriv.value(), equalTo(expectedDerivative));
        } else {
          assertThat(sumDeriv.value(), closeTo(expectedDerivative, 0.00001));
        }
      }
      lastSumValue = thisSumValue;
    }
  }
示例#3
0
  @Test
  public void docCountDerivativeWithGaps() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("empty_bucket_idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                histogram("histo")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .interval(1)
                    .subAggregation(derivative("deriv").setBucketsPaths("_count")))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocsEmptyIdx));

    InternalHistogram<Bucket> deriv = searchResponse.getAggregations().get("histo");
    assertThat(deriv, Matchers.notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(valueCounts_empty.length));

    for (int i = 0; i < valueCounts_empty.length; i++) {
      Histogram.Bucket bucket = buckets.get(i);
      checkBucketKeyAndDocCount("Bucket " + i, bucket, i, valueCounts_empty[i]);
      SimpleValue docCountDeriv = bucket.getAggregations().get("deriv");
      if (firstDerivValueCounts_empty[i] == null) {
        assertThat(docCountDeriv, nullValue());
      } else {
        assertThat(docCountDeriv.value(), equalTo(firstDerivValueCounts_empty[i]));
      }
    }
  }
示例#4
0
  @Test
  public void partiallyUnmapped() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx", "idx_unmapped")
            .addAggregation(
                histogram("histo")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .interval(interval)
                    .subAggregation(derivative("deriv").setBucketsPaths("_count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

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

    for (int i = 0; i < numValueBuckets; ++i) {
      Histogram.Bucket bucket = buckets.get(i);
      checkBucketKeyAndDocCount("Bucket " + i, bucket, i * interval, valueCounts[i]);
      SimpleValue docCountDeriv = bucket.getAggregations().get("deriv");
      if (i > 0) {
        assertThat(docCountDeriv, notNullValue());
        assertThat(docCountDeriv.value(), equalTo((double) firstDerivValueCounts[i - 1]));
      } else {
        assertThat(docCountDeriv, nullValue());
      }
    }
  }
示例#5
0
  @Test
  public void multiValueAggDerivative() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                histogram("histo")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .interval(interval)
                    .subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
                    .subAggregation(derivative("deriv").setBucketsPaths("stats.sum")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    InternalHistogram<Bucket> deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    assertThat(deriv.getBuckets().size(), equalTo(numValueBuckets));
    Object[] propertiesKeys = (Object[]) deriv.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) deriv.getProperty("_count");
    Object[] propertiesSumCounts = (Object[]) deriv.getProperty("stats.sum");

    List<Bucket> buckets = new ArrayList<Bucket>(deriv.getBuckets());
    Long expectedSumPreviousBucket = Long.MIN_VALUE; // start value, gets
    // overwritten
    for (int i = 0; i < numValueBuckets; ++i) {
      Histogram.Bucket bucket = buckets.get(i);
      checkBucketKeyAndDocCount("Bucket " + i, bucket, i * interval, valueCounts[i]);
      Stats stats = bucket.getAggregations().get("stats");
      assertThat(stats, notNullValue());
      long expectedSum = valueCounts[i] * (i * interval);
      assertThat(stats.getSum(), equalTo((double) expectedSum));
      SimpleValue sumDeriv = bucket.getAggregations().get("deriv");
      if (i > 0) {
        assertThat(sumDeriv, notNullValue());
        long sumDerivValue = expectedSum - expectedSumPreviousBucket;
        assertThat(sumDeriv.value(), equalTo((double) sumDerivValue));
        assertThat(
            (double)
                bucket.getProperty(
                    "histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()),
            equalTo((double) sumDerivValue));
      } else {
        assertThat(sumDeriv, nullValue());
      }
      expectedSumPreviousBucket = expectedSum;
      assertThat((long) propertiesKeys[i], equalTo((long) i * interval));
      assertThat((long) propertiesDocCounts[i], equalTo(valueCounts[i]));
      assertThat((double) propertiesSumCounts[i], equalTo((double) expectedSum));
    }
  }
  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));
  }