2013-06-05 49 views
0

我在neo4j中有一个图,对于给定的节点N,我希望找到所有可以在不超过N步的P步中访问的节点,以及所有节点集。 Cypher或Traversal框架似乎都可以实现;比另一个更受欢迎?我正在用Java和嵌入式数据库做这件事,而且我需要对子图进行进一步的查询。我捅了一下,还没有找到任何确凿的答案。neo4j中的诱导子图

回答

2

我认为暗号是让你的所希望的数据,查询可变长度路径的最简洁的方式,一些收集和精炼:

如果n是您的节点N的内部ID和P为5:

START begin = node(n)    // or e.g. index lookup 
MATCH p = (begin)<-[r*..5]-(end) // match all paths of length up to 5 
WITH distinct nodes(p) as nodes // collect the nodes contained in the paths 
MATCH (x)<-[r]-(y)    // find all relationships between nodes 
WHERE x in nodes and y in nodes // which were found earlier 
RETURN distinct x,r,y    // and deduplicate as you find all pairs twice 

它可能不是最有效的方式,但至少在http://console.neo4j.org/执行计划的解释表明,y in nodesMATCH (x)-[r]-(y)之前考虑。

我想不出一种方法来避免两次匹配关系,因此distinct在return语句中。

+0

谢谢,我会试试!似乎创建一个新的嵌入式数据库并将图形放入其中是在该子图上执行附加操作的最佳方式。 – betseyb