Search code examples
neo4jcyphernodes

How do I create a link between nodes based on a subnode link in Neo4j


I have a database with two clusters. There is a hard link between the nodes near the bottom. But when you select just the top nodes, I would like to see that the root nodes are connected somehere.

Extracted picture - Explicit exists, need to generate "generated"

So the links at the bottom are connected but when you select only the top nodes, I would like to show that at some point (any point) these are connected.

1) I can find the link to between groups by matching a child connection where the target has different groupid (group id is on every node) 2) I think I then have to run up the tree and find the root node. I can't seem to make that work. I can see it, but can't come up with the cypher to do it. It would be a node with no parents. 3) then link (with a different link ID :peerLink)) from group a to group b. Thats easy enough to do once I have 2

I am having a beast putting them together. I did create a separate field for a list of peerLinks so they didnt confuse my parent logic. So again, the goal is to produce the dashed line based on subnode connection.

I DON'T Need to show it at the sublevels. They will appear in the UI as an offpage connector

Anyone done this before ?


Solution

  • Can you please try this?

    MATCH (A_leaf)->(B_leaf)
    WHERE A_leaf.GroupID <> B_leaf.GroupID
    WITH A_leaf
    MATCH (A_leaf)<-[*]-(A_root)
    WHERE NOT (A_root)<-()
    WITH B_leaf
    MATCH (B_leaf)<-[*]-(B_root)
    WHERE B_leaf.GroupID == B_root.GroupID
    AND NOT (B_root)<-()
    CREATE (A_root)-[:relName]->(B_root)