简体   繁体   English

Neo4j Cypher 查找探索排序关系的所有路径

[英]Neo4j Cypher find all paths exploring sorted relationships

I'm struggling for days to find a way for finding all paths (to a maximum length) between two nodes while controlling the path exploration by Neo4j by sorting the relationships that are going to be explored (by one of their properties).我一直在努力寻找一种方法来查找两个节点之间的所有路径(最大长度),同时通过对将要探索的关系(通过它们的一个属性)进行排序来控制 Neo4j 的路径探索。

So to be clear, lets say I want to find K best paths between two nodes until a maximum length M. The query will be like:所以要清楚,假设我想在两个节点之间找到 K 个最佳路径,直到最大长度 M。查询将如下所示:

match (source{name:"source"}), (target{name:"target"}),
p = (source)-[*..M]->(target)
return p order by length(p) limit K;

So far so good.到目前为止一切顺利。 But lets say the relationships of the path have a property called "priority".但是可以说路径的关系有一个称为“优先级”的属性。 What I want is to write a query that tells Neo4j on each step of path exploration which relationships should be explored first.我想要的是编写一个查询,告诉 Neo4j 在路径探索的每个步骤中应该首先探索哪些关系。

I know that can be possible when I use the java libraries and an embedded database (By implementing PathExpander interface and giving it as input to the GraphAlgoFactory.allSimplePaths() function in Java).我知道当我使用 java 库和嵌入式数据库时这是可能的(通过实现 PathExpander 接口并将其作为 Java 中的 GraphAlgoFactory.allSimplePaths() 函数的输入)。 But now I'm trying to find a way doing this in a server mode database access using Bolt or REST api.但是现在我正在尝试使用 Bolt 或 REST api 在服务器模式数据库访问中找到一种方法。

Is there any way to do this in the server mode?有没有办法在服务器模式下做到这一点? Or maybe using Java libraries functions while accessing the graph in server mode?或者可能在服务器模式下访问图形时使用 Java 库函数?

  1. use labels and an index to find your two start-nodes使用标签和索引来找到你的两个起始节点
  2. perhaps consider allShortestPaths to make it faster也许考虑 allShortestPaths 以使其更快

try this:试试这个:

match (source{name:"source"}), (target{name:"target"}),
p = (source)-[rels:*..20]->(target)
return p, reduce(prio=0, r IN rels | prio + r.priority) as priority 
order by priority ASC, length(p) 
limit 100;

I had a very similar problem.我有一个非常相似的问题。 I was trying to find the shortest path from one node to all other nodes.我试图找到从一个节点到所有其他节点的最短路径。 I had written a query similar to the one in the answer above ( https://stackoverflow.com/a/38030536/783836 ) and couldn't get it to perform in any reasonable time.我写了一个类似于上面答案( https://stackoverflow.com/a/38030536/783836 )中的查询,但无法在任何合理的时间内执行。

Asking Can Graph DBs perform well with unspecified end nodes?询问Graph DB 能否在未指定的终端节点上表现良好? pointed me to the solution: the Single Shortest Path algorithm.向我指出了解决方案: Single Shortest Path算法。

In Neo4j you need to install the Graph Data Science Library and make use of this function: gds.alpha.shortestPath.deltaStepping.stream在 Neo4j 中,您需要安装 Graph Data Science Library 并使用此功能: gds.alpha.shortestPath.deltaStepping.stream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM