根据节点之间现有的关系创建节点之间的关系
我在两个用户之间有多个关系,如下所示:
(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)
我想将所有这些关系组合成两个用户之间的单个关系(同时保留旧的关系),同时保留节点的值来创建列表并设置如下新关系的价值是这样的:
(u1:User)-[:R {data:["A", "B", "C"]}]-(u2:User)
有什么办法可以做到这一点吗?
编辑:关键字可以出现多次。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的测试数据。我创建了不同场景的示例数据。问题是 u1 和 u2 具有非方向关系,因此您将获得从 u1 到 u2 以及 u2 到 u1 的重复路径。
步骤1是确保u1和u2不相同。然后收集不同的值并按该值对其进行排序。然后我创建了所有节点的集合来生成行号(称为 uw 或 row)。然后仅为偶数行创建关系,以便不存在重复项。最后,使用值作为数据创建从 u1 到 u2 的关系。
结果:
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.
Step1 is to ensure that u1 and u2 are not the same. 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). 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.
Result:
我不知道这是否是最有效的方法,但我可以使用以下命令来实现此目的:
编辑:然后删除重复的关系:
I don't know if this is the most efficient way but I'm able to achieve this with thee following command:
Edit: And then remove the duplicate relationships with: