Reputation: 2619
I want to submit a list of semi-colon separated Cypher write statements via the web browser but I'm getting errors...
MERGE (a:user{id: 'A'})
MERGE (b:product{id: '1'})
CREATE UNIQUE (a)-[:USED_BY]->(b);
MERGE (a:user{id: 'B'})
MERGE (b:product{id: '4'})
CREATE UNIQUE (a)-[:USED_BY]->(b);
I'm creating new nodes and referring to them in later relationship statements so I want to submit separate queries rather than one long one and I'd like to do this via Cypher.
What's the best way to do this?
Upvotes: 19
Views: 25644
Reputation: 91
MERGE (a:user{id: 'A'})
MERGE (b:product{id: '1'})
CREATE UNIQUE (a)-[:USED_BY]->(b)
MERGE (c:user{id: 'B'})
MERGE (d:product{id: '4'})
CREATE UNIQUE (c)-[:USED_BY]->(d)
Try this. It also works
Upvotes: 0
Reputation: 739
I believe apoc.cypher.runMany will do the trick.
CALL apoc.cypher.runMany(statement, {})
(I'm in the process of trying this out via Python Driver. I'll update this post later with my findings.)
Upvotes: 0
Reputation: 9191
There is a simple setting to do this now: Enable multi statement query editor
.
Then you can run multiple statements separated by semi-colons ;
Upvotes: 21
Reputation: 4138
I found a solution at Multiple unrelated queries in Neo4j Cypher?
Simply put WITH count(*) as dummy
between independent commands.
Upvotes: 11
Reputation: 1
Convert all your Cypher statements into one single line and put a space between each of the statements. This works in neo4j browser console.
Upvotes: 0
Reputation: 1798
This isn't a bug, but rather the expected behaviour (as of the time of writing, e.g. Neo4j 2.2 and earlier).
The Neo4j Browser runs each command entered as a single query, and displays the results of that query. It does not support multiple queries (or the implied multiple sets of results!).
Note that it is possible to use neo4j shell to run multiple queries, as you're trying to do. The results of each individual command will be written to stdout. Examples are in the documentation: http://neo4j.com/docs/stable/shell-sample-session.html
Upvotes: 2
Reputation: 791
As far as I know it is not possible to do directly, though if you have python you could install py2neo and then use a very easy snippet which makes use of REST api of neo4j, i.e.
from py2neo import cypher
session = cypher.Session("http://localhost:7474/db/data/")
tx = session.create_transaction()
cypher = [
"MERGE (a:user{id: 'A'})"
"MERGE (b:product{id: '1'})"
"CREATE UNIQUE (a)-[:USED_BY]->(b)", #first statement
"MERGE (a:user{id: 'B'})"
"MERGE (b:product{id: '4'})"
"CREATE UNIQUE (a)-[:USED_BY]->(b)" #second statement
]
for q in cypher:
tx.append(q)
tx.commit()
this will do the job.
Upvotes: 3