如何为cypher查询返回的节点添加关系?
我编写了一个查询,它返回了所有姓氏相同且姓氏居住在同一地址的人员节点。我现在想在这些人节点之间添加一个关系,表明他们是同一个人。下面的查询返回3个人节点,我想从第一个节点(由ORDER BY返回)添加到另一个节点的关系。
MATCH (a:Address) <-[LIVES_AT]-(p:Person)
WITH a as addnode, p.surname as psurname, COUNT(p.name_urn) as c
WHERE c > 1
MATCH (a2:Address{address_urn:addnode.address_urn})<-[LIVES_AT]- (p2:Person{surname:psurname})
WITH p2 as p2node
ORDER BY CASE
WHEN p2node.master_record = 'Y'
THEN
1
ELSE
2
END
WITH collect(p2node) as colp2node
RETURN colp2node
希望这有道理吗?请告知是否有更好的方法。
答案 0 :(得分:0)
这样的事情对你有用:
MATCH (a:Address)<-[LIVES_AT]-(p:Person)
WITH a, p.surname AS psurname, COUNT(p.name_urn) AS c
WHERE c > 1
MATCH (a2:Address { address_urn:a.address_urn })<-[LIVES_AT]-(p2:Person { surname:psurname })
WITH p2
ORDER BY CASE WHEN p2.master_record = 'Y' THEN 1 ELSE 2 END
WITH collect(p2) AS colp2
WITH colp2[0] AS master, colp2[1..] AS others
UNWIND others AS other
MERGE (master)-[:HAS_ALIAS]->(other);
我使用MERGE
来避免重复关系。
顺便说一下,仅仅因为两个人的姓氏相同并住在同一个地址,这通常不代表他们是同一个人。我希望你确定你所做的是恰当的。