示例#1
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());
    }
  }
  @Override
  public void testOrderByEmptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                terms("terms")
                    .field("value")
                    .order(
                        Terms.Order.compound(
                            Terms.Order.aggregation("filter>percentiles.99", true)))
                    .subAggregation(
                        filter("filter", termQuery("value", 100))
                            .subAggregation(
                                percentiles("percentiles")
                                    .method(PercentilesMethod.TDIGEST)
                                    .field("value"))))
            .get();

    assertHitCount(searchResponse, 10);

    Terms terms = searchResponse.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets, notNullValue());
    assertThat(buckets.size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = buckets.get(i);
      assertThat(bucket, notNullValue());
      assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
      assertThat(bucket.getDocCount(), equalTo(1L));
      Filter filter = bucket.getAggregations().get("filter");
      assertThat(filter, notNullValue());
      assertThat(filter.getDocCount(), equalTo(0L));
      Percentiles percentiles = filter.getAggregations().get("percentiles");
      assertThat(percentiles, notNullValue());
      assertThat(percentiles.percentile(99), equalTo(Double.NaN));
    }
  }
示例#3
0
  @Override
  public void testOrderByEmptyAggregation() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("idx")
            .setQuery(matchAllQuery())
            .addAggregation(
                terms("terms")
                    .field("value")
                    .order(Order.compound(Order.aggregation("filter>stats.avg", true)))
                    .subAggregation(
                        filter("filter", termQuery("value", 100))
                            .subAggregation(stats("stats").field("value"))))
            .get();

    assertHitCount(searchResponse, 10);

    Terms terms = searchResponse.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets, notNullValue());
    assertThat(buckets.size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = buckets.get(i);
      assertThat(bucket, notNullValue());
      assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
      assertThat(bucket.getDocCount(), equalTo(1L));
      Filter filter = bucket.getAggregations().get("filter");
      assertThat(filter, notNullValue());
      assertThat(filter.getDocCount(), equalTo(0L));
      Stats stats = filter.getAggregations().get("stats");
      assertThat(stats, notNullValue());
      assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY));
      assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY));
      assertThat(stats.getAvg(), equalTo(Double.NaN));
      assertThat(stats.getSum(), equalTo(0.0));
      assertThat(stats.getCount(), equalTo(0L));
    }
  }