18

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?

Dirk Calloway
  • 2,569
  • 4
  • 23
  • 34

7 Answers7

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

jpenna
  • 8,426
  • 5
  • 28
  • 36
11

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

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

Community
  • 1
  • 1
NikoNyrh
  • 3,578
  • 2
  • 18
  • 32
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.

Victor Ermolaev
  • 721
  • 1
  • 5
  • 16
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

Chris Leishman
  • 1,777
  • 13
  • 19
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.

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

Clem Wang
  • 689
  • 8
  • 14
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

Mr.yl
  • 91
  • 1
  • 3