2016-07-25 97 views
1

你能幫我建立密碼查詢嗎?我有以下圖形數據庫結構:neo4j Cypher分層樹構建響應JSON

(parent:Category)-[:subcategory]->(child:Category) 

有了這個圖數據我有深層次的樹狀層次結構。

我發現下面的代碼上Stackoverfllow.com,改變了我的數據:

MATCH (root:Category)-[:subcategory]->(parent:Category)-[:subcategory]->(child:Category) 
WITH root, {category: parent, children: collect(child)} AS parent_with_children 
WHERE NOT(()-[:subcategory]->(root)) 
RETURN {category: root, children: collect(parent_with_children)} 

但他只建了3個層次樹的深度反應。我需要更大。我試圖建立像這個例子的JSON響應:

[ 
    category: { 
     name: "PC" 
     children: { 
     category: { 
      name: "Parts" 
      children: { 
      category: { 
       name: "CPU" 
       ... 
      } 
      } 
     }, 
     category: { 
      name: "Accessories" 
      ... 
     } 
     } 
    }, 
    category: { 
     name: "Laptop" 
     ... 
    } 
    ] 

Cypher可以進行遞歸調用?我認爲這會更好。

謝謝。

P.S.我知道有類似的問題,但他們沒有幫助我。

+0

是的,Cypher並沒有真的做遞歸。當你返回一棵數據樹時,你最好的選擇是使用cybersam在下面建議的內容,或者將節點/關係作爲表格返回,並將它們構建在內存中。 –

回答

4

Cypher不適合在葉子處於任意深度時傾出樹形結構中的圖形數據。

但是,使用neo4j 3.x,如果您能夠在服務器上安裝APOC plugin並使用apoc.convert.toTree過程,則可以接近您的要求。

首先,讓我們來創建一些示例數據:

CREATE 
    (c1:Category {name: 'PC'}), 
    (c1)-[:subcategory]->(c2:Category {name: 'Parts'}), 
     (c2)-[:subcategory]->(c3:Category {name: 'CPU'}), 
     (c3)-[:subcategory]->(c4:Category {name: 'CacheRAM'}), 
    (c1)-[:subcategory]->(c5:Category {name: 'Accessories'}), 
     (c5)-[:subcategory]->(c6:Category {name: 'Mouse'}), 
     (c5)-[:subcategory]->(c7:Category {name: 'Keyboard'}), 
    (c10:Category {name: 'Laptop'}), 
    (c10)-[:subcategory]->(c20:Category {name: 'Parts'}), 
     (c20)-[:subcategory]->(c30:Category {name: 'CPU'}), 
    (c10)-[:subcategory]->(c40:Category {name: 'Accessories'}), 
     (c40)-[:subcategory]->(c50:Category {name: 'Stylus'}); 

然後用此查詢:

MATCH p=(n:Category)-[:subcategory*]->(m) 
WHERE NOT()-[:subcategory]->(n) 
WITH COLLECT(p) AS ps 
CALL apoc.convert.toTree(ps) yield value 
RETURN value; 

...你將得到N個結果行,其中N是根Category節點的數量。這裏是一個示例結果的片段:

{ 
    ... 
     "row": [ 
     { 
      "_id": 150, 
      "_type": "Category", 
      "name": "PC", 
      "subcategory": [ 
      { 
       "_id": 154, 
       "_type": "Category", 
       "name": "Accessories", 
       "subcategory": [ 
       { 
        "_id": 156, 
        "_type": "Category", 
        "name": "Keyboard" 
       }, 
       { 
        "_id": 155, 
        "_type": "Category", 
        "name": "Mouse" 
       } 
       ] 
      }, 
      { 
       "_id": 151, 
       "_type": "Category", 
       "name": "Parts", 
       "subcategory": [ 
       { 
        "_id": 152, 
        "_type": "Category", 
        "name": "CPU", 
        "subcategory": [ 
        { 
         "_id": 153, 
         "_type": "Category", 
         "name": "CacheRAM" 
        } 
        ] 
       } 
       ] 
      } 
      ] 
     } 
     ], 
    ... 
     "row": [ 
     { 
      "_id": 157, 
      "_type": "Category", 
      "name": "Laptop", 
      "subcategory": [ 
      { 
       "_id": 158, 
       "_type": "Category", 
       "name": "Parts", 
       "subcategory": [ 
       { 
        "_id": 159, 
        "_type": "Category", 
        "name": "CPU" 
       } 
       ] 
      }, 
      { 
       "_id": 160, 
       "_type": "Category", 
       "name": "Accessories", 
       "subcategory": [ 
       { 
        "_id": 161, 
        "_type": "Category", 
        "name": "Stylus" 
       } 
       ] 
      } 
      ] 
     } 
     ], 
    ... 
}