public static void loadAggregationResults(
      ResponsePojo rp,
      Facets facets,
      Aggregations aggs,
      AggregationOutputPojo aggOutParams,
      ScoringUtils scoreStats,
      AliasLookupTable aliasLookup,
      String[] entityTypeFilterStrings,
      String[] assocVerbFilterStrings,
      AggregationUtils.GeoContainer extraAliasAggregatedGeo) {
    HashMap<String, List<? extends Object>> moments = null;

    if ((null != facets) && (null != facets.getFacets()))
      for (Map.Entry<String, Facet> facet : facets.getFacets().entrySet()) {
        // Geo

        if (facet.getKey().equals("geo")) {
          TermsFacet geoFacet = (TermsFacet) facet.getValue();
          Set<GeoAggregationPojo> geoCounts = null;
          int nHighestCount = -1;
          int nLowestCount = Integer.MAX_VALUE;
          // If we've got some geotags from the alias masters then start with them:
          if ((null != extraAliasAggregatedGeo) && (null != extraAliasAggregatedGeo.geotags)) {
            geoCounts = extraAliasAggregatedGeo.geotags;
            nHighestCount = (int) extraAliasAggregatedGeo.minCount;
            nLowestCount = (int) extraAliasAggregatedGeo.maxCount;
          } else {
            geoCounts = new TreeSet<GeoAggregationPojo>();
          }
          for (TermsFacet.Entry geo : geoFacet.getEntries()) {
            String geohash = FacetUtils.getTerm(geo).substring(2);
            double[] loc = GeoHashUtils.decode(geohash);
            GeoAggregationPojo geoObj = new GeoAggregationPojo(loc[0], loc[1]);
            geoObj.count = geo.getCount();
            geoObj.type = GeoOntologyMapping.decodeOntologyCode(FacetUtils.getTerm(geo).charAt(0));
            geoCounts.add(geoObj);
            // (note this aggregates geo points whose decoded lat/logns are the same, which can
            // result in slightly fewer records than requested)
            // (note the aggregation writes the aggregated count into geoObj.count)

            if (geoObj.count
                > nHighestCount) { // (the counts can be modified by the add command above)
              nHighestCount = geo.getCount();
            }
            if (geoObj.count < nLowestCount) {
              nLowestCount = geo.getCount();
            }
          }
          rp.setGeo(geoCounts, nHighestCount, nLowestCount);
        } // (TESTED)
        if (facet.getKey().equals("time")) {
          DateHistogramFacet timeFacet = (DateHistogramFacet) facet.getValue();
          rp.setTimes(
              timeFacet.getEntries(), QueryHandler.getInterval(aggOutParams.timesInterval, 'm'));
        } // (TESTED)

        if (facet.getKey().equals("events")) {
          TermsFacet eventsFacet = (TermsFacet) facet.getValue();
          rp.setEvents(
              parseEventAggregationOutput(
                  "Event",
                  eventsFacet,
                  scoreStats,
                  aliasLookup,
                  entityTypeFilterStrings,
                  assocVerbFilterStrings));
        }
        if (facet.getKey().equals("facts")) {
          TermsFacet factsFacet = (TermsFacet) facet.getValue();
          rp.setFacts(
              parseEventAggregationOutput(
                  "Fact",
                  factsFacet,
                  scoreStats,
                  aliasLookup,
                  entityTypeFilterStrings,
                  assocVerbFilterStrings));
        }
        // TESTED x2

        if (facet.getKey().equals("sourceTags")) {
          TermsFacet tagsFacet = (TermsFacet) facet.getValue();
          rp.setSourceMetaTags(tagsFacet.getEntries());
        }
        if (facet.getKey().equals("sourceTypes")) {
          TermsFacet typesFacet = (TermsFacet) facet.getValue();
          rp.setSourceMetaTypes(typesFacet.getEntries());
        }
        if (facet.getKey().equals("sourceKeys")) {
          TermsFacet keysFacet = (TermsFacet) facet.getValue();
          rp.setSources(keysFacet.getEntries());
        }
        // TESTED x3

        // Moments (basic functionality)

        if (facet.getKey().startsWith("moments.")) {
          DateHistogramFacet momentFacet = (DateHistogramFacet) facet.getValue();
          if (null == moments) {
            moments = new HashMap<String, List<? extends Object>>();
          }
          moments.put(facet.getKey().substring(8), momentFacet.getEntries());
        } // TESTED
      } // (end loop over generated facets)

    if ((null != aggs) && (null != aggs.asMap()))
      for (Map.Entry<String, Aggregation> agg : aggs.asMap().entrySet()) {

        if (agg.getKey().equals("moments")) {
          if (null == moments) {
            moments = new HashMap<String, List<? extends Object>>();
          }

          DateHistogram val = (DateHistogram) agg.getValue();

          // TODO (INF-2688): Finalize format
          BasicDBList dbl = new BasicDBList();
          for (DateHistogram.Bucket dateBucket : val.getBuckets()) {
            if (dateBucket.getKeyAsNumber().longValue() > 0) {
              BasicDBObject dataBucketDbo = new BasicDBObject();
              dataBucketDbo.put("time", dateBucket.getKeyAsNumber().longValue());
              dataBucketDbo.put("count", dateBucket.getDocCount());
              for (Map.Entry<String, Aggregation> dateAggs :
                  dateBucket.getAggregations().asMap().entrySet()) {
                if (dateAggs.getKey().equals("geo")) {

                  BasicDBList dbl_geo = new BasicDBList();
                  MultiBucketsAggregation geoVal = (MultiBucketsAggregation) dateAggs.getValue();

                  long nHighestCount = Long.MIN_VALUE;
                  for (MultiBucketsAggregation.Bucket geoBucket : geoVal.getBuckets()) {
                    String geohash = geoBucket.getKey().substring(2);
                    double[] loc = GeoHashUtils.decode(geohash);
                    GeoAggregationPojo geoObj = new GeoAggregationPojo(loc[0], loc[1]);
                    BasicDBObject geoDbo = new BasicDBObject(4);
                    geoDbo.put("lat", geoObj.lat);
                    geoDbo.put("lon", geoObj.lon);
                    geoDbo.put("count", geoBucket.getDocCount());
                    geoDbo.put(
                        "type",
                        GeoOntologyMapping.decodeOntologyCode(geoBucket.getKey().charAt(0)));
                    dbl_geo.add(geoDbo);

                    if (geoBucket.getDocCount()
                        > nHighestCount) { // (the counts can be modified by the add command above)
                      nHighestCount = geoBucket.getDocCount();
                    }
                  }
                  dataBucketDbo.put("maxGeoCount", nHighestCount);
                  dataBucketDbo.put("geo", dbl_geo);
                }
              }
              dbl.add(dataBucketDbo);
            }
          }
          moments.put("times", dbl);
        } else {
          if (null == moments) {
            moments = new HashMap<String, List<? extends Object>>();
          }
          DateHistogram val = (DateHistogram) agg.getValue();
          BasicDBList dbl = new BasicDBList();
          for (DateHistogram.Bucket dateBucket : val.getBuckets()) {
            if (dateBucket.getKeyAsNumber().longValue() > 0) {
              BasicDBObject dataBucketDbo = new BasicDBObject();
              dataBucketDbo.put("time", dateBucket.getKeyAsNumber().longValue());
              dataBucketDbo.put("count", dateBucket.getDocCount());
              for (Map.Entry<String, Aggregation> dateAggs :
                  dateBucket.getAggregations().asMap().entrySet()) {
                if (dateAggs.getKey().equals("moments.assoc.nested")) {

                  BasicDBList dbl_assoc = new BasicDBList();
                  Nested nestedVal = (Nested) dateAggs.getValue();
                  MultiBucketsAggregation assocVal =
                      (MultiBucketsAggregation) nestedVal.getAggregations().asList().get(0);
                  long nHighestCount = Long.MIN_VALUE;

                  for (MultiBucketsAggregation.Bucket assocBucket : assocVal.getBuckets()) {
                    BasicDBObject assocDbo = new BasicDBObject(2);
                    assocDbo.put("key", assocBucket.getKey());
                    assocDbo.put("docCount", assocBucket.getDocCount());
                    dbl_assoc.add(assocDbo);

                    if (assocBucket.getDocCount()
                        > nHighestCount) { // (the counts can be modified by the add command above)
                      nHighestCount = assocBucket.getDocCount();
                    }
                  }

                  dataBucketDbo.put("maxAssocCount", nHighestCount);
                  dataBucketDbo.put("assoc", dbl_assoc);
                }
              }
              dbl.add(dataBucketDbo);
            }
            moments.put("assocs", dbl);
          }
        }
      } // (end loop over generated aggregations)

    if ((null != moments) && !moments.isEmpty()) {
      rp.setMoments(moments, QueryHandler.getInterval(aggOutParams.moments.timesInterval, 'm'));
    }
  } // TESTED
 @Test
 public void aliasTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   Aggregations result =
       query(String.format("SELECT COUNT(*) AS mycount FROM %s/account", TEST_INDEX));
   assertThat(result.asMap(), hasKey("mycount"));
 }