2017-05-14 112 views
0

我目前在學習Neo4J和Cypher的早期,所以將不勝感激任何意見,我可以得到!尋找直接的「朋友」和「羣」的朋友

我正在尋找的是獲取「物品」列表。這些項目來自「列表」,直接「擁有」由我,或者「擁有」一個「組」我是一個「成員」:

  • 用戶可以有解釋
  • 用戶可以有組
  • 用戶組的列表
  • 列表可以有項目

到目前爲止,我有(對於來自列表我自己直接獲取項目):

MATCH (owner:User {identifier: "b3b57bc7-f7d2-4915-89b7-b111510d66b0"})-[OWNS]->(list:List)-[HAS]->(item:Item) 
RETURN {identifier: item.identifier, title: item.title, list: list.identifier} 

我會如何恭維這與我是一個成員的組列表?

我希望能找回(爲了滿足GraphQL)的格式如下:

[ 
    { 
    identifier: "item-1-identifier, 
    title: "My Item", 
    list: "list-1-identifier" 
    }, 
    { 
    identifier: "item-2-identifier, 
    title: "My Item on another list", 
    list: "list-2-identifier" 
    } 
] 

謝謝!

回答

3

要將您的列表和組列表合併到一個列中,您可以更改遍歷的關係以匹配任一路徑,也可以使用UNION或COLLECT兩組項目併合並集合,然後UNWIND回到行。

下面是更改遍歷的關係的示例,應該是最簡單的。

MATCH (owner:User {identifier: "b3b57bc7-f7d2-4915-89b7-b111510d66b0"})-[:MEMBER*0..1]->()-[:OWNS]->(list:List)-[:HAS]->(item:Item) 
RETURN {identifier: item.identifier, title: item.title, list: list.identifier} 

下界的0:MEMBER關係意味着它是可選的,這將允許該圖案以匹配到用戶的列表和所述用戶是其成員的組的列表兩者。

+0

感謝您的示例並提供了UNWIND/COLLECT方法的描述 – Rawkode

1

我創建了一個小數據集來重現您的場景。該數據集的腳本可以在here找到。

或者到InverseFalcon solution(以及由InverseFalcon提到的),您可以使用COLLECTUNWIND如下做到這一點:

╒══════════════════════════════╕ 
│"{identifier: unwinded.identif│ 
│ier, title: unwinded.title, li│ 
│st: unwinded.identifier}"  │ 
╞══════════════════════════════╡ 
│{"identifier":"item3","title":│ 
│"Item 3","list":"item3"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item1","title":│ 
│"Item 1","list":"item1"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item2","title":│ 
│"Item 2","list":"item2"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item6","title":│ 
│"Item 6","list":"item6"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item4","title":│ 
│"Item 4","list":"item4"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item5","title":│ 
│"Item 5","list":"item5"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item9","title":│ 
│"Item 9","list":"item9"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item7","title":│ 
│"Item 7","list":"item7"}  │ 
├──────────────────────────────┤ 
│{"identifier":"item8","title":│ 
│"Item 8","list":"item8"}  │ 
└──────────────────────────────┘ 

PROFILE此據:

// First match all items from lists owned by the user 
MATCH (user:User {identifier:"b3b57bc7-f7d2-4915-89b7-b111510d66b0"})-[:OWNS]->(list:List)-[:HAS]->(item:Item) 
// Pass the user and all information about items and list to the next context 
WITH user, COLLECT({identifier: item.identifier, title: item.title, list: list.identifier}) AS userListItems 
// Match all items from lists owned by groups that the user is a member 
MATCH (user)-[:MEMBER]->(group:Group)-[:OWNS]->(list:List)-[:HAS]->(item:Item) 
// Collect the result and add the previous result 
WITH COLLECT({identifier: item.identifier, title: item.title, list: list.identifier}) + userListItems as allItems 
// anwind all 
UNWIND allItems as unwinded 
// return in the desired format 
RETURN {identifier: unwinded.identifier, title: unwinded.title, list: unwinded.identifier} 

基於該數據集的結果查詢命中數據庫62次。非常類似於InverseFalcon提供的非常優雅的解決方案,該解決方案命中數據庫61次。

+0

感謝您將這些放在一起,我將在演示中演示這種語法。再次感謝! – Rawkode