简体   繁体   中英

Creating relationships of nodes of the same kind in Neo4j Cypher

Here is a part from the dataframe that im working with:

A_PERSON, B_PERSON, MESSAGE_TIME
SARAH, RON, 13:20 pm
RON, TIM, 13:33 pm
TIM, MARTHA, 13:45 pm
RON, LARRY, 13:35 pm
LARRY, JIM, 13:38 pm

I want to create a graph that shows all the messages sent between these people, and the arrows should show the time of the message.

I've tried something like this:

load csv with headers 
from "file:\\df.csv" as row 
create(m: Message {a_person: row.A_PERSON, b_person: row.B_PERSON, time: row.MESSAGE_TIME})

so that later I could MATCH them but it doesn't seem possible. The result has some A_PERSON duplicates in it because of the second and third label. So I don't understand how to create unique nodes and connect them to each other. Is it possible to do? And if it is, then how?

The key to this is to use the MERGE clause. MERGE each person and then MERGE the relationship between the two people.

MERGE will create the person if they don't exist or MATCH them if they do.

LOAD CSV WITH HEADERS 
FROM "file:\\df.csv" AS row 
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE {time: row.MESSAGE_TIME}]->(b_person)

or, if you anted message to be a node, you could do something like this.

LOAD CSV WITH HEADERS 
FROM "file:\\df.csv" AS row 
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE]->(:Message {time: row.MESSAGE_TIME})-[:TO]->(b_person)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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