简体   繁体   中英

Neo4j query to ignore parent nodes which doesn't satisfy a condition but keep the same structure

I have a tree-like structure and I'm trying to get a Cypher query which will replace the parent node with the child if the parent node does not have a certain relation

for example the query: MATCH (c)-[:CHILD_OF*]->(p {id:"123"}) return c returns a structure like so (we don't care about what the other nodes are, the structure is the only thing that needs to be preserved)

()<-(A)

()<-()<-(B)<-()<-(C)

()<-(D)<-(E)<-()<-(F)
  \-(G)<-()<-H)

How could I get the query to ignore all nodes without a certain property but keep it the same structure like so:

(A)

(B)<-(C)

(D)<-(E)<-(F)

(G)<-(H)

You should take a look at the procedures for creating virtual nodes and relationships in APOC Procedures.

These will allow you to create virtual relationships, that will not be saved to the graph, but will be present and viewable in your query.

The tricky part will be creating those new virtual relationships. You'll likely be filtering down nodes in all paths to the nodes you're interested in. At that point you may need to use apoc.coll.pairsMin() in order to get each adjacent pair of nodes in the collection on a row so you can create the virtual relationships between them.

After all the virtual relationships are created (in the same cypher query), match from the root node using those virtual relationships, and you should see the graph you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM