【问题标题】:Neo4J Cypher: Why is assigning nodes to variables required in the shortestPath functionNeo4J Cypher:为什么将节点分配给 shortestPath 函数所需的变量
【发布时间】:2024-04-27 16:45:01
【问题描述】:

当我尝试在 Person 节点和 Movie 节点之间执行 shortestPath() 函数时,例如 -

MATCH p=shortestPath((:Person)-[*1..4]->(:Movie))
RETURN length(p)

这失败并出现错误 -

Neo.ClientError.Statement.SyntaxError
shortestPath(...) requires named nodes (line 1, column 9 (offset: 8))
"MATCH p=shortestPath((:Person)-[*1..4]->(:Movie))"
         ^

要解决这个问题,我必须将节点标签分配给变量,就像这样 -

MATCH pt=shortestPath((p:Person)-[*1..4]->(m:Movie))
RETURN length(pt)

这会产生正确的回报 -

length(pt)
1
1
1
1
1
1
1
1
2
2
3

等等。

为什么需要这样做?我没有在我的RETURN 子句中的任何地方使用变量pm

我在shortestPath() 文档中找不到解释。

【问题讨论】:

    标签: neo4j cypher graph-databases shortest-path


    【解决方案1】:

    我在周一的公开密码会议期间向团队提出了您的问题,这就是答案:

    不再需要这些变量。可能有 过去,由于遗留原因,支票仍然存在。这是一个公平的 功能请求。

    干杯

    【讨论】:

    • 但是我用最新版本的 Neo4J (3.3.0)) 执行了这个查询,它仍然抛出这个错误。事实上,我使用的是“Neo4J Desktop”版本,它只出现了几个几周前,所以它是最新的。
    • 你说得对,你仍然必须为最短路径定义开始和结束节点(即使在 3.3 上),但就像你观察到的那样,没有必要拥有它们。这是一个遗留问题......
    最近更新 更多