7

I'm doing a Neo4j hands-on exercise, following along to a UCSD video. I'm cutting and pasting the script provided for the exercises. I've just run into a problem with the provided script for a graph not containing the immediate neighborhood of a specified node:

match (d {Name:'D'})-[:TO]-(b)
with collect(distinct b.Name) as neighbors
match (n)-[r:TO]->(m)
where
not (n.Name in (neighbors+'D'))
and
not (m.Name in (neighbors+'D'))
return n, r, m;

match (d {Name:'D'})-[:TO]-(b)-[:TO]->(leaf)
where not((leaf)-->())
return (leaf);

match (d {Name:'D'})-[:TO]-(b)<-[:TO]-(root)
where not((root)<--())

return (root)

This returns:

Expected exactly one statement per query but got: 3

When I run the first 8 lines, Neo4j returns the graph, with expected nodes and edges. But when I add the subsequent queries, the error msgs start popping up.

James Z
  • 12,209
  • 10
  • 24
  • 44
James_Pineda
  • 71
  • 1
  • 5
  • 2
    You can only run a single query at once: copy and paste the `MATCH ... RETURN ...` statements separately. – Gabor Szarnyas Dec 16 '17 at 20:26
  • 1
    Hi @James_Pineda! If the answer I provided has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Thanks! – Bruno Peres Feb 28 '18 at 12:07
  • as for now you can enable multiple queries in neo4j browser. see https://neo4j.com/developer/neo4j-browser/#browser-tips – bastianowicz Jan 23 '21 at 11:44

3 Answers3

6

If you're using Neo4j Browser to run those CYPHERs, make sure multi statement query editor is enabled. You can enable it in Browser Settings by click the checkbox. enter image description here

ShawnZhu
  • 69
  • 1
  • 2
5

Neo4j Browser can run only one query at time. You are trying to run 3:

Query 1:

match (d {Name:'D'})-[:TO]-(b)
with collect(distinct b.Name) as neighbors
match (n)-[r:TO]->(m)
where
not (n.Name in (neighbors+'D'))
and
not (m.Name in (neighbors+'D'))
return n, r, m;

Query 2

match (d {Name:'D'})-[:TO]-(b)-[:TO]->(leaf)
where not((leaf)-->())
return (leaf);

Query 3:

match (d {Name:'D'})-[:TO]-(b)<-[:TO]-(root)
where not((root)<--())

return (root)

You must copy, paste and run these 3 queries separately.

Here is an open issue in the Neo4j Browser Github Repo about supporting multiple Cypher statements at a time in the browser, but this is specifically for statements that don't return any data.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • The other possibility is to join them together to make a single query getting rid of intermediate RETURN statements and adding a WITH statement between each remembering to pass whatever is needed for the following statement(s). Clearly this makes most sense where following queries build on whatever is matched in the earlier ones. – wikitect Dec 18 '17 at 15:13
  • 1
    Just noting (for posterity) that this was once true, but the Neo4j Browser now has support for multi-statement queries, and can handle this (with the appropriate configuration toggled on) – InverseFalcon Feb 12 '20 at 20:03
3

A quick solution which worked for me (also mentioned at the neo4j.com/graphacademy/online-training/) when playing with an online sandbox:

Before you start

Enable multi-statement query editor

  1. Click the Browser Settings button in the lower left side of Neo4j Browser

    settings icon

  2. Make sure that the Enable multi statement query editor checkbox is selected:

    setting option image

tgogos
  • 23,218
  • 20
  • 96
  • 128