Cypher:从查询返回节点和边缘?

时间:2017-02-24 12:20:57

标签: neo4j cypher

我有一个初学者Cypher问题。我有这样的查询:

MATCH (rebecca:Person {name:"Rebecca"})-[1..2]->(companies:Company)
RETURN companies

这将返回Rebecca两跳内的所有公司,但它只返回最终的公司节点。如何查看边缘和中间节点?

1 个答案:

答案 0 :(得分:4)

您只能返回别名的实体。

MATCH (rebecca:Person {name:"Rebecca"})-[1..2]->(companies:Company)
RETURN companies

为您的人际关系添加r别名:

MATCH (rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies, r

对于返回中间节点,您可以将其设为路径:

MATCH p=(rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies, relationships(p), nodes(p)

您还可以将公司以及(relationship,startnode,endnode)地图一起退回:

MATCH (rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies,
       extract(x IN r | {rel: x, start: startNode(x), end: endNode(x)})