woodpecker
woodpecker

Reputation: 31

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]

Upvotes: 2

Views: 1377

Answers (1)

stdob--
stdob--

Reputation: 29172

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])

Upvotes: 3

Related Questions