如何使用 cypher 与 Parent 和 Child 建立关系?

How do I create a relationship with Parent and Child using cypher?

我已使用 LOAD 功能将我的 CSV 导入 Neo4j。但是,我不确定如何将所有父类别映射到子类别。

这是 CSV 示例:

category_id,category,description,parent_id,is_adult
4,Electronics,Electronics,0,0
5,Audio Equipment,Audio Equipment,4,0
6,Home Entertainment,Home Entertainment,4,0
7,Photography,Photography,4,0
8,Portable Audio,Portable Audio,4,0
9,Televisions,Televisions,4,0
10,Amplifiers & Receivers,Amplifiers & Receivers,5,0
11,Audio Systems,Audio Systems,5,0
12,Cassette Decks,Cassette Decks,5,0
13,CD Players,CD Players,5,0
14,Radios,Radios,5,0
15,HiFi Speakers,HiFi Speakers,5,0
17,MiniDisc Separates,MiniDisc Separates,5,0
18,Tuners,Tuners,5,0
19,DJ Equipment,DJ Equipment,5,0
20,DVD Players,DVD Players,6,0
21,DVD Recorders,DVD Recorders,6,0
22,Headphones,Headphones,6,0
23,Home Cinema,Home Cinema,6,0
24,Projectors,Projectors,6,0
25,Remote Controls,Remote Controls,6,0
26,Set Top Boxes & Receivers,Set Top Boxes & Receivers,6,0
27,VCR Players,VCR Players,6,0
29,Camcorders,Camcorders,7,0
30,Accessories,Accessories,7,0
32,Cameras,Cameras,7,0

如您所见,category_id(11) 有一个 parent_id(5)

我使用的 LOAD CSV 命令是:

USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///tmp/categories.csv" AS row 
CREATE (:Category {category_id: row.category_id, category: row.category, description: row.description, parent_id: row.parent_id, is_adult: row.is_adult});

我还没有使用 is_adult(布尔值)的转换。

我是 Neo4j 的新手,欢迎任何帮助。

如果您提供 load csv

可能会有所帮助

我不确定您在导入数据时以节点标签的方式提供了什么,但是这个示例应该找到尚未与数据中存在父类别的父类别匹配的子项(即大于等于 4)。然后它应该根据节点上的父 ID 匹配父节点,并在子节点和父节点之间创建 :CHILD_OF 关系。

match (child:Category)
where not (child-[:CHILD_OF]->())
and child.parent_id >= 4
with child
match (parent:Category)
where parent.category_id = child.parent_id
create child-[:CHILD_OF]->parent

我使用了您的 CSV 文件并在 Neo4j 中创建了一个数据库,然后使用以下内容创建了关系:

MATCH (child:Category) WHERE child.parent_id <> 0
MATCH (parent:Category {category_id: child.parent_id})
CREATE (child)-[:CHILD_OF]->(parent)
RETURN null;

我看到你已经选择了第一个(也是正确的)答案,但这是另一种给猫剥皮的方法。