Neo4j Cypher 查找探索排序关系的所有路径 [英] Neo4j Cypher find all paths exploring sorted relationships

查看:118
本文介绍了Neo4j Cypher 查找探索排序关系的所有路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找一种方法来查找两个节点之间的所有路径(最大长度),同时通过对将要探索的关系(通过它们的一个属性)进行排序来控制 Neo4j 的路径探索.

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).

所以要清楚,假设我想在两个节点之间找到 K 个最佳路径,直到最大长度 M.查询将如下所示:

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:

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

到目前为止一切顺利.但是可以说路径的关系有一个称为优先级"的属性.我想要的是写一个查询,告诉 Neo4j 在路径探索的每一步中应该首先探索哪些关系.

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.

我知道当我使用 java 库和嵌入式数据库时这是可能的(通过实现 PathExpander 接口并将其作为 Java 中的 GraphAlgoFactory.allSimplePaths() 函数的输入).但是现在我正在尝试使用 Bolt 或 REST api 在服务器模式数据库访问中找到一种方法.

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). But now I'm trying to find a way doing this in a server mode database access using Bolt or REST api.

有没有办法在服务器模式下做到这一点?或者可能在服务器模式下访问图形时使用 Java 库函数?

Is there any way to do this in the server mode? Or maybe using Java libraries functions while accessing the graph in server mode?

推荐答案

  1. 使用标签和索引来查找您的两个起始节点
  2. 也许可以考虑 allShortestPaths 以使其更快

试试这个:

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;

这篇关于Neo4j Cypher 查找探索排序关系的所有路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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