Is there a way with py2neo to get all the connected nodes in a graph. All nodes that are connected by relationship?
You can execute a Cypher query that will return you those :
res = graph.cypher.execute("MATCH (n) WHERE size((n)--()) > 0 RETURN n");
for r in res:
print r
This Cypher query should be good enough:
MATCH (n)--() RETURN DISTINCT n
Related
I want to find nodes that do not connect with other nodes. (node A and node B in the picture below)
What I have tried is
MATCH (n:node) WHERE not ((n)<-[:connect]->(:node)) RETURN n
which seems to return only B.
How can I retrieve both A and B?
Thank you in advance!
[UPDATED]
This will work if you only care about connect relationships:
MATCH (n:node)
WHERE SIZE([(n)-[:connect]-(m:node) WHERE n <> m|1]) = 0
RETURN n
But if you want to pay attention to all relationship types, then use this:
MATCH (n:node)
WHERE SIZE([(n)--(m:node) WHERE n <> m|1]) = 0
RETURN n
In this Neo4j query, I want to return only relation and that only specific relation called "Permission". So how can I filter it and get the expected answer?
MATCH (end:Directory) WHERE end.name = "dir1.5"
WITH shortestPath((start)-[*]->(end)) AS p
RETURN p
ORDER BY length(p)
Once you have a path, you can filter on specific types of relationships doing this
WITH [r IN relationships(path) WHERE r.type = 'Permission'] AS rs
UNWIND rs AS r
RETURN r
If you want to visualize the r in the Neo4j broser, you may need to add the start and endnodes, so it becomes
RETURN startNode(r),r,endNode(r)
How can I know how many nodes and edges are involved in a MATCH? Is there another way besides Explain / Profile Match?
If you mean how many nodes are matched in a path, such as a variable-length path, then you can assign a path variable for this:
MATCH p = (k:Person {name:'Keanu Reeves'})-[*..8]-(t:Person {name:'Tom Hanks'})
WITH p LIMIT 1
RETURN p, length(p) as pathLength, length(p) + 1 as numberOfNodesInPath
You can also use nodes(p) and relationships(p) to get the collection of nodes and relationships that make up the path, and you can use size() on those collections to get their size.
There exists the COUNT() function of Cypher that allows you to count the number of elements. As for example in this query:
MATCH (n)
RETURN COUNT(n);
This query will count all nodes in your database.
You can find more information in the cypher manual, under the aggregating functions. Check it out.
The following Cypher snippet should return the number of distinct nodes and relationships found by any given MATCH clause. Just replace <your code here> with your MATCH pattern.
MATCH <your code here>
WITH COLLECT(NODES(p)) AS ns, SUM(SIZE(RELATIONSHIPS(p))) AS relCount
UNWIND ns AS nodeList
UNWIND nodeList AS node
RETURN COUNT(DISTINCT node) AS nodeCount, relCount;
How to build a Neo4J query that:
1) Will return all nodes in a sub-graph of arbitrary depth with nodes connected by a given set of relations?
For example in Cypher-like syntax:
MATCH (*)-[r1:FRIEND_OF AND r2:COLLEAGUE_WITH]->(*) RETURN *
This query will return just the nodes, as you stated in your question:
MATCH (n)-[:FRIEND_OF|COLLEAGUE_WITH*]->(m)
RETURN n, m;
If you also want the relationships:
MATCH (n)-[r:FRIEND_OF|COLLEAGUE_WITH*]->(m)
RETURN n, r, m;
I have a hierarchical org structure like this:
OrgNode(3)-[HAS_PARENT]->OrgNode(2)-[HAS_PARENT]->OrgNode(1)
I want a cypher query that gives me the top org given any of the node ids:
topOrg(3) = OrgNode(1)
topOrg(2) = OrgNode(1)
topOrg(1) = OrgNode(1)
I'm able to write the query to return the top org when the starting node has at least one parent. But I cannot figure out how to return starting node when there is no parent link in the same query:
start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m
You might use the UNION operator to combine your result with the result of another query that handles a starting node without a parent,
start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m as result
UNION
Start n=node(3)
Match n
Where not(n-[:PARENT]-())
Return n as result