[英]neo4j cypher query to delete a middle node and connect all its parent node to child node
If I have a graph by executing this query, then i want to delete a middle node say 'and' and connect its previous node say 'graph' to its child node say 'db' by using its corresponding outgoing relationships based on same 'seqid' 如果我通过执行此查询得到一个图,则我想删除一个中间节点说“和”,并通过基于相同的“ seqid”使用其对应的传出关系,将其上一个节点说“ graph”连接到其子节点说“ db” '
MERGE (n:Person { name: 'graph'})
MERGE (n:Person { name: 'and'})
MERGE (n:Person { name: 'relational' })
MERGE (n:Person { name: 'nosql'})
MERGE (n:Person { name: 'server'})
MERGE (n:Person { name: 'db'})
MERGE (a:Person { name: 'graph'}) MERGE (b:Person { name: 'and' }) MERGE (a)-[:NEXT{seqid:1}]->(b)
MERGE (a:Person { name: 'and' }) MERGE (b:Person { name: 'db'}) MERGE (a)-[:NEXT{seqid:1 , caps: 'true'}]->(b)
MERGE (a:Person { name: 'relational'}) MERGE (b:Person { name: 'db'}) MERGE (a)-[:NEXT{seqid:1}]->(b)
MERGE (a:Person { name: 'nosql'}) MERGE (b:Person { name: 'db' }) MERGE (a)-[:NEXT{seqid:2, caps: 'true'}]->(b)
MERGE (a:Person { name: 'server'}) MERGE (b:Person { name: 'and' }) MERGE (a)-[:NEXT{seqid:1}]->(b)
MERGE (a:Person { name: 'and' }) MERGE (b:Person { name: 'db'}) MERGE (a)-[:NEXT{seqid:1}]->(b)
MERGE (a:Person { name: 'server'}) MERGE (b:Person { name: 'and'}) MERGE (a)-[:CONNECTS{seqid:2}]->(b)
MERGE (a:Person { name: 'and' }) MERGE (b:Person { name: 'db'}) MERGE (a)-[:CONNECTS{seqid:2, caps: 'true'}]->(b)
ie 即
(graph)-[:NEXT{seqid:1 , caps: 'true'}]->(db)
(relational)-[:NEXT{seqid:1}]->(db)
(nosql)-[:NEXT{seqid:2, caps: 'true'}]->(db)
(server)-[:NEXT{seqid:1}]->(db)
(server)-[:CONNECTS{seqid:2, caps: 'true'}]->(db)
pls help me to solve this............. 请帮助我解决这个问题。
(I am using neo4j 2.3.6 community edition via java api in embedded mode..) (我正在通过嵌入式模式下的java api使用neo4j 2.3.6社区版。)
The obstacle here is that relationship types cannot be created dynamically. 这里的障碍是关系类型不能动态创建。 You cannot check for the incoming relationships, not knowing their type, and create a new relationship of that same type.
您无法检查传入的关系,不知道它们的类型,也不能创建相同类型的新关系。
If you know the types of the relationships you need to process, and can address those explicitly, then you can do this with Cypher. 如果您知道需要处理的关系类型并且可以明确解决这些关系,则可以使用Cypher来完成。 Here's the query to do this for all :NEXT relationships, copying over the properties of the relationship from the middle node to the end node over to the newly created relationship:
这是对所有:NEXT关系执行此查询的查询,将关系的属性从中间节点复制到末端节点,再复制到新创建的关系:
MATCH (middle:Person{name:'and'})
WITH middle
MATCH (from:Person)-[rFrom:NEXT]->(middle)
WHERE exists(rFrom.seqid)
WITH middle, rFrom, from
MATCH (middle)-[rTo:NEXT]->(to:Person)
WHERE rTo.seqid = rFrom.seqid
WITH middle, rFrom, from, rTo, to
CREATE (from)-[rNew:NEXT]->(to)
SET rNew += rTo
DELETE rFrom
You'll want to repeat this for every relationship type you're interested in, and when there are no more relationships to or from your middle node, delete the node. 您将要对感兴趣的每种关系类型重复此操作,并且当与中间节点之间没有更多的关系时,请删除该节点。
Note that if you do upgrade to neo4j 3, the APOC Procedures library has procedures for graph refactoring, which will easily take care of this. 请注意,如果确实要升级到neo4j 3,则APOC程序库具有用于图形重构的程序,这些程序可以轻松解决。
EDIT 编辑
Altered my Cypher above to do CREATE instead of MERGE. 改变了我上面的Cypher来创建而不是合并。
Also removed deletion of the relationships from the middle node to the next node, as you seem want to take the relationship properties from the relationship connecting the middle node to the next node, and since there may be multiple incoming relationships to the middle node with the same type and id, but only a single relationship from the middle node with that type and id. 还删除了从中间节点到下一个节点的关系的删除,因为您似乎想从连接中间节点到下一个节点的关系中获取关系属性,并且由于可能存在多个到中间节点的传入关系类型和ID相同,但中间节点与该类型和ID的关系只有一个。
This means your ratio of incoming relationships with the same type and id is not equal to the outgoing relationships of the same type and id, so we'll be reusing those outgoing relationships when creating the new relationships. 这意味着您具有相同类型和ID的传入关系的比率不等于具有相同类型和ID的传出关系的比率,因此我们在创建新关系时将重用这些传出关系。
Only after you're all done creating the new relationships should you detach and delete the middle node. 只有在完成创建新关系之后,才应该分离并删除中间节点。
Adding another answer that will fulfill all requirements, but requires Neo4j 3.0.x or greater. 添加另一个满足所有要求但需要Neo4j 3.0.x或更高版本的答案。 Specifically, this requires the apoc.create.relationship() procedure from APOC Procedures, which will let us create a relationship with a dynamic type, supplied from the matched from relationships.
具体来说,这需要APOC Procedures中的apoc.create.relationship()过程,该过程将使我们创建一个具有动态类型的关系,该关系是从matched from关系提供的。
This will take care of all relationships at once (at least those with seqid), so we should be okay to detach and delete the middle node at the end. 这将立即处理所有关系(至少具有seqid的关系),因此我们应该可以分离并最后删除中间节点。
MATCH (middle:Person{name:'and'})
WITH middle
MATCH (from:Person)-[rFrom]->(middle)
WHERE EXISTS(rFrom.seqid)
WITH middle, rFrom, from
MATCH (middle)-[rTo]->(to:Person)
WHERE TYPE(rTo) = TYPE(rFrom) AND rTo.seqid = rFrom.seqid
WITH middle, rFrom, from, rTo, to
CALL apoc.create.relationship(from, TYPE(rFrom), PROPERTIES(rTo), to) YIELD rel
DETACH DELETE middle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.