2013-06-05 54 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