我尝试获得节点之间的最短路径,计算途中的某些节点和 return 目标以及起点
I try to get the shortest way between nodes, count certain nodes on the way and return the targets along with the start
我有一个密码方面的问题,我自己无法解决。我想要的好像很复杂:
我已经通过 ID 指定了一个节点。然后我想要到相同类型的其他节点的最短路线,但在某些条件下。然后我想计算途中的某些节点。最后,我想 return 起始节点和目标。
到目前为止,我的查询如下所示:
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
RETURN nodes(path)
实际结果是这样的:
enter image description here
结果必须如下所示:
locations: [
{
name: abc,
travels: 0
},
{ name: def,
travels: 1
},
and so far
]
希望有人能帮助我:/
此致,卡维斯
编辑:
结果:
MATCH path=(location:Solarsystem)-[:CanTravel*1..6]-(property:JumpPoint)<-[:CanTravel]-(destination:Solarsystem)
WHERE property.size IN ['large'] AND location.name = 'Ellis'
WITH location, destination, min(size([node in nodes(path) where
node:JumpPoint])) as travels
WITH location, collect({name:destination, travels:travels}) as locations,
Collect(destination) + location as nodes
WITH locations + {name:location, travels:0} as locations, nodes
UNWIND nodes as n
RETURN n, locations
编辑 2:
我还有一件事:我发现以下结果与此查询一起出现令人困惑。再看看我附上的图片:
MATCH path=(location:Solarsystem)-[:CanTravel*1..6]-(jumpPoint:JumpPoint)<-[:CanTravel]-(destination:Solarsystem)
WHERE jumpPoint.size IN ['small'] AND location.name = 'Ellis'
RETURN path
https://www.instpic.de/g2yycmgQZOGKmEsIOEeI.PNG
我的预期结果是:没有结果,因为我只过滤了一个小尺寸的路径,并且开始'Ellis'没有连接任何尺寸小的门...
您应该能够通过对路径节点进行简单过滤来获取路径中的 :Gate 节点数:
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
RETURN location.name as start, collect({name:destination.name, travels:size([node in nodes(path) where node:Gate])}) as locations
编辑
好的,所以让我们确保我们只有到每个目的地的最短路径,然后将当前位置也添加到集合中,travels:0
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
WITH location as start, destination, min(size([node in nodes(path) where node:Gate])) as travels
WITH start, collect({name:destination.name, travels:travels}) as locations
WITH locations + {name:start.name, travels:0} as locations
RETURN locations
编辑 2
关于您的跳转点查询,您得到该结果的原因是因为您没有过滤路径中的所有 :JumpPoints,您只过滤了目的地之前的最后一个 :JumpPoint。如果你想过滤所有 :JumpPoints,你需要明确地这样做,比如:
WHERE all(jump in [node in nodes(path) where node:JumpPoint] WHERE jump.size in ['small']) and ...
我有一个密码方面的问题,我自己无法解决。我想要的好像很复杂:
我已经通过 ID 指定了一个节点。然后我想要到相同类型的其他节点的最短路线,但在某些条件下。然后我想计算途中的某些节点。最后,我想 return 起始节点和目标。
到目前为止,我的查询如下所示:
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
RETURN nodes(path)
实际结果是这样的: enter image description here
结果必须如下所示:
locations: [
{
name: abc,
travels: 0
},
{ name: def,
travels: 1
},
and so far
]
希望有人能帮助我:/
此致,卡维斯
编辑:
结果:
MATCH path=(location:Solarsystem)-[:CanTravel*1..6]-(property:JumpPoint)<-[:CanTravel]-(destination:Solarsystem)
WHERE property.size IN ['large'] AND location.name = 'Ellis'
WITH location, destination, min(size([node in nodes(path) where
node:JumpPoint])) as travels
WITH location, collect({name:destination, travels:travels}) as locations,
Collect(destination) + location as nodes
WITH locations + {name:location, travels:0} as locations, nodes
UNWIND nodes as n
RETURN n, locations
编辑 2:
我还有一件事:我发现以下结果与此查询一起出现令人困惑。再看看我附上的图片:
MATCH path=(location:Solarsystem)-[:CanTravel*1..6]-(jumpPoint:JumpPoint)<-[:CanTravel]-(destination:Solarsystem)
WHERE jumpPoint.size IN ['small'] AND location.name = 'Ellis'
RETURN path
https://www.instpic.de/g2yycmgQZOGKmEsIOEeI.PNG
我的预期结果是:没有结果,因为我只过滤了一个小尺寸的路径,并且开始'Ellis'没有连接任何尺寸小的门...
您应该能够通过对路径节点进行简单过滤来获取路径中的 :Gate 节点数:
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
RETURN location.name as start, collect({name:destination.name, travels:size([node in nodes(path) where node:Gate])}) as locations
编辑
好的,所以让我们确保我们只有到每个目的地的最短路径,然后将当前位置也添加到集合中,travels:0
MATCH path=(location:Location)-[:CanTravel*1..4]-(property:Gate)<-[:CanTravel]-(destination:Location)
WHERE property.size IN ['medium', 'large'] AND location.name = 'Ellis'
WITH location as start, destination, min(size([node in nodes(path) where node:Gate])) as travels
WITH start, collect({name:destination.name, travels:travels}) as locations
WITH locations + {name:start.name, travels:0} as locations
RETURN locations
编辑 2
关于您的跳转点查询,您得到该结果的原因是因为您没有过滤路径中的所有 :JumpPoints,您只过滤了目的地之前的最后一个 :JumpPoint。如果你想过滤所有 :JumpPoints,你需要明确地这样做,比如:
WHERE all(jump in [node in nodes(path) where node:JumpPoint] WHERE jump.size in ['small']) and ...