2016-05-29 3 views
0

enter image description hereのNeo4j CYPHER深い最初の検索

以下のように私はツリー状のグラフを持っているが今の私は、ルートノードRから始めるとにからすべてのパスを見つけたいとしましょう最も近いタイプBノード。グラフの例では、結果は

path-1: 1,2 
path-2: 1,3,6,10,13 
path-3: 1,3,7,10,13 

になるはずです。

答えて

1

ノードタイプをラベル(:A)および(:B)のままにしておくと、ノード間の関係は「接続」タイプになります。

// Find all paths from Root to all B-nodes 
MATCH (A:A {name:1}), p = (A)-[:connect*]->(B:B) 

    // Get all node labels for each path 
    WITH A, p, extract(n in nodes(p) | labels(n)) as pathLabels 

    // We find the number of occurrences of B-node in each path 
    WITH A, p, reduce(bCount = 0, Labels in pathLabels | 
        CASE WHEN 'B' IN Labels THEN 1 ELSE 0 END + bCount 
      ) as bCount 

    // Return only the path in which the B-node is in the end of the path 
    WHERE bCount = 1 
RETURN p 

例のデータのクエリ:(ないA-typeノードを検索)

MERGE (A1:A {name:1})-[:connect]-(B2:B {name:2}) MERGE (A1)-[:connect]-(A3:A {name:3}) MERGE (B2)-[:connect]-(A4:A {name:4}) MERGE (B2)-[:connect]-(A5:A {name:5}) MERGE (A4)-[:connect]-(B8:B {name:8}) MERGE (B8)-[:connect]-(A11:A {name:11}) MERGE (B8)-[:connect]-(A12:A {name:12}) MERGE (A5)-[:connect]-(A9:A {name:9}) MERGE (A3)-[:connect]-(A6:A {name:6}) MERGE (A3)-[:connect]-(A7:A {name:7}) MERGE (A6)-[:connect]-(A10:A {name:10}) MERGE (A7)-[:connect]-(A10) MERGE (A10)-[:connect]-(B13:B {name:13}) RETURN * 

更新:それは作品

// Find all paths from Root to all not A-nodes 
MATCH (A:A {name:1}), p = (A)-[:connect*]->(B) WHERE NOT 'A' IN labels(B) 

    // Get all node labels for each path 
    WITH A, p, extract(n in nodes(p) | labels(n)) as pathLabels 

    // We find the number of occurrences of A-node in each path 
    WITH A, p, reduce(aCount = 0, Labels in pathLabels | 
        CASE WHEN 'A' IN Labels THEN 1 ELSE 0 END + aCount 
      ) as aCount 

    // Return only the path in which the count of A-node 
    // is 1 less the total number of nodes in the path. 
    WHERE aCount = length(p) 
RETURN p 
+0

。ただ一つの簡単な質問です。「タイプBノード」の検索から「タイプしないノード」を検索するには、どのように変更できますか? –

+0

@StevenLuo更新を参照してください。 –

+0

ありがとうございます! –

関連する問題