Search code examples
neo4jneo4j-apoc

Neo4j: bad result using the WITH statement


To import a json into Neo4J, I use these two requests:

Request 1:

CALL apoc.load.json("file:///files.json") yield value
unwind value.nodes as node
merge (file:File {id:node.path}) ON CREATE SET file.name = node.name

Request 2:

CALL apoc.load.json("file:///files.json") yield value
unwind value.edges as edge
MATCH
    (source:File {id: edge.sourceId}),
    (target:File {id: edge.targetId})
CREATE (source)-[:`R`]->(target)

The result is the correct one (254 nodes and 3578 relationships).

Now I will like to merge these two queries into one, so I am using the WITH statement as follows:

CALL apoc.load.json("file:///files.json") yield value
unwind value.nodes as node
merge (file:File {id:node.path}) ON CREATE SET file.name = node.name
WITH value
unwind value.edges as edge
MATCH
    (source:File {id: edge.sourceId}),
    (target:File {id: edge.targetId})
CREATE (source)-[:`R`]->(target)

I get the right number of nodes but not the right number of edges (254 nodes and 908812 relationships). Any idea what's going on here?


Solution

  • I didn't understand the problem, but found a solution by replacing the MATCH / CREATE part of the relationship.

    Valid request:

    CALL apoc.load.json("file:///files.json") yield value
    unwind value.nodes as node
    merge (file:File {id:node.path}) ON CREATE SET file.name = node.name
    WITH value
    unwind value.edges as edge
    MATCH (source:File {id: edge.sourceId}), (target:File {id: edge.targetId})
    MERGE (source)-[:`R`]->(target)