@Test
  // the main purpose of this test is to make sure we're not allocating 2GB of memory per shard
  public void sizeIsZero() {
    final int minDocCount = randomInt(1);
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .minDocCount(minDocCount)
                    .size(0))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(
        terms.getBuckets().size(),
        equalTo(minDocCount == 0 ? 105 : 100)); // 105 because of the other type
  }
示例#2
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());
    }
  }
  @Test
  public void singleValuedField_OrderedBySingleValueSubAggregationAsc() throws Exception {
    boolean asc = true;
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .order(Terms.Order.aggregation("avg_i", asc))
                    .subAggregation(avg("avg_i").field("i")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));

    int i = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
      Avg avg = bucket.getAggregations().get("avg_i");
      assertThat(avg, notNullValue());
      assertThat(avg.getValue(), equalTo((double) i));
      i++;
    }
  }
  @Test
  public void script_SingleValue_WithSubAggregator_Inherited() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .script("doc['" + SINGLE_VALUED_FIELD_NAME + "'].value")
                    .subAggregation(count("count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));

    for (int i = 0; i < 5; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
      ValueCount valueCount = bucket.getAggregations().get("count");
      assertThat(valueCount, notNullValue());
      assertThat(valueCount.getValue(), equalTo(1l));
    }
  }
  @Test
  public void stringTermsNestedIntoPerBucketAggregator() throws Exception {
    // no execution hint so that the logic that decides whether or not to use ordinals is executed
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                filter("filter")
                    .filter(termFilter(MULTI_VALUED_FIELD_NAME, "val3"))
                    .subAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME)))
            .execute()
            .actionGet();

    assertThat(response.getFailedShards(), equalTo(0));

    Filter filter = response.getAggregations().get("filter");

    Terms terms = filter.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));

    for (int i = 2; i <= 4; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      assertThat(bucket.getDocCount(), equalTo(i == 3 ? 2L : 1L));
    }
  }
  @Test
  public void script_MultiValued() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .script("doc['" + MULTI_VALUED_FIELD_NAME + "'].values"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(6));

    for (int i = 0; i < 6; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      if (i == 0 || i == 5) {
        assertThat(bucket.getDocCount(), equalTo(1l));
      } else {
        assertThat(bucket.getDocCount(), equalTo(2l));
      }
    }
  }
  @Test
  public void unmapped() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx_unmapped")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .size(randomInt(5))
                    .field(SINGLE_VALUED_FIELD_NAME))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(0));
  }
  @Test
  public void singleValueField_OrderedByTermDesc() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .order(Terms.Order.term(false)))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));

    int i = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
      i--;
    }
  }
  @Test
  public void partiallyUnmapped() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx", "idx_unmapped")
            .setTypes("type")
            .addAggregation(
                terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));

    for (int i = 0; i < 5; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }
  }
示例#10
0
  @Test
  public void multiValuedField_WithValueScript_NotUnique() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(MULTI_VALUED_FIELD_NAME)
                    .script("_value.substring(0,3)"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(1));

    Terms.Bucket bucket = terms.getBucketByKey("val");
    assertThat(bucket, notNullValue());
    assertThat(key(bucket), equalTo("val"));
    assertThat(bucket.getDocCount(), equalTo(5l));
  }
示例#11
0
  @Test
  public void singleValueField_WithMaxSize() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .size(20)
                    .order(
                        Terms.Order.term(
                            true))) // we need to sort by terms cause we're checking the first 20
                                    // values
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(20));

    for (int i = 0; i < 20; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val" + Strings.padStart(i + "", 3, '0'));
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val" + Strings.padStart(i + "", 3, '0')));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }
  }
示例#12
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));
  }
示例#13
0
  @Test
  public void multiValuedField_WithValueScript_WithInheritedSubAggregator() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("terms")
                    .executionHint(randomExecutionHint())
                    .field(MULTI_VALUED_FIELD_NAME)
                    .script("'foo_' + _value")
                    .subAggregation(count("count")))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(6));

    for (int i = 0; i < 6; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("foo_val" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("foo_val" + i));
      if (i == 0 | i == 5) {
        assertThat(bucket.getDocCount(), equalTo(1l));
        ValueCount valueCount = bucket.getAggregations().get("count");
        assertThat(valueCount, notNullValue());
        assertThat(valueCount.getValue(), equalTo(2l));
      } else {
        assertThat(bucket.getDocCount(), equalTo(2l));
        ValueCount valueCount = bucket.getAggregations().get("count");
        assertThat(valueCount, notNullValue());
        assertThat("term[" + key(bucket) + "]", valueCount.getValue(), equalTo(4l));
      }
    }
  }
示例#14
0
  // https://github.com/elasticsearch/elasticsearch/issues/6435
  public void testReduce() throws Exception {
    createIndex("idx");
    final int value = randomIntBetween(0, 10);
    indexRandom(true, client().prepareIndex("idx", "type").setSource("f", value));
    ensureYellow("idx"); // only one document let's make sure all shards have an active primary
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                filter("filter")
                    .filter(FilterBuilders.matchAllFilter())
                    .subAggregation(
                        range("range")
                            .field("f")
                            .addUnboundedTo(6)
                            .addUnboundedFrom(6)
                            .subAggregation(sum("sum").field("f"))))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Filter filter = response.getAggregations().get("filter");
    assertNotNull(filter);
    assertEquals(1, filter.getDocCount());

    Range range = filter.getAggregations().get("range");
    assertThat(range, notNullValue());
    assertThat(range.getName(), equalTo("range"));
    assertThat(range.getBuckets().size(), equalTo(2));

    Range.Bucket bucket = range.getBucketByKey("*-6.0");
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKey(), equalTo("*-6.0"));
    assertThat(bucket.getFrom().doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
    assertThat(bucket.getTo().doubleValue(), equalTo(6.0));
    assertThat(bucket.getDocCount(), equalTo(value < 6 ? 1L : 0L));
    Sum sum = bucket.getAggregations().get("sum");
    assertEquals(value < 6 ? value : 0, sum.getValue(), 0d);

    bucket = range.getBucketByKey("6.0-*");
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKey(), equalTo("6.0-*"));
    assertThat(bucket.getFrom().doubleValue(), equalTo(6.0));
    assertThat(bucket.getTo().doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(bucket.getDocCount(), equalTo(value >= 6 ? 1L : 0L));
    sum = bucket.getAggregations().get("sum");
    assertEquals(value >= 6 ? value : 0, sum.getValue(), 0d);
  }
示例#15
0
  @Test
  public void singleValueField_WithRegexFiltering_WithFlags() throws Exception {

    // include without exclude
    // we should be left with: val000, val001, val002, val003, val004, val005, val006, val007,
    // val008, val009
    // with case insensitive flag on the include regex

    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .include("VAL00.+", Pattern.CASE_INSENSITIVE))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }

    // include and exclude
    // we should be left with: val002, val003, val004, val005, val006, val007, val008, val009
    // with multi-flag masking on the exclude regex

    response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .include("val00.+")
                    .exclude(
                        "( val000 | VAL001 )#this is a comment",
                        Pattern.CASE_INSENSITIVE | Pattern.COMMENTS))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(8));

    for (int i = 2; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }

    // exclude without include
    // we should be left with: val000, val001, val002, val003, val004, val005, val006, val007,
    // val008, val009
    // with a "no flag" flag

    response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms").field(SINGLE_VALUED_FIELD_NAME).exclude("val0[1-9]+.+", 0))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }
  }
示例#16
0
  @Test
  public void singleValueField_WithRegexFiltering() throws Exception {

    // include without exclude
    // we should be left with: val000, val001, val002, val003, val004, val005, val006, val007,
    // val008, val009

    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).include("val00.+"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }

    // include and exclude
    // we should be left with: val002, val003, val004, val005, val006, val007, val008, val009

    response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(
                terms("terms")
                    .field(SINGLE_VALUED_FIELD_NAME)
                    .include("val00.+")
                    .exclude("(val000|val001)"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(8));

    for (int i = 2; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }

    // exclude without include
    // we should be left with: val000, val001, val002, val003, val004, val005, val006, val007,
    // val008, val009

    response =
        client()
            .prepareSearch("idx")
            .setTypes("high_card_type")
            .addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).exclude("val0[1-9]+.+"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(10));

    for (int i = 0; i < 10; i++) {
      Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
      assertThat(bucket, notNullValue());
      assertThat(key(bucket), equalTo("val00" + i));
      assertThat(bucket.getDocCount(), equalTo(1l));
    }
  }
示例#17
0
  // Make sure that unordered, reversed, disjoint and/or overlapping ranges are supported
  // Duel with filters
  public void testRandomRanges() throws Exception {
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final double[][] docs = new double[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
      final int numValues = randomInt(5);
      docs[i] = new double[numValues];
      for (int j = 0; j < numValues; ++j) {
        docs[i][j] = randomDouble() * 100;
      }
    }

    createIndex("idx");
    for (int i = 0; i < docs.length; ++i) {
      XContentBuilder source = jsonBuilder().startObject().startArray("values");
      for (int j = 0; j < docs[i].length; ++j) {
        source = source.value(docs[i][j]);
      }
      source = source.endArray().endObject();
      client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
    }
    assertNoFailures(
        client()
            .admin()
            .indices()
            .prepareRefresh("idx")
            .setIndicesOptions(IndicesOptions.lenientExpandOpen())
            .execute()
            .get());

    final int numRanges = randomIntBetween(1, 20);
    final double[][] ranges = new double[numRanges][];
    for (int i = 0; i < ranges.length; ++i) {
      switch (randomInt(2)) {
        case 0:
          ranges[i] = new double[] {Double.NEGATIVE_INFINITY, randomInt(100)};
          break;
        case 1:
          ranges[i] = new double[] {randomInt(100), Double.POSITIVE_INFINITY};
          break;
        case 2:
          ranges[i] = new double[] {randomInt(100), randomInt(100)};
          break;
        default:
          throw new AssertionError();
      }
    }

    RangeBuilder query = range("range").field("values");
    for (int i = 0; i < ranges.length; ++i) {
      String key = Integer.toString(i);
      if (ranges[i][0] == Double.NEGATIVE_INFINITY) {
        query.addUnboundedTo(key, ranges[i][1]);
      } else if (ranges[i][1] == Double.POSITIVE_INFINITY) {
        query.addUnboundedFrom(key, ranges[i][0]);
      } else {
        query.addRange(key, ranges[i][0], ranges[i][1]);
      }
    }

    SearchRequestBuilder reqBuilder = client().prepareSearch("idx").addAggregation(query);
    for (int i = 0; i < ranges.length; ++i) {
      RangeFilterBuilder filter = FilterBuilders.rangeFilter("values");
      if (ranges[i][0] != Double.NEGATIVE_INFINITY) {
        filter = filter.from(ranges[i][0]);
      }
      if (ranges[i][1] != Double.POSITIVE_INFINITY) {
        filter = filter.to(ranges[i][1]);
      }
      reqBuilder = reqBuilder.addAggregation(filter("filter" + i).filter(filter));
    }

    SearchResponse resp = reqBuilder.execute().actionGet();
    Range range = resp.getAggregations().get("range");

    for (int i = 0; i < ranges.length; ++i) {

      long count = 0;
      for (double[] values : docs) {
        for (double value : values) {
          if (value >= ranges[i][0] && value < ranges[i][1]) {
            ++count;
            break;
          }
        }
      }

      final Range.Bucket bucket = range.getBucketByKey(Integer.toString(i));
      assertEquals(bucket.getKey(), count, bucket.getDocCount());

      final Filter filter = resp.getAggregations().get("filter" + i);
      assertThat(filter.getDocCount(), equalTo(count));
    }
  }
示例#18
0
  @Test
  public void singleValuedField_OrderedBySubAggregationAsc_MultiHierarchyLevels() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("tags")
                    .executionHint(randomExecutionHint())
                    .field("tag")
                    .order(Terms.Order.aggregation("filter1>filter2>stats.max", asc))
                    .subAggregation(
                        filter("filter1")
                            .filter(FilterBuilders.matchAllFilter())
                            .subAggregation(
                                filter("filter2")
                                    .filter(FilterBuilders.matchAllFilter())
                                    .subAggregation(stats("stats").field("i")))))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms tags = response.getAggregations().get("tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));

    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();

    // the max for "more" is 2
    // the max for "less" is 4

    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "more" : "less"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3l : 2l));
    Filter filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 3l : 2l));
    Filter filter2 = filter1.getAggregations().get("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3l : 2l));
    Stats stats = filter2.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0));

    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "less" : "more"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2l : 3l));
    filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 2l : 3l));
    filter2 = filter1.getAggregations().get("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2l : 3l));
    stats = filter2.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0));
  }
示例#19
0
  @Test
  public void singleValuedField_OrderedBySingleBucketSubAggregationAsc() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .setTypes("type")
            .addAggregation(
                terms("tags")
                    .executionHint(randomExecutionHint())
                    .field("tag")
                    .order(Terms.Order.aggregation("filter", asc))
                    .subAggregation(filter("filter").filter(FilterBuilders.matchAllFilter())))
            .execute()
            .actionGet();

    assertSearchResponse(response);

    Terms tags = response.getAggregations().get("tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));

    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();

    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "less" : "more"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2l : 3l));
    Filter filter = tag.getAggregations().get("filter");
    assertThat(filter, notNullValue());
    assertThat(filter.getDocCount(), equalTo(asc ? 2l : 3l));

    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "more" : "less"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3l : 2l));
    filter = tag.getAggregations().get("filter");
    assertThat(filter, notNullValue());
    assertThat(filter.getDocCount(), equalTo(asc ? 3l : 2l));
  }