Reputation: 862
I am trying to create a Linked List structure with Neo/Cypher as per the recomendation here: CYPHER store order of node relationships of the same label when I create
(p:Parent)-[r1:PARENTID_RELTYPE]->(c1:Child)-[r2:PARENTID_RELTYPE]->(c2:Child)-[r3:PARENTID_RELTYPE]->(c3:Child)
But I am having trouble understanding the syntax for the required sequence of events in order to unshift new nodes onto the structure or replace a particular index with a different node.
Where I am confused is the part where I need to add the new node and repair the structure that keeps the old node still inside the linked list sturcture. There should be only one relationship of each type (PARENTID_RELTYPE) that stems from a Parent node; But there can be multiple relationships of different type from each parent. Child nodes can be featured multiple times within a LinkedList and a child node could be featured in a LinkedList of multiple Parents or in a LinkedList of the same parent but a different relationship type.
So one of three things could happen when I try to unshift:
There is no existing child that links to the parent by the PARENTID_RELTYPE
There already exists a child node that links to the parent by PARENTID_RELTYPE
There already exists a child node that links to the parent by PARENTID_RELTYPE and that child node is simply a duplicate of the child node I am attempting to unshift onto the linked list structure (in which case the intended result is to have the same child node in the zero and first indices of the linked list).
The answer url mentioned above helps me greatly in understanding how to read a linked list structure in Neo/Cypher but because of the alternative way for which conditionals are handled in cypher, I am having trouble understanding how to write to the structure (and also delete from he structure).
Below is my initial attempt to do so but I am somewhat baffled by what I need the syntax to do.
MATCH (a:%s {id: {_parentnodeid}}), (b:%s {id: {_id}})
MERGE a-[relold:%s]->b
ON CREATE
SET relold.metadata = {_metaData}
ON MATCH
...
I am very grateful for help you can provide.
Upvotes: 0
Views: 232
Reputation: 66999
[UPDATED]
In the following queries, for simplicity I pretend that:
Parent
node of interest by name.Foo
.General Notes:
OPTIONAL MATCH
clauses find the sibling, if any, that should follow the child being inserted.FOREACH
clauses take care of linking the that sibling, if any, to the child being inserted, and then deletes the obsolete relationship to that sibling.To unshift the Child
having an id
of 123
right after the Parent
node:
MATCH (p:Parent {name:"Fred"})
OPTIONAL MATCH (p)-[r:Foo]->(c:Child)
WITH p, r, COLLECT(c) AS cs
MERGE (cNew:Child {id:123})
CREATE (p)-[rNew:Foo]->(cNew)
FOREACH (x IN cs |
CREATE (cNew)-[:Foo]->(x)
DELETE r)
RETURN p, rNew, cNew;
To insert the Child
node having an id
of 123
at index 4 (i.e, make it the 5th child):
MATCH (p:Parent {name:"Fred"})
MATCH (p)-[:Foo*3]->()-[r:Foo]->(c:Child)
OPTIONAL MATCH (c)-[r1:Foo]->(c1:Child)
WITH c, r1, COLLECT(c1) AS c1s
MERGE (cNew:Child {id:123})
CREATE (c)-[rNew:Foo]->(cNew)
FOREACH (x IN c1s |
CREATE (cNew)-[:Foo]->(x)
DELETE r1)
RETURN c, rNew, cNew;
To replace the Child
at index 4 (i.e, the 5th child) with the Child
having an id
of 123
:
MATCH (p:Parent { name:"Fred" })
MATCH (p)-[:Foo*4]->(c0)-[r:Foo]->(c:Child)
OPTIONAL MATCH (c)-[r1:Foo]->(c1:Child)
WITH c0, r, c, r1, COLLECT(c1) AS c1s
MERGE (cNew:Child { id:123 })
CREATE (c0)-[rNew:Foo]->(cNew)
DELETE r, c
FOREACH (x IN c1s |
CREATE (cNew)-[:Foo]->(x)
DELETE r1)
RETURN c0, rNew, cNew;
Note: The DELETE r, c
clause always deletes the node being replaced (c
). That is only suitable if that is what you actually want to happen, and will only succeed if c
does not have relationships other than r
. To explore how to address more specific needs, please ask a new question.
Upvotes: 2
Reputation: 4407
If I'm following, your nodes may belong to multiple linked lists. A simple 'next' relation is insufficient because when lists cross--share a child node--the 'next' relations will drag in all the downstream nodes of both lists. So you're making the 'next' relations unique to each list by adding the id of the parent node. (Note using the metadata id may lead to issues down the road.)
So you might have a parent p1 whose id=1, and a unique relationship 'n_p1' to link its children, and a child 'c' whose id=21 you want to add.
For the parent with no child you could add your new child by:
MATCH (c {id:21}), (p {id:1}) WHERE NOT p-[:n_p1]->() MERGE p-[:n_p1]->c
And if the parent has one or more children, find the last one that's not the same as the one being added:
MATCH (c {id:21}), (p {id:1})-[:n_p1*1..5]->(cn) WHERE NOT cn-[:n_p1]->() AND NOT cn.id=c.id MERGE cn-[:n_p1]->c
Somebody else might have a better way, but you could UNION these together. Remember the parts of a UNION must return the same columns, so just return the new child c. The whole thing might look like this:
MATCH (c {id:21}), (p {id:1}) WHERE NOT p-[:n_p1]->() MERGE p-[:n_p1]->c return c UNION MATCH (c {id:21}), (p {id:1})-[:n_p1*1..5]->(cn) WHERE NOT cn-[:n_p1]->() AND NOT cn.id=c.id MERGE cn-[:n_p1]->c return c;
Upvotes: 1