neo4j - 从一个节点到另一个节点的多个关系
问题描述
我是neo4j的新手。我有一个 .csv 文件,其中两列由“,”分隔。第一列包含名字,第二列包含姓氏:
Lname,Fname
Brown,Helen
Right,Eliza
Green,Helen
Pink,Kate
Yellow,Helen
我想为 Lname 列创建节点,为Fname列创建节点。对于具有相同 Fname 的行,我想将 Lname 连接到相应的 Fname。例如,我想要一个“Helen”节点,其中三个节点“Brown”、“Green”和“Yellow”连接到“Helen”。我还想将“Fname”节点连接到“中心节点”。我写了这段代码:
LOAD CSV WITH HEADERS FROM 'file:///names.csv' AS row
WITH row.Fname AS first, row.Lname AS last
MERGE (p:la {last: last})
MERGE (o:fi {first: first})
MERGE (c:central {name: "central node"})
MERGE (c)-[r:CONTAINS {first:first}]->(o)-[rel:CONTAINS {first: first}]->(p)
RETURN count(o)
当我运行此代码并使用此查询显示输出时:
MATCH (c:central)-[r:CONTAINS]->(o:fi)-[rel:CONTAINS]->(p:la)
RETURN c, r, o, rel, p
如您所见,根据姓氏的数量,我与名字的关系数量相同,例如我有 3 个从“中心节点”到“海伦”的关系,但我只想要一个来自“中心节点”的关系到“海伦”。这里有什么问题?
解决方案
The answer lies in your final MERGE
clause.
MERGE (c)-[r:CONTAINS {first:first}]->(o)-[rel:CONTAINS {first: first}]->(p)
Neo4j will take this entire pattern and ensure it is unique. Since each time it is invoked (due to the last name changing) the whole thing is created. If you would like to have a single relationship from the central to the first name nodes then you need to split it up into two separate parts. Using the following the first MERGE
will only create the central-first relationship once.
MERGE (c)-[r:CONTAINS {first:first}]->(o)
MERGE (o)-[rel:CONTAINS {first: first}]->(p)
推荐阅读
- c# - MassTransit/RabbitMq 错误队列 - 如何删除消息?
- flutter - 使用 void 回调颤动的小部件通信
- c++ - 如何在我的 C++ 代码中使用 rapidjson 库?
- java - 在集成测试期间不要创建spring bean
- android - 在根项目“android”中找不到任务“assembleFIRDebugEnabledDebug”
- r - R中数据框中所有列的ECDF图
- sql-server - 使数据库表暂时只读(并且不引发错误)
- c# - 如何使用.Net Core 从网络接口接收所有 IPV6 数据包?
- python - Django ORM 使用字典数组排除记录
- mysql - 如何在 React 页面上显示存储在 MySQL 中的图像