在变长查询中包含路径长度

时间:2016-08-12 23:55:59

标签: neo4j cypher

我有一个cypher查询,它当前返回具有可变长度路径的给定节点的所有关系:

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

我希望将路径长度添加为返回的一部分:

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)

如何包含路径长度以及所有关系?

1 个答案:

答案 0 :(得分:2)

WITH子句删除所有先前定义的标识符(如path),除非它们在子句中指定。您的第一个WITH子句未指定path(或仅指定路径的长度),因此它已被删除。

这样的事情对你有用:

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;