I'm Stuck when trying to deal with loops in one use case as operator ID repeats and below are columns I'm working with:
Operator ID, Activity Done
14, enter
12, open
13, change
14, update
15, close
16, view
14, update
11, Note
I used below query :
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" as line
CREATE (n:Operator)
SET n.linenumber = linenumber(),
n.id = line.Operator ID,
n.activity = line.Activity Done;
MATCH (n:Operator)
MATCH (n1:Operator) WHERE n1.linenumber = n.linenumber-1
CREATE (n1)-[:HAS_RELATION]->(n)
REMOVE n.linenumber,n1.linenumber
RETURN n1,n;
But in output, due to linenumber() function I think it restricts looping back to the different node(Correct me If I'm wrong), attached the output in neo4j below using above query:
I've tried it, but not able to understand of how to connect the Operator ID row-wise without using below statement : MATCH (n1:Operator) WHERE n1.linenumber = n.linenumber-1
If i'm using it, then I'm not able to implement loops and visualize the process flow. And How could I implement this on larger dataset ?
It would be helpful if you provide some solution for it, It would really help my understanding.
This query will create nodes and relationships, you will need APOC installed on the database:
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.create.relationship(n, activities[i], {}, m)
YIELD rel
RETURN rel
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.Path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.create.relationship(n, activities[i], {path: path[i]}, m)
YIELD rel
RETURN rel
Did you delete everything before to use the query or there were still nodes and relationships in the database? If there are nodes and relations already created, we have to use apoc.merge.relationship.
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
RETURN rel
Hi @cobra, Thanks for inputs given previously in this topic which was really helpful.
Further working on this use case and I'm stuck. Could provide some input on it?
I have a question, It is with regards to the query you suggested below:
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
RETURN rel
Question that I have is:
Can we add add a common label for all relationships arcs, Like how for all 6 nodes the label is "Operator" . And put the "Activity Done" column values as the properties of Arcs along with "path" column values ?
LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
MERGE (n)-[r:ACTIVITY_DONE]->(m)
SET r.activity = activities[i], r.path = path[i]