Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-16-2020 06:37 AM
I have a directed multigraph. Using the following query, I managed to get the set of nodes that is connected with the node n1_34
MATCH (n1:Node{uuid: "n1_34"}) -[r]- (n2:Node) RETURN n2, r
This yields the nodes n1_1187, n2_2280, n2_1834 and n2_722 and their relationship as seen in the figure.
From here I need to sort the result based on the number their relationships within this subgraph. That means, n1_1187
should be on top with 4 relationships while the others comes after that with 1 relationship.
I have tried and reapplied the solution provided by related question: (Stackoverflow) Extract subgraph from Neo4j graph with Cypher and Count relationship on each nodes. None of these works. The former gives the same result as my original query and the latter gives all the relationships that each node has within the entire graph.
How can I get the relationships within the subgraph?
Solved! Go to Solution.
08-16-2020 08:14 AM
Sometimes you just need someone to question your logic to help you. I have been thinking about the solution for days. Thank you Cobra.
Here is the query I need (in case anyone needs it):
MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node)
MATCH (n1) -- () -[r]- (n2) RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC
I will surely take a look at the article if I need to project a subgraph. Thanks again.
08-16-2020 06:40 AM
Hello @chandrautama.lucky and welcome to the Neo4j community
MATCH (n1:Node{uuid: "n1_34"})-[r]-(n2:Node)
RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC
Regards,
Cobra
08-16-2020 07:02 AM
Thank you, Cobra but this is not what I need. count(r)
gives the number of relationship r between n1 (node with uuid "n1_34") and the n2 nodes.
Maybe I am not very clear in the question. Here is the subgraph with n1, n2 and r as the results:
Seeing the figure, you can see why count(r) isn't the one I am looking for and will give the number relationship between n1 and each n2 nodes. Hence, 1 for each n2 nodes.
Maybe you have an alternative?
08-16-2020 07:03 AM
Can you write the result you would like to get based on your last screenshot?
08-16-2020 07:12 AM
OK. This my question: Given the node "n1_34" find
The neighbouring nodes and
The relationship it has among themselves excluding the relationship with "n1_34"
The result I expected
uuid | n
---------------
n1_1187 | 4
n2_2280 | 1
n2_1834 | 1
n2_722 | 1
n2_932 | 1
With the query MATCH (n1:Node{uuid: "n1_34"}) -[r]- (n2:Node) RETURN n2, r
, I can get the neighbours. But then somehow I need to traverse each node in context of the subgraph. I dont know how.
08-16-2020 07:16 AM
I'm sorry, but the query I gave you should return this result.
But then somehow I need to traverse each node in context of the subgraph.
What do you mean?
08-16-2020 07:39 AM
I'm sorry, but the query I gave you should return this result.
I tried it. But your query gives me
uuid | n
---------------
n2_722 | 1
n2_1834 | 1
n2_932 | 1
n2_2280 | 1
n1_1187 | 1
I just think I need to traverse the n2 since I need their relationships not the n1's relationship. I once thought this following query will work: MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node) -[r]- () RETURN n2.uuid AS uuid, count(r) AS n ORDER BY n DESC
. But this gives me the number of the relationship of the n2 nodes in an entire graph like this:
uuid | n
---------------
n2_1834 |1672|
n2_2280 | 170|
n2_722 | 68|
n2_932 | 54|
n1_1187 | 4|
Hence I said I need the traversal in context of the subgraph.
08-16-2020 07:46 AM
Oh I get it , you should have a look at the documentation here, it will should you how to make projections
08-16-2020 08:14 AM
Sometimes you just need someone to question your logic to help you. I have been thinking about the solution for days. Thank you Cobra.
Here is the query I need (in case anyone needs it):
MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node)
MATCH (n1) -- () -[r]- (n2) RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC
I will surely take a look at the article if I need to project a subgraph. Thanks again.
08-16-2020 08:17 AM
No problem
You could optimise a bit I think:
MATCH (n1:Node{uuid: "n1_34"})-()-[r]-(n2:Node)
RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC
All the sessions of the conference are now available online