简体   繁体   English

根据节点之间的现有关系创建节点之间的关系

[英]Create a relationship between nodes based on existing relationship between the nodes

I have multiple relationships between two users that are as follow:我在两个用户之间有多种关系,如下所示:

(u1:User)-[:BR]-(b:Buyer {value:"A"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"B"})-[:BR]-(u2:User)
(u1:User)-[:BR]-(b:Buyer {value:"C"})-[:BR]-(u2:User)

I'd like to combine all of them into a single relationship (while keeping the old ones) between the two users, while keeping the node's value to create a list and set is as the new relationship's value like this:我想将它们全部组合成两个用户之间的单一关系(同时保留旧关系),同时保留节点的值以创建列表并设置为新关系的值,如下所示:

(u1:User)-[:R {data:["A", "B", "C"]}]-(u2:User)

Is there any way to do this?有什么办法吗?

Edit: keywords can appear more than once.编辑:关键词可以出现不止一次。

This is my test data.这是我的测试数据。 I created sample data with different scenarios.我创建了具有不同场景的示例数据。 The problem is u1 and u2 have non-directional relationships so you will get duplicated paths from u1 to u2 and u2 to u1.问题是 u1 和 u2 具有非定向关系,因此您将获得从 u1 到 u2 和 u2 到 u1 的重复路径。

CREATE (u1:User)
CREATE (u2:User)
CREATE (u3:User)
CREATE (ba:Buyer {value:"A"})
CREATE (bb:Buyer {value:"B"})
CREATE (bc:Buyer {value:"C"})
CREATE (bd:Buyer {value:"D"})
MERGE (u1)-[:BR]-(ba)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bb)-[:BR]-(u2)
MERGE (u1)-[:BR]-(bc)-[:BR]-(u2)
MERGE (u2)-[:BR]-(bb)-[:BR]-(u3)
MERGE (u2)-[:BR]-(bd)-[:BR]-(u3)

Step1 is to ensure that u1 and u2 are not the same. Step1是保证u1和u2不一样。 Then collect distinct value and sort it by this values.然后收集不同的值并按此值对其进行排序。 Then I created a collection of all the nodes to generate a row number (called it uw or row).然后我创建了所有节点的集合以生成行号(称为 uw 或行)。 Then create the relationship for even rows only so that there is no duplicates.然后仅为偶数行创建关系,以便没有重复项。 Lastly, create the relationship from u1 to u2 using values as data.最后,使用值作为数据创建从 u1 到 u2 的关系。

MATCH (u1:User)-[:BR]-(b:Buyer)-[:BR]-(u2:User) where u1 <> u2
WITH u1, u2, collect(distinct b.value) as v  order by v
WITH collect([u1,u2,v]) as cols 
UNWIND range(1, size(cols)) as uw 
WITH uw as row, cols[uw-1][0] as u1, cols[uw-1][1] as u2, cols[uw-1][2] as values WHERE row%2=0
MERGE (u1)-[:R {data: values}]-(u2)

Result:结果: 在此处输入图像描述

I don't know if this is the most efficient way but I'm able to achieve this with thee following command:我不知道这是否是最有效的方法,但我可以使用以下命令实现此目的:

match (u1:User)-[:BR]-(b:Buyers)-[:BR]-(u2:User) with u1, u2, collect(b.value) as bys merge (u1)-[:R {data:bys}]-(u2)

Edit: And then remove the duplicate relationships with:编辑:然后删除与以下内容的重复关系:

match (u1:User {userid:"622f0137d799ed4369b077e1"})-[r:R]->(u2)-[:R]->(u1)
DELETE r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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