This is the graph I am working on and I set different colors to nodes based on thier labels.
What I am trying to do is to match the path which is consisted of nodes with same labels and merge them into one node for better visualization.
2)
MATCH (a:Stop)-[:HUWON]->(b:Stop)
WITH [a,b] as nodes
CALL apoc.refactor.mergeNodes(nodes,{properties:"combine", mergeRels:TRUE, produceSelfRel:FALSE})
YIELD node
RETURN count(*);
I wanted to merge nodes by matching specific paths(consisted of only ( :Stop) and [ :HUWON]) and enlist them but it show this certain errors.
I am not sure how this Apoc works but I can't just use WITH Head(collect([a1,a2])) as nodes like an example from documentation because
then it will only merge the first path in the list due to head().
That is, is a Cypher behaviour that cannot be fixed with apoc itself.
So, a workaround is putting WITH * LIMIT 1 after the MATCH clause.
Of course, will be merged only 1 result at once.
Or even better, you could put the query into a apoc.periodic.repeat,
so that you can execute the query every second and, instead of a match between 2 nodes, execute a match between multiple nodes (a:Stop)-[:HUWON*]->(b:Stop) ad pick the longest path, to execute fewer executions. That is:
call apoc.periodic.repeat("merge", 'MATCH p=(a:Stop)-[:HUWON*]->(b:Stop)
WITH p, length(p) AS length ORDER BY length DESC LIMIT 1 CALL apoc.refactor.mergeNodes(nodes(p),{properties:"combine", mergeRels:TRUE, produceSelfRel:FALSE})
YIELD node RETURN count(*)', 1)
Anyway, with big databases, the `ORDER BY length` could be heavy, so you could do only
`MATCH p=(a:Stop)-[:HUWON*]->(b:Stop) WITH p LIMIT 1` after `CALL mergeNodes`.