Dirk Calloway
Dirk Calloway

Reputation: 2619

Submitting multiple semi-colon separated Cypher statements through Neo4j Browser

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

Answers (7)

Mr.yl
Mr.yl

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

Clem Wang
Clem Wang

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

jpenna
jpenna

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 ;

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

Upvotes: 21

NikoNyrh
NikoNyrh

Reputation: 4138

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

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

Upvotes: 11

Deepita Pai
Deepita Pai

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

Chris Leishman
Chris Leishman

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

Victor Ermolaev
Victor Ermolaev

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

Related Questions