Exemplo n.º 1
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();
  }
Exemplo n.º 3
0
 /**
  * Create aggregation according to the SQL function.
  *
  * @param field SQL function
  * @param parent parentAggregation
  * @return AggregationBuilder represents the SQL function
  * @throws SqlParseException in case of unrecognized function
  */
 public AbstractAggregationBuilder makeFieldAgg(
     MethodField field, AbstractAggregationBuilder parent) throws SqlParseException {
   groupMap.put(field.getAlias(), new KVValue("FIELD", parent));
   ValuesSourceMetricsAggregationBuilder builder;
   field.setAlias(fixAlias(field.getAlias()));
   switch (field.getName().toUpperCase()) {
     case "SUM":
       builder = AggregationBuilders.sum(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "MAX":
       builder = AggregationBuilders.max(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "MIN":
       builder = AggregationBuilders.min(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "AVG":
       builder = AggregationBuilders.avg(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "STATS":
       builder = AggregationBuilders.stats(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "EXTENDED_STATS":
       builder = AggregationBuilders.extendedStats(field.getAlias());
       return addFieldOrScriptToAggregation(field, builder);
     case "PERCENTILES":
       builder = AggregationBuilders.percentiles(field.getAlias());
       addSpecificPercentiles((PercentilesBuilder) builder, field.getParams());
       return addFieldOrScriptToAggregation(field, builder);
     case "TOPHITS":
       return makeTopHitsAgg(field);
     case "SCRIPTED_METRIC":
       return scriptedMetric(field);
     case "COUNT":
       groupMap.put(field.getAlias(), new KVValue("COUNT", parent));
       return makeCountAgg(field);
     default:
       throw new SqlParseException("the agg function not to define !");
   }
 }