コード例 #1
0
  @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));
      }
    }
  }
コード例 #2
0
ファイル: ChildrenTests.java プロジェクト: jsvd/elasticsearch
  @Test
  public void testChildrenAggs() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(matchQuery("randomized", true))
            .addAggregation(
                terms("category")
                    .field("category")
                    .size(0)
                    .subAggregation(
                        children("to_comment")
                            .childType("comment")
                            .subAggregation(
                                terms("commenters")
                                    .field("commenter")
                                    .size(0)
                                    .subAggregation(topHits("top_comments")))))
            .get();
    assertSearchResponse(searchResponse);

    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(categoryToControl.size()));
    for (Map.Entry<String, Control> entry1 : categoryToControl.entrySet()) {
      Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry1.getKey());
      assertThat(categoryBucket.getKey(), equalTo(entry1.getKey()));
      assertThat(categoryBucket.getDocCount(), equalTo((long) entry1.getValue().articleIds.size()));

      Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
      assertThat(childrenBucket.getName(), equalTo("to_comment"));
      assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size()));
      assertThat(
          (long) childrenBucket.getProperty("_count"),
          equalTo((long) entry1.getValue().commentIds.size()));

      Terms commentersTerms = childrenBucket.getAggregations().get("commenters");
      assertThat((Terms) childrenBucket.getProperty("commenters"), sameInstance(commentersTerms));
      assertThat(
          commentersTerms.getBuckets().size(),
          equalTo(entry1.getValue().commenterToCommentId.size()));
      for (Map.Entry<String, Set<String>> entry2 :
          entry1.getValue().commenterToCommentId.entrySet()) {
        Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey());
        assertThat(commentBucket.getKey(), equalTo(entry2.getKey()));
        assertThat(commentBucket.getDocCount(), equalTo((long) entry2.getValue().size()));

        TopHits topHits = commentBucket.getAggregations().get("top_comments");
        for (SearchHit searchHit : topHits.getHits().getHits()) {
          assertThat(entry2.getValue().contains(searchHit.getId()), is(true));
        }
      }
    }
  }
コード例 #3
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));
    }
  }
コード例 #4
0
  @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++;
    }
  }
コード例 #5
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--;
    }
  }
コード例 #6
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));
  }
コード例 #7
0
ファイル: BaseIndex.java プロジェクト: ihr/sonarqube
  public Map<String, Long> countByField(IndexField indexField, FilterBuilder filter) {
    Map<String, Long> counts = new HashMap<>();

    SearchRequestBuilder request =
        client
            .prepareSearch(this.getIndexName())
            .setTypes(this.getIndexType())
            .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter))
            .setSize(0)
            .addAggregation(
                AggregationBuilders.terms(indexField.field())
                    .field(indexField.field())
                    .order(Terms.Order.count(false))
                    .size(Integer.MAX_VALUE)
                    .minDocCount(0));

    SearchResponse response = request.get();

    Terms values = response.getAggregations().get(indexField.field());

    for (Terms.Bucket value : values.getBuckets()) {
      counts.put(value.getKey(), value.getDocCount());
    }
    return counts;
  }
コード例 #8
0
  @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));
    }
  }
コード例 #9
0
  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());
  }
コード例 #10
0
  @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));
    }
  }
コード例 #11
0
  @Test
  public void testSimpleSubAggregations() throws Exception {
    final String query =
        String.format(
            "SELECT /*! DOCS_WITH_AGGREGATION(10) */ * FROM %s/account GROUP BY (gender), (state) ",
            TEST_INDEX);

    SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
    SearchResponse response = (SearchResponse) select.get();
    Aggregations result = response.getAggregations();

    Terms gender = result.get("gender");
    for (Terms.Bucket genderBucket : gender.getBuckets()) {
      String genderKey = genderBucket.getKey();
      Assert.assertTrue("Gender should be m or f", genderKey.equals("m") || genderKey.equals("f"));
    }

    Assert.assertEquals(2, gender.getBuckets().size());

    Terms state = result.get("state");
    for (Terms.Bucket stateBucket : state.getBuckets()) {
      if (stateBucket.getKey().equalsIgnoreCase("ak")) {
        Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
      }
    }

    Assert.assertEquals(response.getHits().totalHits(), 1000);
    Assert.assertEquals(response.getHits().hits().length, 10);
  }
コード例 #12
0
  @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));
    }
  }
コード例 #13
0
ファイル: EsUtils.java プロジェクト: SonarSource/sonarqube
 public static LinkedHashMap<String, Long> termsToMap(Terms terms) {
   LinkedHashMap<String, Long> map = new LinkedHashMap<>();
   List<Terms.Bucket> buckets = terms.getBuckets();
   for (Terms.Bucket bucket : buckets) {
     map.put(bucket.getKeyAsString(), bucket.getDocCount());
   }
   return map;
 }
コード例 #14
0
ファイル: RandomTests.java プロジェクト: 289/elasticsearch
  // 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());
    }
  }
コード例 #15
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));
  }
コード例 #16
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));
      }
    }
  }
コード例 #17
0
ファイル: NestedIT.java プロジェクト: episerver/elasticsearch
  public void testNestNestedAggs() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx_nested_nested_aggs")
            .addAggregation(
                nested("level1", "nested1")
                    .subAggregation(
                        terms("a")
                            .field("nested1.a")
                            .collectMode(aggCollectionMode)
                            .subAggregation(
                                nested("level2", "nested1.nested2")
                                    .subAggregation(sum("sum").field("nested1.nested2.b")))))
            .get();
    assertSearchResponse(response);

    Nested level1 = response.getAggregations().get("level1");
    assertThat(level1, notNullValue());
    assertThat(level1.getName(), equalTo("level1"));
    assertThat(level1.getDocCount(), equalTo(2L));

    StringTerms a = level1.getAggregations().get("a");
    Terms.Bucket bBucket = a.getBucketByKey("a");
    assertThat(bBucket.getDocCount(), equalTo(1L));

    Nested level2 = bBucket.getAggregations().get("level2");
    assertThat(level2.getDocCount(), equalTo(1L));
    Sum sum = level2.getAggregations().get("sum");
    assertThat(sum.getValue(), equalTo(2d));

    a = level1.getAggregations().get("a");
    bBucket = a.getBucketByKey("b");
    assertThat(bBucket.getDocCount(), equalTo(1L));

    level2 = bBucket.getAggregations().get("level2");
    assertThat(level2.getDocCount(), equalTo(1L));
    sum = level2.getAggregations().get("sum");
    assertThat(sum.getValue(), equalTo(2d));
  }
コード例 #18
0
  private void assertDocCountErrorWithinBounds(
      int size, SearchResponse accurateResponse, SearchResponse testResponse) {
    Terms accurateTerms = accurateResponse.getAggregations().get("terms");
    assertThat(accurateTerms, notNullValue());
    assertThat(accurateTerms.getName(), equalTo("terms"));
    assertThat(accurateTerms.getDocCountError(), equalTo(0L));

    Terms testTerms = testResponse.getAggregations().get("terms");
    assertThat(testTerms, notNullValue());
    assertThat(testTerms.getName(), equalTo("terms"));
    assertThat(testTerms.getDocCountError(), greaterThanOrEqualTo(0L));
    Collection<Bucket> testBuckets = testTerms.getBuckets();
    assertThat(testBuckets.size(), lessThanOrEqualTo(size));
    assertThat(accurateTerms.getBuckets().size(), greaterThanOrEqualTo(testBuckets.size()));

    for (Terms.Bucket testBucket : testBuckets) {
      assertThat(testBucket, notNullValue());
      Terms.Bucket accurateBucket = accurateTerms.getBucketByKey(testBucket.getKeyAsString());
      assertThat(accurateBucket, notNullValue());
      assertThat(accurateBucket.getDocCountError(), equalTo(0L));
      assertThat(testBucket.getDocCountError(), lessThanOrEqualTo(testTerms.getDocCountError()));
      assertThat(
          testBucket.getDocCount() + testBucket.getDocCountError(),
          greaterThanOrEqualTo(accurateBucket.getDocCount()));
      assertThat(
          testBucket.getDocCount() - testBucket.getDocCountError(),
          lessThanOrEqualTo(accurateBucket.getDocCount()));
    }

    for (Terms.Bucket accurateBucket : accurateTerms.getBuckets()) {
      assertThat(accurateBucket, notNullValue());
      Terms.Bucket testBucket = accurateTerms.getBucketByKey(accurateBucket.getKeyAsString());
      if (testBucket == null) {
        assertThat(accurateBucket.getDocCount(), lessThanOrEqualTo(testTerms.getDocCountError()));
      }
    }
  }
コード例 #19
0
 /*
  * 所有姓氏为"Smith"的员工的最大的共同点(兴趣爱好)是什么
  * 聚合变成只包含和查询语句想匹配的文档
  */
 @Test
 public void queryAggregationTest() {
   Client client = ClientTemplate.getClient();
   SearchResponse response =
       client
           .prepareSearch("fccs")
           .setTypes("employee")
           .setQuery(QueryBuilders.matchQuery("lastName", "Smith"))
           .addAggregation(AggregationBuilders.terms("by_interests").field("interests"))
           .get();
   Terms terms = response.getAggregations().get("by_interests");
   for (Terms.Bucket bucket : terms.getBuckets()) {
     System.out.println("key:" + bucket.getKey() + " doc_count:" + bucket.getDocCount());
   }
   client.close();
 }
コード例 #20
0
  @Test
  public void testMetric_topLevel() throws Exception {
    SearchResponse response =
        client()
            .prepareSearch("idx")
            .addAggregation(
                terms("terms")
                    .field("tag")
                    .subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME)))
            .addAggregation(maxBucket("max_bucket").setBucketsPaths("terms>sum"))
            .execute()
            .actionGet();

    assertSearchResponse(response);

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

    List<String> maxKeys = new ArrayList<>();
    double maxValue = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < interval; ++i) {
      Terms.Bucket bucket = buckets.get(i);
      assertThat(bucket, notNullValue());
      assertThat((String) bucket.getKey(), equalTo("tag" + (i % interval)));
      assertThat(bucket.getDocCount(), greaterThan(0l));
      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 = response.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()])));
  }
コード例 #21
0
ファイル: BaseIndex.java プロジェクト: ihr/sonarqube
 // Response helpers
 protected Multimap<String, FacetValue> processAggregations(Aggregations aggregations) {
   Multimap<String, FacetValue> stats = ArrayListMultimap.create();
   if (aggregations != null) {
     for (Aggregation aggregation : aggregations.asList()) {
       if (aggregation instanceof StringTerms) {
         for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
           FacetValue facetValue = new FacetValue(value.getKey(), value.getDocCount());
           stats.put(aggregation.getName(), facetValue);
         }
       } else if (aggregation instanceof InternalValueCount) {
         InternalValueCount count = (InternalValueCount) aggregation;
         FacetValue facetValue = new FacetValue(count.getName(), count.getValue());
         stats.put(count.getName(), facetValue);
       }
     }
   }
   return stats;
 }
コード例 #22
0
  @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));
    }
  }
コード例 #23
0
  @Test
  public void testSubAggregations() throws Exception {
    Set expectedAges =
        new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
    final String query =
        String.format(
            "SELECT /*! DOCS_WITH_AGGREGATION(10) */"
                + " * FROM %s/account GROUP BY (gender, age), (state) LIMIT 0,10",
            TEST_INDEX);

    Map<String, Set<Integer>> buckets = new HashMap<>();

    SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
    SearchResponse response = (SearchResponse) select.get();
    Aggregations result = response.getAggregations();

    Terms gender = result.get("gender");
    for (Terms.Bucket genderBucket : gender.getBuckets()) {
      String genderKey = genderBucket.getKey();
      buckets.put(genderKey, new HashSet<Integer>());
      Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
      for (Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
        buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey()));
      }
    }

    Assert.assertEquals(2, buckets.keySet().size());
    Assert.assertEquals(expectedAges, buckets.get("m"));
    Assert.assertEquals(expectedAges, buckets.get("f"));

    Terms state = result.get("state");
    for (Terms.Bucket stateBucket : state.getBuckets()) {
      if (stateBucket.getKey().equalsIgnoreCase("ak")) {
        Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
      }
    }

    Assert.assertEquals(response.getHits().totalHits(), 1000);
    Assert.assertEquals(response.getHits().hits().length, 10);
  }
コード例 #24
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));
    }
  }
コード例 #25
0
 @Override
 public Map<String, Integer> countTermsByField(SearchCriteria searchCriteria, String field) {
   ElasticSearchCriteria criteria = (ElasticSearchCriteria) searchCriteria;
   if (criteria == null) return null;
   SearchRequestBuilder srb = criteria2builder(criteria);
   srb.setFrom(0);
   srb.setSize(0);
   TermsBuilder tb = AggregationBuilders.terms(field);
   tb.field(field);
   srb.addAggregation(tb);
   try {
     SearchResponse response = srb.execute().get();
     StringTerms aggr = response.getAggregations().get(field);
     Map<String, Integer> result = new LinkedHashMap<>();
     for (Terms.Bucket bucket : aggr.getBuckets()) {
       result.put(bucket.getKey(), (int) bucket.getDocCount());
     }
     return result;
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return Collections.emptyMap();
 }
コード例 #26
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));
    }
  }
コード例 #27
0
ファイル: NestedIT.java プロジェクト: episerver/elasticsearch
  public void testNestedSameDocIdProcessedMultipleTime() throws Exception {
    assertAcked(
        prepareCreate("idx4")
            .setSettings(
                Settings.builder()
                    .put(SETTING_NUMBER_OF_SHARDS, 1)
                    .put(SETTING_NUMBER_OF_REPLICAS, 0))
            .addMapping(
                "product",
                "categories",
                "type=text",
                "name",
                "type=text",
                "property",
                "type=nested"));
    ensureGreen("idx4");

    client()
        .prepareIndex("idx4", "product", "1")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("name", "product1")
                .field("categories", "1", "2", "3", "4")
                .startArray("property")
                .startObject()
                .field("id", 1)
                .endObject()
                .startObject()
                .field("id", 2)
                .endObject()
                .startObject()
                .field("id", 3)
                .endObject()
                .endArray()
                .endObject())
        .get();
    client()
        .prepareIndex("idx4", "product", "2")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("name", "product2")
                .field("categories", "1", "2")
                .startArray("property")
                .startObject()
                .field("id", 1)
                .endObject()
                .startObject()
                .field("id", 5)
                .endObject()
                .startObject()
                .field("id", 4)
                .endObject()
                .endArray()
                .endObject())
        .get();
    refresh();

    SearchResponse response =
        client()
            .prepareSearch("idx4")
            .setTypes("product")
            .addAggregation(
                terms("category")
                    .field("categories")
                    .subAggregation(
                        nested("property", "property")
                            .subAggregation(terms("property_id").field("property.id"))))
            .get();
    assertNoFailures(response);
    assertHitCount(response, 2);

    Terms category = response.getAggregations().get("category");
    assertThat(category.getBuckets().size(), equalTo(4));

    Terms.Bucket bucket = category.getBucketByKey("1");
    assertThat(bucket.getDocCount(), equalTo(2L));
    Nested property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    Terms propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));

    bucket = category.getBucketByKey("2");
    assertThat(bucket.getDocCount(), equalTo(2L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));

    bucket = category.getBucketByKey("3");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));

    bucket = category.getBucketByKey("4");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
  }
コード例 #28
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));
  }
コード例 #29
0
ファイル: NestedIT.java プロジェクト: episerver/elasticsearch
  // Test based on: https://github.com/elastic/elasticsearch/issues/9280
  public void testParentFilterResolvedCorrectly() throws Exception {
    XContentBuilder mapping =
        jsonBuilder()
            .startObject()
            .startObject("provider")
            .startObject("properties")
            .startObject("comments")
            .field("type", "nested")
            .startObject("properties")
            .startObject("cid")
            .field("type", "long")
            .endObject()
            .startObject("identifier")
            .field("type", "keyword")
            .endObject()
            .startObject("tags")
            .field("type", "nested")
            .startObject("properties")
            .startObject("tid")
            .field("type", "long")
            .endObject()
            .startObject("name")
            .field("type", "keyword")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .startObject("dates")
            .field("type", "object")
            .startObject("properties")
            .startObject("day")
            .field("type", "date")
            .field("format", "dateOptionalTime")
            .endObject()
            .startObject("month")
            .field("type", "object")
            .startObject("properties")
            .startObject("end")
            .field("type", "date")
            .field("format", "dateOptionalTime")
            .endObject()
            .startObject("start")
            .field("type", "date")
            .field("format", "dateOptionalTime")
            .endObject()
            .startObject("label")
            .field("type", "keyword")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject();
    assertAcked(
        prepareCreate("idx2")
            .setSettings(
                Settings.builder()
                    .put(SETTING_NUMBER_OF_SHARDS, 1)
                    .put(SETTING_NUMBER_OF_REPLICAS, 0))
            .addMapping("provider", mapping));
    ensureGreen("idx2");

    List<IndexRequestBuilder> indexRequests = new ArrayList<>(2);
    indexRequests.add(
        client()
            .prepareIndex("idx2", "provider", "1")
            .setSource(
                "{\"dates\": {\"month\": {\"label\": \"2014-11\", \"end\": \"2014-11-30\", \"start\": \"2014-11-01\"}, \"day\": \"2014-11-30\"}, \"comments\": [{\"cid\": 3,\"identifier\": \"29111\"}, {\"cid\": 4,\"tags\": [{\"tid\" :44,\"name\": \"Roles\"}], \"identifier\": \"29101\"}]}"));
    indexRequests.add(
        client()
            .prepareIndex("idx2", "provider", "2")
            .setSource(
                "{\"dates\": {\"month\": {\"label\": \"2014-12\", \"end\": \"2014-12-31\", \"start\": \"2014-12-01\"}, \"day\": \"2014-12-03\"}, \"comments\": [{\"cid\": 1, \"identifier\": \"29111\"}, {\"cid\": 2,\"tags\": [{\"tid\" : 22, \"name\": \"DataChannels\"}], \"identifier\": \"29101\"}]}"));
    indexRandom(true, indexRequests);

    SearchResponse response =
        client()
            .prepareSearch("idx2")
            .setTypes("provider")
            .addAggregation(
                terms("startDate")
                    .field("dates.month.start")
                    .subAggregation(
                        terms("endDate")
                            .field("dates.month.end")
                            .subAggregation(
                                terms("period")
                                    .field("dates.month.label")
                                    .subAggregation(
                                        nested("ctxt_idfier_nested", "comments")
                                            .subAggregation(
                                                filter(
                                                        "comment_filter",
                                                        termQuery("comments.identifier", "29111"))
                                                    .subAggregation(
                                                        nested("nested_tags", "comments.tags")
                                                            .subAggregation(
                                                                terms("tag")
                                                                    .field(
                                                                        "comments.tags.name"))))))))
            .get();
    assertNoFailures(response);
    assertHitCount(response, 2);

    Terms startDate = response.getAggregations().get("startDate");
    assertThat(startDate.getBuckets().size(), equalTo(2));
    Terms.Bucket bucket = startDate.getBucketByKey("2014-11-01T00:00:00.000Z");
    assertThat(bucket.getDocCount(), equalTo(1L));
    Terms endDate = bucket.getAggregations().get("endDate");
    bucket = endDate.getBucketByKey("2014-11-30T00:00:00.000Z");
    assertThat(bucket.getDocCount(), equalTo(1L));
    Terms period = bucket.getAggregations().get("period");
    bucket = period.getBucketByKey("2014-11");
    assertThat(bucket.getDocCount(), equalTo(1L));
    Nested comments = bucket.getAggregations().get("ctxt_idfier_nested");
    assertThat(comments.getDocCount(), equalTo(2L));
    Filter filter = comments.getAggregations().get("comment_filter");
    assertThat(filter.getDocCount(), equalTo(1L));
    Nested nestedTags = filter.getAggregations().get("nested_tags");
    assertThat(nestedTags.getDocCount(), equalTo(0L)); // This must be 0
    Terms tags = nestedTags.getAggregations().get("tag");
    assertThat(tags.getBuckets().size(), equalTo(0)); // and this must be empty

    bucket = startDate.getBucketByKey("2014-12-01T00:00:00.000Z");
    assertThat(bucket.getDocCount(), equalTo(1L));
    endDate = bucket.getAggregations().get("endDate");
    bucket = endDate.getBucketByKey("2014-12-31T00:00:00.000Z");
    assertThat(bucket.getDocCount(), equalTo(1L));
    period = bucket.getAggregations().get("period");
    bucket = period.getBucketByKey("2014-12");
    assertThat(bucket.getDocCount(), equalTo(1L));
    comments = bucket.getAggregations().get("ctxt_idfier_nested");
    assertThat(comments.getDocCount(), equalTo(2L));
    filter = comments.getAggregations().get("comment_filter");
    assertThat(filter.getDocCount(), equalTo(1L));
    nestedTags = filter.getAggregations().get("nested_tags");
    assertThat(nestedTags.getDocCount(), equalTo(0L)); // This must be 0
    tags = nestedTags.getAggregations().get("tag");
    assertThat(tags.getBuckets().size(), equalTo(0)); // and this must be empty
  }
コード例 #30
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));
    }
  }