2015-11-19 91 views
3

用戶, 我試圖接收所有路徑(例如,長度爲< 3),打印節點標題,但也顯示關係類型。Cypher Query:返回可變長度路徑關係的類型

使用:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1) 
RETURN p AS path 

打印路徑,但是該關係作爲我使用表示爲{}

*。2,

`RETURN p,type(r)' 

不工作(類型不匹配:預期的關係,但是收集)

我可以使用類似的解決方法:

MATCH 
    (p0:Page {title:'New Zealand'}), 
    (p1:Page {title:'Kiwi'}), 
    p = (p0)-[r]-(p3)-[r1]-(p1) 
RETURN p0,type(r),p3,type(r1) 

但隨着pathlength的增加,我會執行查詢每個pathlength。

我想:

MATCH 
    (p0:Page {title:'New Zealand'}), 
    (p1:Page {title:'Kiwi'}), 
    p = (p0)-[r*..2]-(p1) FOREACH(r in p | RETURN p,p0,type(r),p1) 

- >預計收藏但路徑

沒有人有暗示?從Neo4j的接口

更多信息JSON輸出(片段):

{ 
    "results": [ 
    { 
     "columns": [ 
     "p", 
     "rels(p)", 
     "nodes(p)" 
     ], 
     "data": [ 
     { 
      "row": [ 
      [ 
       { 
       "title": "New Zealand" 
       }, 
       {}, 
       { 
       "title": "Category:Birds of New Zealand" 
       }, 
       {}, 
       { 
       "title": "Kiwi" 
       } 
      ], 
      [ 
       {}, 
       {} 
      ], 
      [ 
       { 
       "title": "New Zealand" 
       }, 
       { 
       "title": "Category:Birds of New Zealand" 
       }, 
       { 
       "title": "Kiwi" 
       } 
      ] 
      ], 
      "graph": { 
      "nodes": [ 
       { 
       "id": "11120", 
       "labels": [ 
        "Page" 
       ], 
       "properties": { 
        "title": "Kiwi" 
       } 
       }, 
       { 
       "id": "1942858", 
       "labels": [ 
        "Page" 
       ], 
       "properties": { 
        "title": "New Zealand" 
       } 
       }, 
       { 
       "id": "11994493", 
       "labels": [ 
        "Category" 
       ], 
       "properties": { 
        "title": "Category:Birds of New Zealand" 
       } 
       } 
      ], 
      "relationships": [ 
       { 
       "id": "1070940", 
       "type": "To_Category", 
       "startNode": "11120", 
       "endNode": "11994493", 
       "properties": {} 
       }, 

回答

3

您可以簡單地使用extract來提取路徑內的關係類型。

基於一個簡單的電影圖形上http://console.neo4j.org

MATCH p=(:Crew { name:"Neo" })-[r*3]-() 
RETURN p, extract(x IN rels(p)| type(x)) AS types 

您的查詢將被:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1) 
RETURN p, extract (rel in rels(p) | type(rel)) as types 

這將返回一個類型的集合:

[愛,懂得,知道]

所以你可以有重複。如果您需要刪除重複的,用放鬆和獨特:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1) 
WITH p, extract (rel in rels(p) | type(rel)) as types 
UNWIND types as t 
RETURN p, collect(distinct t) as types 

UPDATE

我是不太滿意的最後一個,所以這裏一個簡單的方法:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1) 
UNWIND rels(p) as rel 
RETURN p, collect(distinct type(rel)) as types 
+0

非常感謝! – AfterEight

+0

我編輯了一個更容易的重複數據刪除 –

1

是否使用rels功能幫你?

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1) 
RETURN p, rels(p), nodes(p) 
+0

感謝您的快速回復。不幸的是,它具有相同的效果: rels(p)[(empty),(empty)];列:[p,rels(p),nodes(p)]當前行0:[{{title = New Zealand},{},{title = Category:New Zealand Birds of New Zealand},{},{title = Kiwi} ],[{},{}],[{title = New Zealand},{title = Category:New Zealand Birds of New Zealand},{title = Kiwi}]]。也許我在構建我的圖時做錯了什麼(?) – AfterEight

相關問題