简体   繁体   English

Neo4j只显示一个节点的1对多关系

[英]Neo4j display only one node for 1 to many relationship

i'm trying to solve a problem of the 1: many relationship display in neo4j. 我正在尝试解决neo4j中1:many关系显示的问题。 My dataset is as below 我的数据集如下

child,desc,type,parent
1,PGD,Exchange,0
2,MSE 1,MSE,1
3,MSE 2,MSE,1
4,MSE 3,MSE,1
5,MSE 4,MSE,1
6,BRAS 1,BRAS,2
6,BRAS 1,BRAS,3
7,BRAS 2,BRAS,4
7,BRAS 2,BRAS,5
10,NPE 1,NPE,6
11,NPE 2,NPE,7
12,OLT,OLT,10
12,OLT,OLT,11
13,FDC,FDC,12
14,FDP,FDP,13
15,Cust 1,Customer,14
16,Cust 2,Customer,14
17,Cust 3,Customer,14
LOAD CSV WITH HEADERS FROM 'file:///FTTH_sample.csv' AS line 
CREATE(:ftthsample
{child_id:line.child,
desc:line.desc,
type:line.type,
parent_id:line.parent});

//Relations 
match (child:ftthsample),(parent:ftthsample)
where child.child_id=parent.parent_id 
create (child)-[:test]->(parent)

//Query:
MATCH (child)-[childrel:test*]-(elem)-[parentrel:test*]->(parent) 
WHERE elem.desc='FDP'
RETURN child,childrel,elem,parentrel

It returns a display as below. 它返回如下显示。

产量

I want the duplicate nodes to be displayed as one. 我希望重复的节点显示为一个。 Newbie with Neo4J. 新手与Neo4J。 Can anyone of the experts help please? 任何专家都可以帮忙吗?

This seems like an error in your graph creation query. 这似乎是图创建查询中的错误。 You have a few lines in your query specifying the same node multiple times, but with multiple parents: 您的查询中有几行指定多个相同的节点,但有多个父节点:

6,BRAS 1,BRAS,2
6,BRAS 1,BRAS,3

I'm guessing you actually want this to be a single node, with parent relationships to nodes with the given parent ids, instead of two separate nodes. 我猜你真的希望这是一个单一的节点,父节点与给定父id的节点,而不是两个单独的节点。

Let's adjust your import query. 让我们调整您的导入查询。 Instead of using a CREATE on each line, we'll use MERGE, and just on the child_id , which seems to be your primary key (maybe consider just using id instead, as a node can have an id on its own, without having to consider the context of whether it's a parent or child). 而不是在每一行上使用CREATE,我们将使用MERGE,并且只使用child_id ,这似乎是您的主键(可能只考虑使用id ,因为节点可以自己拥有一个id,而不必考虑它是父母还是孩子的背景。 We can use the ON CREATE clause after MERGE to add in the remaining properties only if the MERGE resulted in node creation (instead of matching to an existing node. 只有当MERGE导致节点创建(而不是匹配现有节点)时,我们才能在MERGE之后使用ON CREATE子句添加其余属性。

That will ensure we only have one node created per child_id . 这将确保我们每个child_id只创建一个节点。

Rather than having to rematch the child, we can use the child node we just created, match on the parent, and create the relationship. 我们可以使用刚创建的子节点,匹配父节点,并创建关系,而不必重新匹配子节点。

LOAD CSV WITH HEADERS FROM 'file:///FTTH_sample.csv' AS line 
MERGE(child:ftthsample {child_id:line.child})
ON CREATE SET
child.desc = line.desc,
child.type = line.type

WITH child, line.parent as parentId
MATCH (parent:ftthsample)
WHERE parent.child_id = parentId
MERGE (child)-[:test]->(parent)

Note that we haven't added line.parent as a property. 请注意,我们尚未将line.parent添加为属性。 It's not needed, since we only use that to create relationships, and after the relationships are there, we won't need those again. 它不是必需的,因为我们只使用它来创建关系,并且在关系存在之后,我们将不再需要它们。

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

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