0

私はneo4jにグラフを持っています。特定のノードNについて、NからPステップ以内のパスで到達可能なすべてのノードと、そのノードセット間のすべてのリンクを見つけたいと思います。これは、Cypher または Traversal フレームワークのいずれかで可能になるようです。どちらが優先されますか?埋め込みデータベースを使用して Java からこれを行っています。サブグラフに対してさらにクエリを実行する必要があります。私はいろいろ調べましたが、決定的な答えは見つかりませんでした。

4

1 に答える 1

2

I think cypher is the most concise way of getting your desired data, querying for variable length paths, some collecting and refining:

If n is the internal id of your node N and your P is 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

It might not be the most efficient way, but at least the execution plan explanation in http://console.neo4j.org/ suggests that the y in nodes is considered before the MATCH (x)-[r]-(y).

I couldn't think of a way to avoid matching the relationships twice, therefore the distinct in the return statement.

于 2013-06-05T21:43:07.703 に答える