cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Create a path of nodes from a list of strings

Luc-Bertin
Node

Hello,
Using Cypher language only, based on a liste of strings e.g [ "A", "B", "C" ], I want to create the following path:

( n1:Test {ps: "A"}) -[:NEXT]-> (n2:Test {ps: "B"}) -[:NEXT]-> (n3:Test {ps:"C"})

The list would be of any length. Nodes and relationships can already exist in the graph, hence reusing them, creating only connecting relationships and / or nodes when required.

1 REPLY 1

giuseppe_villan
Graph Fellow

@Luc-Bertin

You could do something like this:

WITH [ "A", "B", "C" ] as list
UNWIND range(0, size(list) - 1) as index // list from 0 to size-1, so [0,1,2]
MERGE (n:Test {ps: list[index]}) 
WITH n, index, list
WHERE index > 0 // to exclude first item from rel creation
MATCH (m:Test {ps: list[index -1]}) 
MERGE p=(m)-[:NEXT]->(n)
RETURN p