无法识别 Neo4J 变量别名 [英] Neo4J variable alias not recognized

查看:46
本文介绍了无法识别 Neo4J 变量别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个具有各种关系的 Person 节点.每个节点都有一个纬度和经度.

I have three Person nodes with various relationships. Each node has a latitude and longitude.

首先,我找到节点对的组合:

First, I find the combinations of pairs of nodes:

MATCH (p1: Person)-[]->(p2: Person)
RETURN p1.name, p2.name

我的输出是正确的:

接下来,我尝试找到节点对之间的距离:

Next, I attempt to find the distances between the pairs of nodes:

MATCH (p1:Person)-[]->(p2:Person)
WITH point({longitude: p1.longitude, latitude: p1.latitude}) AS p1Point,
point({longitude: p2.longitude, latitude: p2.latitude}) AS p2Point
RETURN (distance(p1Point, p2Point)) AS distance

我的输出在这里:

最后,我想把它们放在一起.我想列出每个节点对的名称以及它们之间的关联距离:

Finally, I want to put it all together. I want to list the names of each node pair and the associated distance between them:

MATCH (p1:Person)-[]->(p2:Person)
WITH point({longitude: p1.longitude, latitude: p1.latitude}) AS p1Point,
point({longitude: p2.longitude, latitude: p2.latitude}) AS p2Point
RETURN p1.name, p2.name, (distance(p1Point, p2Point)) AS distance

我收到一个错误,指出 p1 未定义.

I get an error that p1 is not defined.

这里有什么问题?这里描述了类似的语法:https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/all-pairs-shortest-path/

What is the problem here? There is similar syntax described here: https://neo4j.com/docs/graph-algorithms/current/labs-algorithms/all-pairs-shortest-path/

推荐答案

WITH 子句解除所有变量的绑定,除了它继承的变量.

The WITH clause unbinds all variables except for the ones it carries forward.

这应该有效:

MATCH (p1:Person)-->(p2:Person)
WITH p1, p2,
  point({longitude: p1.longitude, latitude: p1.latitude}) AS p1Point,
  point({longitude: p2.longitude, latitude: p2.latitude}) AS p2Point
RETURN p1.name, p2.name, distance(p1Point, p2Point) AS distance

这也应该有效(因为 WITH 并不是真正需要的):

This should also work (since WITH is not really needed):

MATCH (p1:Person)-->(p2:Person)
RETURN p1.name, p2.name,
  distance(
    point({longitude: p1.longitude, latitude: p1.latitude}),
    point({longitude: p2.longitude, latitude: p2.latitude})) AS distance

[更新]

顺便说一下,对于像 RELATED_TO 这样的双向关系,你应该只使用一个 无向关系 而不是指向相反方向的两个有向关系.但是请注意,CREATE 子句仅支持创建定向 关系,因此只需选择任意方向——哪个方向无关紧要.稍后,当您进行搜索时,只需使用无向搜索(例如 MATCH (p1:Person)--(p2:Person) ...).此外,您应该考虑是否应该使用 MERGE 代替,因为它确实允许无向关系模式.

By the way, with an always-bidirectional relationship like RELATED_TO, you should just use a single undirected relationship instead of two directed relationships pointing in opposite directions. Note, though, that the CREATE clause only supports creating directed relationships, so just pick any arbitrary direction -- it does not matter which. Later, when you do a search, just use an undirected search (like MATCH (p1:Person)--(p2:Person) ...). Also, you should look into whether you should use MERGE instead, since it does allow an undirected relationship pattern.

这篇关于无法识别 Neo4J 变量别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆