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 static QueryResultsIterable<? extends Element> executeQuery( VertexiumCypherQueryContext ctx, Query query, MatchConstraint<?, ?> matchConstraint ) { QueryResultsIterable<? extends Element> elements; if (matchConstraint instanceof NodeMatchConstraint) { elements = query.vertices(ctx.getFetchHints()); } else if (matchConstraint instanceof RelationshipMatchConstraint) { elements = query.edges(ctx.getFetchHints()); } else { throw new VertexiumCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName()); } return elements; }
@Override public User findByPasswordResetToken(String token) { QueryResultsIterable<Vertex> userVertices = graph.query(authorizations) .has(UserVisalloProperties.PASSWORD_RESET_TOKEN.getPropertyName(), token) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .vertices(); Vertex user = singleOrDefault(userVertices, null); return createFromVertex(user); }
@Override public Iterable<User> find(int skip, int limit) { QueryResultsIterable<Vertex> userVertices = graph.query(authorizations) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .skip(skip) .limit(limit) .vertices(); return new ConvertingIterable<Vertex, User>(userVertices) { @Override protected User convert(Vertex vertex) { return createFromVertex(vertex); } }; }
@Override public Iterable<User> findByStatus(int skip, int limit, UserStatus status) { QueryResultsIterable<Vertex> userVertices = graph.query(authorizations) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .has(UserVisalloProperties.STATUS.getPropertyName(), status.toString()) .skip(skip) .limit(limit) .vertices(); return new ConvertingIterable<Vertex, User>(userVertices) { @Override protected User convert(Vertex vertex) { return createFromVertex(vertex); } }; }
QueryResultsIterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).vertices(FetchHints.ALL); assertResultsCount(0, vertices); QueryResultsIterable<String> vertexIds = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).vertexIds(IdFetchHint.NONE); assertResultsCount(0, 0, vertexIds); vertexIds = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).vertexIds(); assertResultsCount(0, 0, vertexIds); vertices = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).vertices(FetchHints.ALL_INCLUDING_HIDDEN); assertResultsCount(1, vertices); assertVertexIdsAnyOrder(vertices, v1.getId()); vertices = graph.query(AUTHORIZATIONS_B_AND_C).hasAuthorization(VISIBILITY_C_STRING).vertices(FetchHints.ALL_INCLUDING_HIDDEN); assertResultsCount(1, vertices); assertVertexIdsAnyOrder(vertices, v3.getId()); vertexIds = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).vertexIds(IdFetchHint.ALL_INCLUDING_HIDDEN); assertResultsCount(1, 1, vertexIds); assertIdsAnyOrder(vertexIds, v1.getId()); vertexIds = graph.query(AUTHORIZATIONS_B_AND_C).hasAuthorization(VISIBILITY_C_STRING).vertexIds(IdFetchHint.ALL_INCLUDING_HIDDEN); assertResultsCount(1, 1, vertexIds); assertIdsAnyOrder(vertexIds, v3.getId()); QueryResultsIterable<Edge> edges = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).edges(FetchHints.ALL); assertResultsCount(0, edges); QueryResultsIterable<String> edgeIds = graph.query(AUTHORIZATIONS_A).hasAuthorization(VISIBILITY_A_STRING).edgeIds(IdFetchHint.NONE);
vertices = graph.query(AUTHORIZATIONS_A).skip(1).vertices(); assertResultsCount(1, 2, vertices); vertices = graph.query(AUTHORIZATIONS_A).limit(1).vertices(); assertResultsCount(1, 2, vertices); vertices = graph.query(AUTHORIZATIONS_A).skip(1).limit(1).vertices(); assertResultsCount(1, 2, vertices); vertices = graph.query(AUTHORIZATIONS_A).skip(2).vertices(); assertResultsCount(0, 2, vertices); vertices = graph.query(AUTHORIZATIONS_A).skip(1).limit(2).vertices(); assertResultsCount(1, 2, vertices); edges = graph.query(AUTHORIZATIONS_A).hasEdgeLabel(LABEL_LABEL1).edges(); assertResultsCount(1, 1, edges); edges = graph.query(AUTHORIZATIONS_A).hasEdgeLabel(LABEL_LABEL1, LABEL_LABEL2).edges(); assertResultsCount(2, 2, edges); vertices = graph.query(AUTHORIZATIONS_A).has(namePropertyName).vertices(); assertResultsCount(1, 1, vertices); vertices = graph.query(AUTHORIZATIONS_A).hasNot(namePropertyName).vertices(); assertResultsCount(1, 1, vertices); vertices = graph.query(AUTHORIZATIONS_A).has("notSetProp").vertices(); assertResultsCount(0, 0, vertices);
public String search(Graph graph, Authorizations authorizations) { Query query = graph.query(authorizations).limit(1); List<Vertex> vertices = Lists.newArrayList(query.vertices()); if (vertices.size() == 0) { throw new VisalloException("query returned no vertices"); } else if (vertices.size() > 1) { throw new VisalloException("query returned more than one vertex"); } return vertices.get(0).getId(); }
.skip(0) .limit(1) .vertexIds(); assertIdsAnyOrder(idsIterable, "v1"); assertResultsCount(1, 3, idsIterable); .skip(1) .limit(1) .vertexIds(); assertIdsAnyOrder(idsIterable, "v2"); .skip(2) .limit(1) .vertexIds(); assertIdsAnyOrder(idsIterable, "v3"); idsIterable = graph.query(AUTHORIZATIONS_A).sort(namePropertyName, SortDirection.ASCENDING).vertexIds(); assertResultsCount(3, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).limit((Long) null).vertexIds(); assertResultsCount(3, 3, idsIterable); .skip(0) .limit(1) .vertices()); assertEquals(1, vertices.size()); assertEquals("v2", vertices.get(0).getId());
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<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()); }
@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()); }
@Override public Iterable<Edge> getEdges(final String key, final Object value) { final Authorizations authorizations = getAuthorizationsProvider().getAuthorizations(); return new ConvertingIterable<org.vertexium.Edge, Edge>(getGraph().query(authorizations).has(key, Compare.EQUAL, value).edges(getFetchHints())) { @Override protected Edge convert(org.vertexium.Edge edge) { return VertexiumBlueprintsEdge.create(VertexiumBlueprintsGraph.this, edge, authorizations); } }; }
@Test public void testGraphQueryForEdgesUsingInOutVertexIds() { Vertex v1 = graph.addVertex("v1", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v2 = graph.addVertex("v2", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v3 = graph.addVertex("v3", VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e1", v1, v2, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e2", v1, v3, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e3", v3, v1, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.flush(); QueryResultsIterable<Edge> edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .has(Edge.IN_VERTEX_ID_PROPERTY_NAME, "v2") .edges(); assertEdgeIdsAnyOrder(edges, "e1"); edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .edges(); assertEdgeIdsAnyOrder(edges, "e1", "e2"); }
idsIterable = graph.query(AUTHORIZATIONS_A).skip(1).vertexIds(); assertResultsCount(2, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).limit(1).vertexIds(); assertResultsCount(1, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).skip(1).limit(1).vertexIds(); assertResultsCount(1, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).skip(3).vertexIds(); assertResultsCount(0, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).skip(2).limit(2).vertexIds(); assertResultsCount(1, 3, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).hasEdgeLabel(LABEL_LABEL1).edgeIds(); assertIdsAnyOrder(idsIterable, "e1"); assertResultsCount(1, 1, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).hasEdgeLabel(LABEL_LABEL1, LABEL_LABEL2).edgeIds(); assertResultsCount(2, 2, idsIterable); idsIterable = graph.query(AUTHORIZATIONS_A).has(namePropertyName).vertexIds(); assertIdsAnyOrder(idsIterable, "v1"); assertResultsCount(1, 1, idsIterable); QueryResultsIterable<ExtendedDataRowId> extendedDataRowIds = graph.query(AUTHORIZATIONS_A).hasExtendedData("table1").extendedDataRowIds(); List<String> rowIds = stream(extendedDataRowIds).map(ExtendedDataRowId::getRowId).collect(Collectors.toList()); assertIdsAnyOrder(rowIds, "row1", "row2");
.reduce( query, (q, labelName) -> q.has(ctx.getLabelPropertyName(), labelName), (q, q2) -> q ); } else if (matchConstraint instanceof RelationshipMatchConstraint) { List<String> normalizedLabelNames = labelNamesStream.collect(Collectors.toList()); query = query.hasEdgeLabel(normalizedLabelNames); } else { throw new VertexiumCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName()); query.has(propertyMatch.getKey(), Contains.IN, value); } else { query.has(propertyMatch.getKey(), value);
private void applyDirectoryEntityJsonObjectEqualityToQuery(Query graphQuery, String propertyName, JSONObject value0, User user) { String directoryEntityId = value0.optString("directoryEntityId", null); if (directoryEntityId != null) { graphQuery.has(propertyName, Compare.EQUAL, directoryEntityId); } else if (value0.optBoolean("currentUser", false)) { directoryEntityId = directoryRepository.getDirectoryEntityId(user); graphQuery.has(propertyName, Compare.EQUAL, directoryEntityId); } else { throw new VisalloException("Invalid directory entity JSONObject filter:\n" + value0.toString(2)); } }