Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher WITH is required between CREATE and MATCH

Tags:

I want to execute several queries at the same time on the browser console, here are my requests :

CREATE (newNode1:NEW_NODE)
CREATE (newNode2:NEW_NODE)
MATCH (n1:LABEL_1 {id: "node1"}) CREATE (newNode1)-[:LINKED_TO]->(n1)
MATCH (n2:LABEL_2 {id: "node2"}) CREATE (newNode2)-[:LINKED_TO]->(n2)

When I execute them one by one there is no problem, but when I execute them at the same time, I get the following error : WITH is required between CREATE and MATCH

Is there any way to correct this?

like image 837
jimmy Avatar asked Jan 23 '14 01:01

jimmy


People also ask

WHAT IS WITH clause in Cypher?

The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

How do you create a relationship between two nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

What is optional match in Neo4j?

An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.

Why would you use Neo4j match?

The MATCH clause allows you to specify the patterns Neo4j will search for in the database. This is the primary way of getting data into the current set of bindings. It is worth reading up more on the specification of the patterns themselves in Patterns.


1 Answers

Add a couple of WITHs?

CREATE (newNode1:NEW_NODE)
CREATE (newNode2:NEW_NODE)
WITH newNode1, newNode2
MATCH (n1:LABEL_1 {id: "node1"}) 
CREATE (newNode1)-[:LINKED_TO]->(n1)
WITH newNode1, newNode2
MATCH (n2:LABEL_2 {id: "node2"}) 
CREATE (newNode2)-[:LINKED_TO]->(n2)

Alternatively, you could do it in a different order and avoid the WITHs, the difference being that it won't create anything if n1/n2 don't MATCH.

MATCH (n1:LABEL_1 { id: "node1" }) 
MATCH (n2:LABEL_2 { id: "node2" }) 
CREATE (newNode1:NEW_NODE)-[:LINKED_TO]->(n1) 
CREATE (newNode2:NEW_NODE)-[:LINKED_TO]->(n2)
like image 53
Eve Freeman Avatar answered Sep 17 '22 18:09

Eve Freeman