路径之间特定节点标签的 Neo4j 最短路径 [英] Neo4j Shortest Path for specific node labels in-between the path

查看:79
本文介绍了路径之间特定节点标签的 Neo4j 最短路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找解决以下问题的提示:

I'm looking for a hint to solve the following issue:

给定一个 Neo4j DB,其中应用了两种节点类型作为标签

Given a Neo4j DB with two node types applied as labels

:地址和:里程碑(每个节点一个)

:Address and :Milestone (one for each node)

不同节点之间存在定向的 :LEADS_TO 关系.

Between the different nodes there are directed :LEADS_TO relationships.

鉴于以下模式(我跳过 [ ] 的关系):

Given the following pattern (I skipped the [ ] for the relations):

模式 1:

(a:Address)->(:Milestone)->(:Milestone)<-(:Milestone)<-(:Milestone)<-(b:Address)

模式 2:

(a:Address)->(:Milestone)->(c:Address)<-(:Milestone)<-(b:Address)

我正在寻找在 shortestPath 分析中仅包含相同类型的中间"节点的查询.在我的例子中,它应该只包含标签为 :Milestone 的节点,并跳过节点标记为 :Address 的较短路径.

I'm looking for a query that only includes "in-between" nodes of the same type within the shortestPath analysis. In my case it should only include nodes with the label :Milestone and skips shorter paths that have a node labeled :Address somewhere in-between.

换句话说:查询应该匹配模式 1 而不是模式 2,即使它更短.

In other words: the query should match Pattern 1 and not Pattern 2 even if it is shorter.

我发现以下解决方案很接近但在我的情况下失败了,因为它也涵盖了开始和结束节点标签,因此根本不选择任何行:

I found the following solution which comes close but fails in my case because it covers the start and end node label as well and due to this selects no rows at all:

MATCH p=shortestPath( (a:Address)-[:LEADS_TO*..10]-(b:Address) ) 
WHERE a.name = 'XYZ'
AND b.name = 'ABC'
AND ALL(x IN nodes(p) WHERE (x:MILESTONE))
RETURN p

我最初的想法是创建第一跳:

My initial idea was to create a first hop:

对于起始节点

MATCH (a:Address)-[:LEADS_TO]->(aa:Milestone)

和结束节点类似,并从那里开始查询.

and similar for the end node and start the query from there.

但这是有问题的,因为每个 :Address 节点都可以有多个 :LEADS_TO 关系.查询定义的开始和结束节点的最短路径,这种方法将创建 n 个开始节点和 m 个结束节点,仅用于一个可以使用不同开始和结束节点执行的查询.

But this is problematic because every :Address node can have multiple :LEADS_TO relationships. Querying the shortest path for a defined start and end node this approach would create n start nodes and m end nodes just for one query that could be executed with a distinct start and end node.

有谁知道如何从 Cypher 中的 WHERE 子句中排除开始和结束节点?

Does anyone have an idea how to exclude the start and end node from the WHERE clause within Cypher?

非常感谢任何提示.

推荐答案

试试这个,你可以用切片操作符使用集合的子集

Try this, you can use a subset of a collection with a slice operator

MATCH p=shortestPath( (a:Address)-[:LEADS_TO*..10]-(b:Address) ) 
WHERE a.name = 'XYZ'
AND b.name = 'ABC'
AND ALL(x IN nodes(p)[1..-1] WHERE (x:MILESTONE))
RETURN p

这篇关于路径之间特定节点标签的 Neo4j 最短路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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