0
votes

I have a cypher query that currently returns all of the relationships from a given node with a variable length path:

MATCH (n)
WHERE n.name = ({name})
OPTIONAL MATCH path=n-[*..2]-(c)
WHERE n <> c
WITH rels(path) AS rels
UNWIND rels AS rel
WITH DISTINCT rel
RETURN startnode(rel).name as source, endnode(rel).name as target,  rel.average_alignment

I'm looking to add the path length as part of the return:

MATCH (n)
WHERE n.name = ({name})
OPTIONAL MATCH path=n-[*..2]-(c)
WHERE n <> c
WITH rels(path) AS rels
UNWIND rels AS rel
WITH DISTINCT rel
RETURN startnode(rel).name as source, endnode(rel).name as target,  rel.average_alignment, length(path)

How does one include the path length along with all of the relationships?

1

1 Answers

2
votes

The WITH clause drops all previously defined identifiers (like path) unless they specified in the clause. Your first WITH clause did not specify path (or just the length of the path), so it was dropped.

Something like this should work for you:

MATCH (n) WHERE n.name = {name}
OPTIONAL MATCH path = (n)-[*..2]-(c)
WHERE n <> c
WITH LENGTH(path) AS lth, RELS(path) AS rels
UNWIND rels AS rel
WITH DISTINCT lth, rel
RETURN STARTNODE(rel).name as source, ENDNODE(rel).name as target,  rel.average_alignment, lth;