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.

Duplicated Node info

Hello!! I have two labels which represent money transfers. We have the column sender, receptor, quantity, date and specific code value whis is different for each transaction:

sender;receptor;quantity;date;code
Almu;Joselu;200;17/02/2020;AJ01
Joselu;Angel;500;17/02/2020;JA01
Angel;Isai;2;17/02/2020;AI01
Joselu;Isai;66;17/02/2020;JI01
Alexo;Angel;10;17/02/2020;AA01
Almu;Alexo;600;17/02/2020;AA02
Isai;Alexo;1000;17/02/2020;IA01
Emilio;Joselu;10;17/02/2020;EJ01

I import this data with a Load statement and at the end I get my two labels sender and receptor

The import code is like this:

LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
CREATE (e:receptor {receptor:c.receptor, code.code})

And similar for the sender.

My problem is that when I try to create a relationship between sender and receptor, I get conexions that are repeated.
The code that I use is:

MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:son]->(b)

And it look like:

I would like not to have nodes that are repeated. Something that looks like:
2X_e_e179538cb98f7c9a222d4cf93e9aac8d914f88bb.png

Thanks in advance for your time and help!

8 REPLIES 8

Hello!

You should create a uniqness constraint to avoid duplicity. For example:
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE;

And probably use MERGE instead of CREATE in the LOAD sentence, then create the relationships.

Hello Pilar!

So I should write:
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})

And then for each of the labels create the constraint:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE

And for making the relation, like this:
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)

Thanks!!

That looks good 😉

Besides, consider setting all the information about the money transfer: quantity, date and code in the relationship.

You might want to do that first. That way the Merge will perform with more reasonable response times as it creates index.

But if I have already created the constraint, when I try to merge the rest of the info, I get an error telling me that Node(9546906) already exists with label Sender and property sender = 'Joselu'

My code is:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE

LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})

MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)

However, as I have said, when I try the first Load I get the error...

Thanks!

Try to rewrite the code in this way:

CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE

LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Receptor {receptor:c.receptor})
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)

Thanks Pilar for all your help, I really appreciate all your effort.

Your code works perfect. It created the unique nodes in the correct way. However, for creating the relationship I need to add the code of the transsaction when I am loading the properties, and it gives me error. I saw you:

  1. code
    CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE

  2. code
    CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE

3.code
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender, code:c.code})

  1. code
    LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
    Merge (e:Receptor {receptor:c.receptor, code:c.code})

5.code
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)

In the 1st load with the code:c.code propertie I get:
Node(47) already exists with label AA and property emisor = 'Joselu'

Adding the property gives me an error...

Since the constraint is only on sender, It will try to treat only sender as unique. If the sender and code combination is unique, then the constraint should be on the combination.

CREATE CONSTRAINT ON (e:Sender)  ASSERT (e.sender, e.code) IS NODE KEY

Can you please try that?