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
示例#2
0
  @Get
  public Representation get() {
    ResponsePojo rp = new ResponsePojo();
    Date startTime = new Date();
    cookieLookup = RESTTools.cookieLookup(cookie);

    // If JSON is != null, check that it is valid JSON
    boolean isValidJson = true;
    if (json != null) {
      try {
        JSON.parse(json);
      } catch (Exception e) {
        rp.setResponse(
            new ResponseObject(
                "Parsing JSON",
                false,
                "The value passed via the json parameter could not be" + " parsed as valid JSON."));
        isValidJson = false;
      }
    }

    if (isValidJson) {
      if (cookieLookup == null) {
        // User is not logged in
        rp.setResponse(
            new ResponseObject(
                "Cookie Lookup",
                false,
                "Cookie session expired or never existed, please login first"));
      } else {
        // UserId which will serve as the OwnerId for transactions below that require it
        personId = cookieLookup;

        if (action.equals("saveJson")) {
          rp = this.shareController.saveJson(personId, id, type, title, description, json);
        } else if (action.equals("addBinaryGET")) {
          rp =
              new ResponsePojo(
                  new ResponseObject(
                      "addBinary", false, "Can only add binary in POST (do not use GET)"));
        } else if (action.equals("addBinaryPOST")) {
          rp =
              this.shareController.addBinary(
                  personId,
                  "binary",
                  title,
                  description,
                  this.getRequest().getEntity().getMediaType().toString(),
                  binaryData);
        } else if (action.equals("updateBinaryGET")) {
          rp =
              new ResponsePojo(
                  new ResponseObject(
                      "updateBinary", false, "Can only update binary in POST (do not use GET)"));
        } else if (action.equals("updateBinaryPOST")) {
          rp =
              this.shareController.updateBinary(
                  personId,
                  id,
                  "binary",
                  title,
                  description,
                  this.getRequest().getEntity().getMediaType().toString(),
                  binaryData);
        } else if (action.equals("addRef")) {
          // Not currently supported
          // rp = this.shareController.addRef(personId, type, documentId, title, description);
          rp.setResponse(new ResponseObject("Cookie Lookup", false, "Not currently supported"));
        } else if (action.equals("updateRef")) {
          // Not currently supported
          // rp = this.shareController.updateRef(personId, id, type, documentId, title,
          // description);
          rp.setResponse(new ResponseObject("Cookie Lookup", false, "Not currently supported"));
        } else if (action.equals("removeShare")) {
          rp = this.shareController.removeShare(personId, shareId);
        } else if (action.equals("endorseShare")) {
          rp = this.shareController.endorseShare(personId, communityId, shareId, isEndorsed);
        } else if (action.equals("addCommunity")) {
          rp = this.shareController.addCommunity(personId, shareId, communityId, comment);
        } else if (action.equals("removeCommunity")) {
          rp = this.shareController.removeCommunity(personId, shareId, communityId);
        } else if (action.equals("getShare")) {
          rp = this.shareController.getShare(personId, shareId, returnContent);
          SharePojo share = (SharePojo) rp.getData();
          if (null != share) {
            boolean bBinary = share.getType().equals("binary");
            if (bBinary && returnContent) {
              try {
                ByteArrayOutputRepresentation rep =
                    new ByteArrayOutputRepresentation(MediaType.valueOf(share.getMediaType()));
                rep.setOutputBytes(share.getBinaryData());
                return rep;
              } catch (Exception ex) {
                rp =
                    new ResponsePojo(
                        new ResponseObject("get Share", false, "error converting bytes to output"));
              }
            } else if (!bBinary && jsonOnly) {
              try {
                BasicDBObject dbo = (BasicDBObject) com.mongodb.util.JSON.parse(share.getShare());
                rp.setData(dbo, null);
              } catch (Exception e) { // Try a list instead
                BasicDBList dbo = (BasicDBList) com.mongodb.util.JSON.parse(share.getShare());
                rp.setData(dbo, (BasePojoApiMap<BasicDBList>) null);
              }
            }
          }
          // (else error)
        } else if (action.equals("searchShares")) {
          rp =
              this.shareController.searchShares(
                  personId, searchby, id, type, skip, limit, ignoreAdmin, returnContent);
        }
      }
    }

    Date endTime = new Date();
    rp.getResponse().setTime(endTime.getTime() - startTime.getTime());
    return new StringRepresentation(rp.toApi(), MediaType.APPLICATION_JSON);
  }