Search code examples
neo4jcypherextract

Cypher: Extract node and relationship properties from path


I would like to extract from a path properties from nodes and relationships. I can do it separately for nodes and relationships using the following queries.

extract(n IN nodes(path)| n.name)

extract(r IN relationships(path)| r.metric)

Is there a way to extract names and metrics from path elements in a list that looks as following [name1, metric1, name2, metric2, name3]


Solution

  • You can use reduce for combining arrays:

    WITH path,
         extract(n IN nodes(path)| n.name) as names,
         extract(r IN relationships(path)| r.metric) as metrics
    RETURN HEAD(names) + 
           REDUCE(acc = [], i in RANGE(1,size(metrics)) | 
                  acc  + metrics[i-1] + names[i])