cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher Statement Not giving Desired Result in Neo4j?

Hello All I'm new to Neo4j, I'm trying to learn Neo4j. This example I have chosen is process of resolving issue related to Mobile repair.

Below shown are 3 columns for Operation1&2 (which are done by Operator ID A,B,C..etc) and Time for both operations to complete for particular mobile issue to be resolved
(OPER_1,OPER_2,Time)
(A, A, 6)
(A, A, 😎
(A, B, 3)
(B, C, 4 )
(C, D, 6)
(D, E, 9)

I wanted to create a graph with nodes as Operator & time on relations as shown below:

A---6-->A---8--->A---3--->B---4--->C----6--->D---9--->E

I have used below query:

LOAD CSV WITH HEADERS FROM "file:///C:/xyz.csv" AS line
CREATE (n:Oper1 {,OperId:line.OPER_1})
CREATE (m:Oper2 {OperId:line.OPER_2})
WITH n,m
CREATE (n) -[:TO {TimeUsed: line.Time} ]-> (m)

But it creating something like this:
A--6--->A, A---8--->A,A--3--->B, B--4--->C, C---6--->D, D---9--->E all relations are separately generated.

What should I do?

6 REPLIES 6

it appears your result created 6 pairs where each node points to another node but what you wanted was one long path? is that correct?
If so change the CREATE to a MERGE and retry.
And so with Neo4j 4.2.5 and with a csv defined as

OPER_1,OPER_2,Time
A,A,6
A,A,8
A,B,3
B,C,4
C,D,6
D,E,9

note I removed the leading ( and trailing ) from each line

And when running

LOAD CSV WITH HEADERS FROM "file:///test.csv" AS line
MERGE (n:Oper {OperId:line.OPER_1})
MERGE (m:Oper {OperId:line.OPER_2})
MERGE (n) -[:TO {TimeUsed: line.Time} ]-> (m);

the result is

Thank you @dana.canzano for the idea.

Yes, for me the result created 6 pairs where each node points to another node. But is it possible to create one long path without the loops at A ??

The first two rows of your .csv file has:

OPER_1,OPER_2,Time
A,A,6
A,A,8

With this you cannot avoid the observed behavior.

Okay, Thank you @ameyasoft

avoiding loops?

But your initial statement of

I wanted to create a graph with nodes as Operator & time on relations as shown below:
A---6-->A---8--->A---3--->B---4--->C----6--->D---9--->E

demonstrates a loop of A---6-->A---8--->A ? no?

Yes true @dana.canzano it forms a loop.

In the context of my application, I wanted to depict in graph the process of how mobile traversed for repair. I might have modeled it wrong. Thanks for the inputs, I have corrected it now.