Submitting multiple semi-colon separated Cypher statements through Neo4j Browser
Asked Answered
F

7

19

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?

Fulminous answered 6/12, 2013 at 9:12 Comment(2)
Submitted a feature request for this: github.com/neo4j/neo4j/issues/3908Ineducation
You should do that in the shell instead.Liquefy
B
21

There is a simple setting to do this now: Enable multi statement query editor.

Then you can run multiple statements separated by semi-colons ;

neo4j screenshot with the setting highlighted and result of both statements above

Bea answered 30/8, 2019 at 5:20 Comment(1)
But why 'match (n) where n.url="" return n; match (z) where z.titre="" return z' is green checked but with no returned nodes (where I know they exist with separated commands) ?Moujik
A
11

I found a solution at Multiple unrelated queries in Neo4j Cypher?

Simply put WITH count(*) as dummy between independent commands.

Aponeurosis answered 3/1, 2016 at 20:40 Comment(0)
J
2

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.

Junji answered 24/1, 2014 at 15:15 Comment(0)
A
2

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

Alpaca answered 29/1, 2015 at 22:33 Comment(0)
E
0

Convert all your Cypher statements into one single line and put a space between each of the statements. This works in neo4j browser console.

Ensoul answered 18/9, 2015 at 7:23 Comment(0)
C
0

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.)

Curtcurtail answered 17/5, 2021 at 18:5 Comment(0)
S
0
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

Swoon answered 6/10, 2021 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.