cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Create node and multiple relationships if not exists (hyper edge)

I am trying to model a hyperedge in a graph, where a CHUNK node has relations to TOKEN nodes.
First I need to create if not exists, the tokens.
Then I need to check if the CHUNK with relations to these two nodes exists.
If not exists, then I need to create a CHUNK node and also create the relations.

This is the Query that I have come up with:

MERGE 
	(t:TOKEN {text: "test token"})
MERGE    
	(p: PERSON {text:"test person"})
with t,p
OPTIONAL MATCH 
	(c:CHUNK)
WHERE
	(c)-[:CONTAINS_TOKEN]->(t)
	AND 
    (c)-[:CONTAINS_TOKEN]->(p)
    
FOREACH (ignoreme in case when c is NULL then [1] ELSE [] END|
CREATE (c: CHUNK)
CREATE (c)-[:CONTAINS_TOKEN]->(p)
CREATE (c)-[:CONTAINS_TOKEN]->(t)
)

My question is, is this the best way to try and merge a node and multiple relations at the same time?
are there any APOC procedures that make this easier? I ask this because maybe at one point I need to create a hyper edge with more than two tokens, and then the query becomes quite large

1 REPLY 1

jggomez
Graph Voyager

Hi, you can use UNWIND. Neo4j supports many transactions.

Thanks