private void applyAggregationsToQuery(QueryAndData queryAndData, SearchOptions searchOptions) { Query query = queryAndData.getQuery(); String[] aggregates = searchOptions.getOptionalParameter("aggregations[]", String[].class); if (aggregates == null) { return; } for (String aggregate : aggregates) { JSONObject aggregateJson = new JSONObject(aggregate); Aggregation aggregation = getAggregation(aggregateJson); query.addAggregation(aggregation); } }
private StatisticsResult queryGraphQueryWithStatisticsAggregation(String propertyName, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); StatisticsAggregation agg = new StatisticsAggregation("stats", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", StatisticsAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("stats", StatisticsResult.class); }
private CardinalityResult queryGraphQueryWithCardinalityAggregation(String propertyName, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); CardinalityAggregation agg = new CardinalityAggregation("card", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", CardinalityAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("card", CardinalityResult.class); }
private CardinalityResult queryGraphQueryWithCardinalityAggregation(String propertyName, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); CardinalityAggregation agg = new CardinalityAggregation("card", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", CardinalityAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("card", CardinalityResult.class); }
private StatisticsResult queryGraphQueryWithStatisticsAggregation(String propertyName, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); StatisticsAggregation agg = new StatisticsAggregation("stats", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", StatisticsAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("stats", StatisticsResult.class); }
private Map<Object, Long> queryGraphQueryWithTermsAggregation(String queryString, String propertyName, ElementType elementType, Authorizations authorizations) { Query q = (queryString == null ? graph.query(authorizations) : graph.query(queryString, authorizations)).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", agg.getClass().getName()); return null; } q.addAggregation(agg); QueryResultsIterable<? extends Element> elements = elementType == ElementType.VERTEX ? q.vertices() : q.edges(); TermsResult aggregationResult = elements.getAggregationResult("terms-count", TermsResult.class); return termsBucketToMap(aggregationResult.getBuckets()); }
private Map<Object, Long> queryGraphQueryWithTermsAggregation(String queryString, String propertyName, ElementType elementType, Authorizations authorizations) { Query q = (queryString == null ? graph.query(authorizations) : graph.query(queryString, authorizations)).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", agg.getClass().getName()); return null; } q.addAggregation(agg); QueryResultsIterable<? extends Element> elements = elementType == ElementType.VERTEX ? q.vertices() : q.edges(); TermsResult aggregationResult = elements.getAggregationResult("terms-count", TermsResult.class); return termsBucketToMap(aggregationResult.getBuckets()); }
public JSONObject getAverages(int minutes, Graph graph, Authorizations authorizations) { Date minutesAgo = new Date(System.currentTimeMillis() - minutes * 60 * 1000); Query q = graph.query(authorizations) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), PingOntology.IRI_CONCEPT_PING) .has(PingOntology.CREATE_DATE.getPropertyName(), Compare.GREATER_THAN, minutesAgo) .limit(0); q.addAggregation(new StatisticsAggregation(PingOntology.SEARCH_TIME_MS.getPropertyName(), PingOntology.SEARCH_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.RETRIEVAL_TIME_MS.getPropertyName(), PingOntology.RETRIEVAL_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName(), PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName(), PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName())); QueryResultsIterable<Vertex> vertices = q.vertices(); StatisticsResult searchTimeAgg = vertices.getAggregationResult(PingOntology.SEARCH_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult retrievalTimeAgg = vertices.getAggregationResult(PingOntology.RETRIEVAL_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult gpwWaitTimeAgg = vertices.getAggregationResult(PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult lrpWaitTimeAgg = vertices.getAggregationResult(PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName(), StatisticsResult.class); JSONObject json = new JSONObject(); json.put("pingCount", searchTimeAgg.getCount()); json.put("averageSearchTime", searchTimeAgg.getAverage()); json.put("averageRetrievalTime", retrievalTimeAgg.getAverage()); json.put("graphPropertyWorkerCount", gpwWaitTimeAgg.getCount()); json.put("averageGraphPropertyWorkerWaitTime", gpwWaitTimeAgg.getAverage()); json.put("longRunningProcessCount", lrpWaitTimeAgg.getCount()); json.put("averageLongRunningProcessWaitTime", lrpWaitTimeAgg.getAverage()); return json; }
private Map<String, Long> queryGraphQueryWithGeohashAggregation(String propertyName, int precision, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); GeohashAggregation agg = new GeohashAggregation("geo-count", propertyName, precision); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", GeohashAggregation.class.getName()); return null; } q.addAggregation(agg); return geoHashBucketToMap(q.vertices().getAggregationResult("geo-count", GeohashResult.class).getBuckets()); }
private Map<String, Long> queryGraphQueryWithGeohashAggregation(String propertyName, int precision, Authorizations authorizations) { Query q = graph.query(authorizations).limit(0); GeohashAggregation agg = new GeohashAggregation("geo-count", propertyName, precision); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", GeohashAggregation.class.getName()); return null; } q.addAggregation(agg); return geoHashBucketToMap(q.vertices().getAggregationResult("geo-count", GeohashResult.class).getBuckets()); }
private PercentilesResult queryGraphQueryWithPercentilesAggregation( String propertyName, Visibility visibility, Authorizations authorizations, double... percents ) { Query q = graph.query(authorizations).limit(0); PercentilesAggregation agg = new PercentilesAggregation("percentiles", propertyName, visibility); agg.setPercents(percents); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", StatisticsAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("percentiles", PercentilesResult.class); }
private PercentilesResult queryGraphQueryWithPercentilesAggregation( String propertyName, Visibility visibility, Authorizations authorizations, double... percents ) { Query q = graph.query(authorizations).limit(0); PercentilesAggregation agg = new PercentilesAggregation("percentiles", propertyName, visibility); agg.setPercents(percents); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", StatisticsAggregation.class.getName()); return null; } q.addAggregation(agg); return q.vertices().getAggregationResult("percentiles", PercentilesResult.class); }
private Map<Object, Long> queryGraphQueryWithHistogramAggregation( String propertyName, String interval, Long minDocCount, HistogramAggregation.ExtendedBounds extendedBounds, Authorizations authorizations ) { Query q = graph.query(authorizations).limit(0); HistogramAggregation agg = new HistogramAggregation("hist-count", propertyName, interval, minDocCount); agg.setExtendedBounds(extendedBounds); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", HistogramAggregation.class.getName()); return null; } q.addAggregation(agg); return histogramBucketToMap(q.vertices().getAggregationResult("hist-count", HistogramResult.class).getBuckets()); }
private Map<Object, Long> queryGraphQueryWithHistogramAggregation( String propertyName, String interval, Long minDocCount, HistogramAggregation.ExtendedBounds extendedBounds, Authorizations authorizations ) { Query q = graph.query(authorizations).limit(0); HistogramAggregation agg = new HistogramAggregation("hist-count", propertyName, interval, minDocCount); agg.setExtendedBounds(extendedBounds); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", HistogramAggregation.class.getName()); return null; } q.addAggregation(agg); return histogramBucketToMap(q.vertices().getAggregationResult("hist-count", HistogramResult.class).getBuckets()); }
private RangeResult queryGraphQueryWithRangeAggregation( String propertyName, String format, String keyOne, Object boundaryOne, String keyTwo, Object boundaryTwo, String keyThree, Authorizations authorizations ) { Query q = graph.query(authorizations).limit(0); RangeAggregation agg = new RangeAggregation("range-count", propertyName, format); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", RangeAggregation.class.getName()); return null; } agg.addUnboundedTo(keyOne, boundaryOne); agg.addRange(keyTwo, boundaryOne, boundaryTwo); agg.addUnboundedFrom(keyThree, boundaryTwo); q.addAggregation(agg); return q.vertices().getAggregationResult("range-count", RangeResult.class); }
private RangeResult queryGraphQueryWithRangeAggregation( String propertyName, String format, String keyOne, Object boundaryOne, String keyTwo, Object boundaryTwo, String keyThree, Authorizations authorizations ) { Query q = graph.query(authorizations).limit(0); RangeAggregation agg = new RangeAggregation("range-count", propertyName, format); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", RangeAggregation.class.getName()); return null; } agg.addUnboundedTo(keyOne, boundaryOne); agg.addRange(keyTwo, boundaryOne, boundaryTwo); agg.addUnboundedFrom(keyThree, boundaryTwo); q.addAggregation(agg); return q.vertices().getAggregationResult("range-count", RangeResult.class); }
@Test public void testVertexQueryWithNestedTermsAggregationOnExtendedData() { graph.defineProperty("name").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH, TextIndexHint.FULL_TEXT).define(); graph.defineProperty("gender").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "name", "Joe", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "gender", "female", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "gender", "female", VISIBILITY_EMPTY) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); Query q = v1.getExtendedData("t1").query(AUTHORIZATIONS_A_AND_B).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.extendedDataRows().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested"); assertEquals(2, vertexPropertyCountByValue.size()); assertEquals(1, vertexPropertyCountByValue.get("Joe").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Joe").get("male")); assertEquals(2, vertexPropertyCountByValue.get("Sam").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Sam").get("male")); assertEquals(2L, (long) vertexPropertyCountByValue.get("Sam").get("female")); }
@Test public void testCaseSensitivityOfExactMatch() { graph.defineProperty("text").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("text", "Joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("text", "joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v3", VISIBILITY_A) .setProperty("text", "JOE", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v4", VISIBILITY_A) .setProperty("text", "Joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.flush(); QueryResultsIterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A) .has("text", Compare.EQUAL, "Joe") .addAggregation(new TermsAggregation("agg1", "text")) .vertices(); assertVertexIdsAnyOrder(vertices, "v1", "v2", "v3", "v4"); TermsResult agg = vertices.getAggregationResult("agg1", TermsResult.class); ArrayList<TermsBucket> buckets = Lists.newArrayList(agg.getBuckets()); assertEquals(1, buckets.size()); assertEquals("Joe", buckets.get(0).getKey()); assertEquals(4L, buckets.get(0).getCount()); }
@Test public void testGraphQueryWithTermsAggregationAndPredicates() { graph.defineProperty("name").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH, TextIndexHint.FULL_TEXT).define(); graph.defineProperty("gender").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); Query q = graph.query(AUTHORIZATIONS_EMPTY) .has("name", Compare.EQUAL, "Susan"); TermsAggregation agg = new TermsAggregation("terms-count", "name"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Long> vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(0, vertexPropertyCountByValue.size()); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addPropertyValue("k1", "gender", "female", VISIBILITY_A) .save(AUTHORIZATIONS_A); getGraph().flush(); q = graph.query(AUTHORIZATIONS_A) .has("gender", Compare.EQUAL, "female"); agg = new TermsAggregation("terms-count", "gender"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(1, vertexPropertyCountByValue.size()); }
@Test public void testGraphQueryWithTermsAggregationAndPredicates() { graph.defineProperty("name").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH, TextIndexHint.FULL_TEXT).define(); graph.defineProperty("gender").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); Query q = graph.query(AUTHORIZATIONS_EMPTY) .has("name", Compare.EQUAL, "Susan"); TermsAggregation agg = new TermsAggregation("terms-count", "name"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Long> vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(0, vertexPropertyCountByValue.size()); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addPropertyValue("k1", "gender", "female", VISIBILITY_A) .save(AUTHORIZATIONS_A); getGraph().flush(); q = graph.query(AUTHORIZATIONS_A) .has("gender", Compare.EQUAL, "female"); agg = new TermsAggregation("terms-count", "gender"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(1, vertexPropertyCountByValue.size()); }