Can you insert relationship ID to other relationship when creating nodes in Neo4J?

Viewed 28

I have one CQL syntax, and I don't understand how to add relationship id to another relationship within a single query. This is the sample that might give clearer description about what I need

CREATE
(a:IP_ADDRESS {name: "224.36.71.118"}),
(b:TARGET {honeypot: "cowrie", target_post: "2222", target_protocol: "tcp", analyzer_id: "VM1"}),
(c:Unknown {name: 'uname -a;lspci', threat_phase:"Unknown", threat_purpose: "Unknown", threat_category: "Unknown", time: "2022-07-21T00:02:03+0000"}),

(a)-[r:ATTACK {time: "2022-08-21T19:51:05+0000"}]->(b),
(b)-[:GO_TO {attack_id: r.id}]->(c)

ASo, what I'm trying to achieve is, I want to insert the relationship id from (a)->(b) into the relationship from (b)->(c). I already tries to use r.id as the relationship data, but it doesn't work. The query doesn't return error, but the (b)->(c) relationship didn't successfully stored the attack_id

2 Answers

To get the value of the object id of a node or an edge, you will use id(r) and NOT r.id. The property id is a user-defined property so you should define it while id(r) is a function created by neo4j to get the object id of r. Below is the correct query;

CREATE
(a:IP_ADDRESS {name: "224.36.71.118"}),
(b:TARGET {honeypot: "cowrie", target_post: "2222", target_protocol: "tcp", analyzer_id: "VM1"}),
(c:Unknown {name: 'uname -a;lspci', threat_phase:"Unknown", threat_purpose: "Unknown", threat_category: "Unknown", time: "2022-07-21T00:02:03+0000"}),
(a)-[r:ATTACK {time: "2022-08-21T19:51:05+0000"}]->(b),
(b)-[:GO_TO   {attack_id: id(r)}]->(c)

Result:

enter image description here

If you use separate CREATE statements, it works perfectly. Try this

CREATE (t1:Test),
       (t2:Test),
       (t3:Test)
CREATE (t1)-[r:REL_TO]->(t2)
CREATE (t2)-[r2:REL_TO {idr: id(r)}]->(t3)
RETURN id(r), properties(r2)

which returns

╒═══════╤════════════════╕
│"id(r)"│"properties(r2)"│
╞═══════╪════════════════╡
│12     │{"idr":12}      │
└───────┴────────────────┘
Related