private AbstractAggregationBuilder addFieldOrScriptToAggregation(
     MethodField field, ValuesSourceMetricsAggregationBuilder builder) {
   KVValue kvValue = field.getParams().get(0);
   if (kvValue.key != null && kvValue.key.equals("script")) {
     // todo: support different lang script
     return builder.script(((MethodField) kvValue.value).getParams().get(1).toString());
   } else if (kvValue.key != null
       && (kvValue.key.equals("nested") || kvValue.key.equals("reverse_nested"))) {
     NestedType nestedType = (NestedType) kvValue.value;
     builder.field(nestedType.field);
     AggregationBuilder nestedBuilder;
     String nestedAggName = nestedType.field + "@NESTED";
     if (nestedType.isReverse()) {
       if (nestedType.path != null && nestedType.path.startsWith("~")) {
         String realPath = nestedType.path.substring(1);
         nestedBuilder = AggregationBuilders.nested(nestedAggName).path(realPath);
         nestedBuilder = nestedBuilder.subAggregation(builder);
         return AggregationBuilders.reverseNested(nestedAggName + "_REVERSED")
             .subAggregation(nestedBuilder);
       } else {
         nestedBuilder = AggregationBuilders.reverseNested(nestedAggName).path(nestedType.path);
       }
     } else {
       nestedBuilder = AggregationBuilders.nested(nestedAggName).path(nestedType.path);
     }
     return nestedBuilder.subAggregation(builder);
   }
   return builder.field(kvValue.toString());
 }
 /*
  * 统计每种兴趣下职工的平均年龄
  * 聚合也允许分级汇总
  */
 @Test
 public void subAggregationTest() {
   Client client = ClientTemplate.getClient();
   SearchResponse response =
       client
           .prepareSearch("fccs")
           .setTypes("employee")
           .addAggregation(
               AggregationBuilders.terms("by_interests")
                   .field("interests")
                   .subAggregation(AggregationBuilders.avg("avg_age").field("age")))
           .get();
   Terms terms = response.getAggregations().get("by_interests");
   List<Bucket> buckets = terms.getBuckets();
   for (Bucket bucket : buckets) {
     Avg avg = bucket.getAggregations().get("avg_age");
     System.out.println(
         "key:"
             + bucket.getKey()
             + " doc_count:"
             + bucket.getDocCount()
             + " avg_age:"
             + avg.getValue());
   }
   client.close();
 }
  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());
  }
 private AggregationBuilder createNestedAggregation(Field field) {
   AggregationBuilder nestedBuilder;
   String nestedPath = field.getNestedPath();
   if (field.isReverseNested()) {
     if (nestedPath == null || !nestedPath.startsWith("~"))
       return AggregationBuilders.reverseNested(getNestedAggName(field)).path(nestedPath);
     nestedPath = nestedPath.substring(1);
   }
   nestedBuilder = AggregationBuilders.nested(getNestedAggName(field)).path(nestedPath);
   return nestedBuilder;
 }
  public void run() {
    Client client = null;
    TransportClient transportClient = null;
    try {
      transportClient = new TransportClient();
      client =
          transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9300));

      SearchResponse response =
          client
              .prepareSearch("tms-allflat")
              .setTypes("personal")
              .setQuery(QueryBuilders.matchAllQuery())
              .addAggregation(
                  AggregationBuilders.terms("aggs1")
                      .field("skill_1")
                      .size(20)
                      .order(Terms.Order.count(false)))
              .execute()
              .actionGet();

      Terms terms = response.getAggregations().get("aggs1");
      terms
          .getBuckets()
          .stream()
          .forEach(s -> System.out.println(s.getKeyAsText() + "(" + s.getDocCount() + ")"));

    } finally {
      transportClient.close();
      client.close();
    }
  }
  @Test
  public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
    TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
    SearchResponse response =
        client
            .prepareSearch("blog")
            .setTypes("article")
            .addAggregation(aggregation)
            .execute()
            .actionGet();

    Map<String, Aggregation> results = response.getAggregations().asMap();
    StringTerms topTags = (StringTerms) results.get("top_tags");

    List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList());
    Collections.sort(keys);
    assertEquals(
        asList(
            "about",
            "article",
            "data",
            "elasticsearch",
            "engines",
            "search",
            "second",
            "spring",
            "tutorial"),
        keys);
  }
  private ValuesSourceAggregationBuilder<?> dateRange(MethodField field) {
    String alias = gettAggNameFromParamsOrAlias(field);
    DateRangeBuilder dateRange = AggregationBuilders.dateRange(alias).format(TIME_FARMAT);

    String value = null;
    List<String> ranges = new ArrayList<>();
    for (KVValue kv : field.getParams()) {
      value = kv.value.toString();
      if ("field".equals(kv.key)) {
        dateRange.field(value);
        continue;
      } else if ("format".equals(kv.key)) {
        dateRange.format(value);
        continue;
      } else if ("from".equals(kv.key)) {
        dateRange.addUnboundedFrom(kv.value);
        continue;
      } else if ("to".equals(kv.key)) {
        dateRange.addUnboundedTo(kv.value);
        continue;
      } else if ("alias".equals(kv.key)) {
        continue;
      } else {
        ranges.add(value);
      }
    }

    for (int i = 1; i < ranges.size(); i++) {
      dateRange.addRange(ranges.get(i - 1), ranges.get(i));
    }

    return dateRange;
  }
 private AggregationBuilder<?> termsAgg(MethodField field) throws SqlParseException {
   String aggName = gettAggNameFromParamsOrAlias(field);
   TermsBuilder terms = AggregationBuilders.terms(aggName);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "field":
         terms.field(value);
         break;
       case "size":
         terms.size(Integer.parseInt(value));
         break;
       case "shard_size":
         terms.shardSize(Integer.parseInt(value));
         break;
       case "min_doc_count":
         terms.minDocCount(Integer.parseInt(value));
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       default:
         throw new SqlParseException("terms aggregation err or not define field " + kv.toString());
     }
   }
   return terms;
 }
 private AggregationBuilder<?> geohashGrid(MethodField field) throws SqlParseException {
   String aggName = gettAggNameFromParamsOrAlias(field);
   GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "precision":
         geoHashGrid.precision(Integer.parseInt(value));
         break;
       case "field":
         geoHashGrid.field(value);
         break;
       case "size":
         geoHashGrid.size(Integer.parseInt(value));
         break;
       case "shard_size":
         geoHashGrid.shardSize(Integer.parseInt(value));
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       default:
         throw new SqlParseException("geohash grid err or not define field " + kv.toString());
     }
   }
   return geoHashGrid;
 }
Beispiel #10
0
  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;
  }
 public void testDisabledAggsDynamicScripts() {
   // dynamic scripts don't need to be enabled for an indexed script to be indexed and later on
   // executed
   assertAcked(
       client()
           .admin()
           .cluster()
           .preparePutStoredScript()
           .setScriptLang(GroovyScriptEngineService.NAME)
           .setId("script1")
           .setSource(new BytesArray("{\"script\":\"2\"}")));
   client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}").get();
   refresh();
   SearchResponse searchResponse =
       client()
           .prepareSearch("test")
           .setSource(
               new SearchSourceBuilder()
                   .aggregation(
                       AggregationBuilders.terms("test")
                           .script(new Script("script1", ScriptType.STORED, null, null))))
           .get();
   assertHitCount(searchResponse, 1);
   assertThat(searchResponse.getAggregations().get("test"), notNullValue());
 }
Beispiel #12
0
  @Override
  @CheckForNull
  public Date getLastSynchronization(Map<String, String> params) {
    SearchRequestBuilder request =
        client
            .prepareSearch(this.getIndexName())
            .setTypes(this.getIndexType())
            .setQuery(
                QueryBuilders.filteredQuery(
                    QueryBuilders.matchAllQuery(), getLastSynchronizationBuilder(params)))
            .setSize(0)
            .addAggregation(
                AggregationBuilders.max("latest").field(BaseNormalizer.UPDATED_AT_FIELD));

    SearchResponse response = request.get();

    Max max = response.getAggregations().get("latest");
    if (max.getValue() > 0) {
      Date date = new DateTime((long) max.getValue()).toDate();
      LOG.debug(
          "Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date);
      return date;
    } else {
      LOG.debug("Index {}:{} has no last update date", this.getIndexName(), this.getIndexType());
      return null;
    }
  }
  @Test
  public void testDynamicTemplateCopyTo() throws Exception {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareCreate("test-idx")
            .addMapping("doc", createDynamicTemplateMapping()));

    int recordCount = between(1, 200);

    for (int i = 0; i < recordCount * 2; i++) {
      client()
          .prepareIndex("test-idx", "doc", Integer.toString(i))
          .setSource("test_field", "test " + i, "even", i % 2 == 0)
          .get();
    }
    client().admin().indices().prepareRefresh("test-idx").execute().actionGet();

    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());

    SearchResponse response =
        client()
            .prepareSearch("test-idx")
            .setQuery(QueryBuilders.termQuery("even", true))
            .addAggregation(
                AggregationBuilders.terms("test")
                    .field("test_field")
                    .size(recordCount * 2)
                    .collectMode(aggCollectionMode))
            .addAggregation(
                AggregationBuilders.terms("test_raw")
                    .field("test_field_raw")
                    .size(recordCount * 2)
                    .collectMode(aggCollectionMode))
            .execute()
            .actionGet();

    assertThat(response.getHits().totalHits(), equalTo((long) recordCount));

    assertThat(
        ((Terms) response.getAggregations().get("test")).getBuckets().size(),
        equalTo(recordCount + 1));
    assertThat(
        ((Terms) response.getAggregations().get("test_raw")).getBuckets().size(),
        equalTo(recordCount));
  }
 private AbstractAggregationBuilder wrapNestedIfNeeded(
     AggregationBuilder nestedBuilder, boolean reverseNested) {
   if (!reverseNested) return nestedBuilder;
   if (reverseNested && !(nestedBuilder instanceof NestedBuilder)) return nestedBuilder;
   // we need to jump back to root
   return AggregationBuilders.reverseNested(nestedBuilder.getName() + "_REVERSED")
       .subAggregation(nestedBuilder);
 }
  /**
   * 分组查的聚合函数
   *
   * @param field
   * @return
   * @throws SqlParseException
   */
  public AggregationBuilder<?> makeGroupAgg(Field field) throws SqlParseException {
    if (field instanceof MethodField) {

      MethodField methodField = (MethodField) field;
      if (methodField.getName().equals("filter")) {
        Map<String, Object> paramsAsMap = methodField.getParamsAsMap();
        Where where = (Where) paramsAsMap.get("where");
        return AggregationBuilders.filter(paramsAsMap.get("alias").toString())
            .filter(FilterMaker.explan(where));
      }
      return makeRangeGroup(methodField);
    } else {
      TermsBuilder termsBuilder = AggregationBuilders.terms(field.getName()).field(field.getName());
      groupMap.put(field.getName(), new KVValue("KEY", termsBuilder));
      return termsBuilder;
    }
  }
 private AggregationBuilder<?> updateAggIfNested(AggregationBuilder<?> lastAgg, Field field) {
   if (field.isNested()) {
     lastAgg =
         AggregationBuilders.nested(field.getName() + "Nested")
             .path(field.getNestedPath())
             .subAggregation(lastAgg);
   }
   return lastAgg;
 }
  /**
   * Create count aggregation.
   *
   * @param field The count function
   * @return AggregationBuilder use to count result
   */
  private AbstractAggregationBuilder makeCountAgg(MethodField field) {

    // Cardinality is approximate DISTINCT.
    if ("DISTINCT".equals(field.getOption())) {
      return AggregationBuilders.cardinality(field.getAlias())
          .precisionThreshold(40000)
          .field(field.getParams().get(0).value.toString());
    }

    String fieldName = field.getParams().get(0).value.toString();

    // In case of count(*) we use '_index' as field parameter to count all documents
    if ("*".equals(fieldName)) {
      return AggregationBuilders.count(field.getAlias()).field("_index");
    } else {
      return AggregationBuilders.count(field.getAlias()).field(fieldName);
    }
  }
  public Map<QualityProfileKey, Multimap<String, FacetValue>> getStatsByProfileKey(
      List<QualityProfileKey> keys) {

    String[] stringKeys = new String[keys.size()];
    for (int i = 0; i < keys.size(); i++) {
      stringKeys[i] = keys.get(i).toString();
    }

    SearchResponse response =
        getClient()
            .prepareSearch(this.getIndexName())
            .setQuery(
                QueryBuilders.filteredQuery(
                    QueryBuilders.matchAllQuery(),
                    FilterBuilders.termsFilter(
                        ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), stringKeys)))
            .addAggregation(
                AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field())
                    .field(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field())
                    .subAggregation(
                        AggregationBuilders.terms(
                                ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())
                            .field(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()))
                    .subAggregation(
                        AggregationBuilders.terms(
                                ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())
                            .field(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())))
            .setSize(0)
            .setTypes(this.getIndexType())
            .get();

    Map<QualityProfileKey, Multimap<String, FacetValue>> stats =
        new HashMap<QualityProfileKey, Multimap<String, FacetValue>>();
    Aggregation aggregation =
        response.getAggregations().get(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field());
    for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
      stats.put(
          QualityProfileKey.parse(value.getKey()),
          this.processAggregations(value.getAggregations()));
    }

    return stats;
  }
Beispiel #19
0
  @Override
  public void runAggregations() {
    System.out.println("get aggregations:");

    System.out.println("connecting cluster...");
    Node node = nodeBuilder().clusterName("ron-es").client(true).node();
    Client client = node.client();

    AbstractAggregationBuilder maxAgg = AggregationBuilders.max("max-km").field("km");
    AbstractAggregationBuilder minAgg = AggregationBuilders.min("min-km").field("km");
    AbstractAggregationBuilder avgAgg = AggregationBuilders.avg("avg-km").field("km");
    AbstractAggregationBuilder sumAgg = AggregationBuilders.sum("sum-km").field("km");

    String startTime = Util.getDateTimeNow();

    SearchResponse response =
        client
            .prepareSearch("vehicles")
            .setTypes("cars")
            .addAggregation(maxAgg)
            .addAggregation(minAgg)
            .addAggregation(avgAgg)
            .addAggregation(sumAgg)
            .execute()
            .actionGet();

    String stopTime = Util.getDateTimeNow();
    _timeElapssed = Util.getDateDiff(startTime, stopTime);

    // SearchResponse response =
    // client.prepareSearch("vehicles").setTypes("cars").addAggregation(AggregationBuilders.max("max-km").field("km")).execute().actionGet();

    client.close();

    System.out.println("node closed");

    // SearchResponse response = client.prepareSearch().execute().actionGet();
    // Stats agg = response.getAggregations().get("km");
    // double max = agg.getMax();

    System.out.println(response);
  }
  @Override
  public void execute(Client esClient, String index, boolean verbose) {
    FilterBuilder fb =
        new OrFilterBuilder(
            new TermFilterBuilder("eventName", "TopBrowser"),
            new TermFilterBuilder("data.playerEvent", "str-start"));

    AbstractAggregationBuilder aggregations =
        AggregationBuilders.terms("by_browser")
            .field("data.browser")
            .size(50)
            .subAggregation(AggregationBuilders.sum("sum").field("counter"))
            .order(Terms.Order.aggregation("sum", false));

    if (index.equals("")) {
      this.searchResponse =
          esClient
              .prepareSearch()
              .setQuery(
                  org.elasticsearch.index.query.QueryBuilders.filteredQuery(
                      new MatchAllQueryBuilder(), fb))
              .setSize(50)
              .setPostFilter(fb)
              .addAggregation(aggregations)
              .get();
    } else {
      this.searchResponse =
          esClient
              .prepareSearch(index)
              .setQuery(
                  org.elasticsearch.index.query.QueryBuilders.filteredQuery(
                      new MatchAllQueryBuilder(), fb))
              .setSize(50)
              .setPostFilter(fb)
              .addAggregation(aggregations)
              .get();
    }
    fileLog.info(String.valueOf(this.searchResponse.getTookInMillis()));
    if (verbose) printResult();
  }
  public void testSpecialValueVariable() throws Exception {
    // i.e. _value for aggregations
    createIndex("test");
    ensureGreen("test");
    indexRandom(
        true,
        client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2),
        client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4),
        client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8));

    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.matchAllQuery())
        .addAggregation(
            AggregationBuilders.stats("int_agg")
                .field("x")
                .script(
                    new Script(
                        "_value * 3", ScriptType.INLINE, ExpressionScriptEngineService.NAME, null)))
        .addAggregation(
            AggregationBuilders.stats("double_agg")
                .field("y")
                .script(
                    new Script(
                        "_value - 1.1",
                        ScriptType.INLINE,
                        ExpressionScriptEngineService.NAME,
                        null)));

    SearchResponse rsp = req.get();
    assertEquals(3, rsp.getHits().getTotalHits());

    Stats stats = rsp.getAggregations().get("int_agg");
    assertEquals(39.0, stats.getMax(), 0.0001);
    assertEquals(15.0, stats.getMin(), 0.0001);

    stats = rsp.getAggregations().get("double_agg");
    assertEquals(0.7, stats.getMax(), 0.0001);
    assertEquals(0.1, stats.getMin(), 0.0001);
  }
 private HistogramBuilder histogram(MethodField field) throws SqlParseException {
   String aggName = gettAggNameFromParamsOrAlias(field);
   HistogramBuilder histogram = AggregationBuilders.histogram(aggName);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "interval":
         histogram.interval(Long.parseLong(value));
         break;
       case "field":
         histogram.field(value);
         break;
       case "min_doc_count":
         histogram.minDocCount(Long.parseLong(value));
         break;
       case "extended_bounds":
         String[] bounds = value.split(":");
         if (bounds.length == 2)
           histogram.extendedBounds(Long.valueOf(bounds[0]), Long.valueOf(bounds[1]));
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       case "order":
         Histogram.Order order = null;
         switch (value) {
           case "key_desc":
             order = Histogram.Order.KEY_DESC;
             break;
           case "count_asc":
             order = Histogram.Order.COUNT_ASC;
             break;
           case "count_desc":
             order = Histogram.Order.COUNT_DESC;
             break;
           case "key_asc":
           default:
             order = Histogram.Order.KEY_ASC;
             break;
         }
         histogram.order(order);
         break;
       default:
         throw new SqlParseException("histogram err or not define field " + kv.toString());
     }
   }
   return histogram;
 }
 /*
  * 所有姓氏为"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();
 }
  /**
   * 构建范围查询
   *
   * @param field
   * @return
   */
  private RangeBuilder rangeBuilder(MethodField field) {

    LinkedList<KVValue> params = new LinkedList<>(field.getParams());

    String fieldName = params.poll().toString();

    double[] ds = Util.KV2DoubleArr(params);

    RangeBuilder range = AggregationBuilders.range(field.getAlias()).field(fieldName);

    for (int i = 1; i < ds.length; i++) {
      range.addRange(ds[i - 1], ds[i]);
    }

    return range;
  }
  @Test
  public void
      givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
    TermsBuilder aggregation =
        AggregationBuilders.terms("top_tags")
            .field("tags")
            .order(Terms.Order.aggregation("_count", false));
    SearchResponse response =
        client
            .prepareSearch("blog")
            .setTypes("article")
            .addAggregation(aggregation)
            .execute()
            .actionGet();

    Map<String, Aggregation> results = response.getAggregations().asMap();
    StringTerms topTags = (StringTerms) results.get("top_tags");

    List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList());
    assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
  }
  public void testStringSpecialValueVariable() throws Exception {
    // i.e. expression script for term aggregations, which is not allowed
    createIndex("test");
    ensureGreen("test");
    indexRandom(
        true,
        client().prepareIndex("test", "doc", "1").setSource("text", "hello"),
        client().prepareIndex("test", "doc", "2").setSource("text", "goodbye"),
        client().prepareIndex("test", "doc", "3").setSource("text", "hello"));

    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.matchAllQuery())
        .addAggregation(
            AggregationBuilders.terms("term_agg")
                .field("text")
                .script(
                    new Script(
                        "_value", ScriptType.INLINE, ExpressionScriptEngineService.NAME, null)));

    String message;
    try {
      // shards that don't have docs with the "text" field will not fail,
      // so we may or may not get a total failure
      SearchResponse rsp = req.get();
      assertThat(
          rsp.getShardFailures().length,
          greaterThan(0)); // at least the shards containing the docs should have failed
      message = rsp.getShardFailures()[0].reason();
    } catch (SearchPhaseExecutionException e) {
      message = e.toString();
    }
    assertThat(
        message + "should have contained ExpressionScriptExecutionException",
        message.contains("ExpressionScriptExecutionException"),
        equalTo(true));
    assertThat(
        message + "should have contained text variable error",
        message.contains("text variable"),
        equalTo(true));
  }
 /**
  * 按照时间范围分组
  *
  * @param field
  * @return
  * @throws SqlParseException
  */
 private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseException {
   String alias = gettAggNameFromParamsOrAlias(field);
   DateHistogramBuilder dateHistogram =
       AggregationBuilders.dateHistogram(alias).format(TIME_FARMAT);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "interval":
         dateHistogram.interval(new DateHistogram.Interval(kv.value.toString()));
         break;
       case "field":
         dateHistogram.field(value);
         break;
       case "format":
         dateHistogram.format(value);
         break;
       case "time_zone":
       case "pre_zone":
         dateHistogram.preZone(value);
         break;
       case "post_zone":
         dateHistogram.postZone(value);
         break;
       case "post_offset":
         dateHistogram.postOffset(value);
         break;
       case "pre_offset":
         dateHistogram.preOffset(value);
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       default:
         throw new SqlParseException("date range err or not define field " + kv.toString());
     }
   }
   return dateHistogram;
 }
 private AggregationBuilder<?> geoBounds(MethodField field) throws SqlParseException {
   String aggName = gettAggNameFromParamsOrAlias(field);
   GeoBoundsBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
   String value = null;
   for (KVValue kv : field.getParams()) {
     value = kv.value.toString();
     switch (kv.key.toLowerCase()) {
       case "field":
         boundsBuilder.field(value);
         break;
       case "wrap_longitude":
         boundsBuilder.wrapLongitude(Boolean.getBoolean(value));
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       default:
         throw new SqlParseException("geo_bounds err or not define field " + kv.toString());
     }
   }
   return boundsBuilder;
 }
 @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();
 }
 /**
  * TOPHITS查询
  *
  * @param field
  * @return
  */
 private AbstractAggregationBuilder makeTopHitsAgg(MethodField field) {
   String alias = gettAggNameFromParamsOrAlias(field);
   TopHitsBuilder topHits = AggregationBuilders.topHits(alias);
   List<KVValue> params = field.getParams();
   for (KVValue kv : params) {
     switch (kv.key) {
       case "from":
         topHits.setFrom((int) kv.value);
         break;
       case "size":
         topHits.setSize((int) kv.value);
         break;
       case "alias":
       case "nested":
       case "reverse_nested":
         break;
       default:
         topHits.addSort(kv.key, SortOrder.valueOf(kv.value.toString().toUpperCase()));
         break;
     }
   }
   return topHits;
 }