hnnn
hnnn

Reputation: 504

neo4j creating more relationships in one query

i want to create more replationships from one node in one query

MATCH (n:Type) WHERE n.user = "usr2" 
MATCH (p:Test) WHERE p.name = "test1"
CREATE (n)-[:CONTAINS {amount:5}]->(p)
MATCH (p1:Test) WHERE p1.name = "test2"
CREATE (n)-[:CONTAINS {amount:-7}]->(p1)

but im getting invalid syntax.

is there any other way than this?

MATCH (n:Type) WHERE n.user = "usr2"
MATCH (p:Test) WHERE p.name = "test1"
CREATE (n)-[:CONTAINS {amount:5}]->(p); 
MATCH (n:Type) WHERE n.user = "usr2" 
MATCH (p:Test) WHERE p1.name = "test2"
CREATE (n)-[:CONTAINS {amount:-7}]->(p);

Because this doesnt seems to be very efficient for lot of records.

Upvotes: 1

Views: 142

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41676

As the error message says, use WITH after create:

MATCH (n:Type) WHERE n.user = "usr2" 
MATCH (p:Test) WHERE p.name = "test1"
CREATE (n)-[:CONTAINS {amount:5}]->(p)
WITH n
MATCH (p1:Test) WHERE p1.name = "test2"
CREATE (n)-[:CONTAINS {amount:-7}]->(p1)
  1. make sure to have an index/constraint on :Type(user) and :Test(name)
  2. use parameters
  3. use simple statements but many of them then cypher can cache the execution plans
  4. execute many simple statements in a single http request with the cypher transactional endpoint

Upvotes: 2

Dave Bennett
Dave Bennett

Reputation: 11216

How about this? Matches up front and creates afterwards?

MATCH (n:Type) WHERE n.user = "usr2" 
MATCH (p:Test) WHERE p.name = "test1"
MATCH (p1:Test) WHERE p1.name = "test2"
CREATE n-[:CONTAINS {amount:5}]->p
CREATE n-[:CONTAINS {amount:-7}]->p1

Upvotes: 2

Related Questions