cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to create relationship between 2 nodes in the same node labels?

11li
Node Clone

Hello everyone!
I'm trying to load some csv files to create nodes and relationship.
But there is a problem I can't understand and solve.

Here is an example:
In the file [navtech.csv] I created some 'NavTech' nodes.
3X_3_c_3c20166a2e655feb96d9336259f340bc046167da.png
GPS系统 is one of them, which navtech_id = 4.
And you can see 全球定位系统's navtech_id = 3.

In the file[r_navtech_navtech_sameas.csv] I created some relationship.
3X_e_2_e23a36e7df30968169d39c1f5d56d4c28d27951a.png

so this is supposed to be [GPS系统 -same_as -> 全球定位系统]
while the result turns out to be:

I couldn't understand since I didn't create this relationship.
and the subclass_of in this picture is in the same situation.
And if click this node, 全球定位系统 still didn't appear.

This happens to many nodes. The relationship I want to create between 2 different node in same label,but it became the relationship between the same 1 node.

Followings are my operations..

CREATE CONSTRAINT constraint_nav IF NOT EXISTS ON (n:Nav) ASSERT n.nav_id IS UNIQUE;
CREATE CONSTRAINT constraint_nav_tech IF NOT EXISTS ON (n:NavTech) ASSERT n.navtech_id IS UNIQUE;
CREATE CONSTRAINT constraint_dev IF NOT EXISTS ON (n:Device) ASSERT n.dev_id IS UNIQUE;
CREATE CONSTRAINT constraint_sensor_type IF NOT EXISTS ON (n:SensorType) ASSERT n.sensortype_id IS UNIQUE;

create nodes.

LOAD CSV WITH HEADERS FROm 'file:///nav.csv' AS line
MERGE (n:Nav {nav_id: line.nav_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///navtech.csv' AS line
MERGE (n:NavTech {navtech_id: line.navtech_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///dev.csv' AS line
MERGE (n:Device {dev_id: line.dev_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///SensorType.csv' AS line
MERGE (n:SensorType {sensortype_id: line.sensortype_id}) SET n += line

create relationship.

LOAD CSV WITH HEADERS FROm 'file:///r_nav_dev.csv' AS line
MATCH (a:Nav {nav_id: line.nav_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_nav_navtech.csv' AS line
MATCH (a:Nav {nav_id: line.nav_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:adopt]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_dev.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_use.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_subclassof.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_sameas.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:same_as]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_sensortype_dev.csv' AS line
MATCH (a:SensorType {sensortype_id: line.sensortype_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (b)-[:subclass_of]->(a)

Files:
navtech.txt (1.2 KB)
r_nav_dev.txt (458 Bytes)
r_nav_navtech.txt (682 Bytes)
dev.txt (750 Bytes)
nav.txt (32 Bytes)
r_navtech_navtech_subclassof.txt (663 Bytes)
r_navtech_navtech_use.txt (199 Bytes)
r_sensortype_dev.txt (691 Bytes)
r_navtech_dev.txt (257 Bytes)
r_navtech_navtech_sameas.txt (77 Bytes)
SensorType.txt (201 Bytes)

Sorry, my problem is a little troublesome.

1 ACCEPTED SOLUTION

hi,

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_use.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_subclassof.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_sameas.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:same_as]->(b)

The three above blocks of code creating relations are each matching the same node in each because you are specifying the same line attribute navtech_id in each match clause, so it is matching the same node and creating a relation between the same node.

The confusion is resulting because your two columns in your relationship-specifying files have duplicate header names. Neo4j is probably using the last value of navtech_id that it reads as it parses each line when headings are the same. The solution is to change the headings to be unique and update your code to reflect the change.

If that is not practical, you can use indexes instead. For instance:

LOAD CSV FROM 'file:///r_navtech_navtech_subclassof.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:subclass_of]->(b)

View solution in original post

8 REPLIES 8

11li
Node Clone

and 3 other relationship I didn't create....
found in schema.

hi,

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_use.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_subclassof.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_sameas.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:same_as]->(b)

The three above blocks of code creating relations are each matching the same node in each because you are specifying the same line attribute navtech_id in each match clause, so it is matching the same node and creating a relation between the same node.

The confusion is resulting because your two columns in your relationship-specifying files have duplicate header names. Neo4j is probably using the last value of navtech_id that it reads as it parses each line when headings are the same. The solution is to change the headings to be unique and update your code to reflect the change.

If that is not practical, you can use indexes instead. For instance:

LOAD CSV FROM 'file:///r_navtech_navtech_subclassof.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:subclass_of]->(b)

Thank you very much!!!

Sorry...I find another problem.
I use the way you wrote to load csv. And you can see the schema has 10 relationship lines,but I only create 7 relationship lines.

My method is the same as that written on the first floor, only the following parts have been changed.

LOAD CSV  FROM 'file:///r_navtech_navtech_use.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:use]->(b)

LOAD CSV FROM 'file:///r_navtech_navtech_subclassof.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:subclass_of]->(b)


LOAD CSV FROM 'file:///r_navtech_navtech_sameas.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:same_as]->(b)

I executed each of your imports. I did not see those relationships in the final data set. Here is a list of all the relationships that exist in the data:

match(n)-[r]->(m)
return distinct([labels(n)[0], labels(m)[0], type(r)]) as distinctRelationships

I also got back the same schema graph. Those relationships you highlighted do not show up in my data either. I don't know why the schema shows them.

Well, that's cofusing...


I also show 7 relationships here.
But the schema show 10 relationships...

I am puzzled as well. Maybe a staff member can help.

Could you please tell me how can I get help from a staff member?