2014-07-14 61 views
2

我試圖在我的Neo4j數據庫中建立一個日期樹,該日期樹將與Nigel Small的py2neo庫中的日曆模塊一起工作。創建一個Neo4j日期樹,將與py2neo一起工作

我在這裏使用了Mark Needham的入門代碼(http://java.dzone.com/articles/neo4j-cypher-creating-time),但是並沒有將所有Year節點連接到py2neo庫所需的主日曆節點。 (這裏的文檔:http://book.py2neo.org/en/latest/calendar/

我修改標記的代碼,試圖創建一個主節點,以這些年的連接是這樣的:

CREATE (x:Calendar {name:'master'}) 
WITH range(2005, 2014) AS years, range(1,12) as months 
FOREACH(year IN years | 
    CREATE (y:Year {year: year}) 
    MERGE (x)-[:YEAR]->(y) 
    FOREACH(month IN months | 
    CREATE (m:Month {month: month}) 
    MERGE (y)-[:MONTH]->(m) 
    FOREACH(day IN (CASE 
         WHEN month IN [1,3,5,7,8,10,12] THEN range(1,31) 
         WHEN month = 2 THEN 
         CASE 
          WHEN year % 4 <> 0 THEN range(1,28) 
          WHEN year % 100 <> 0 THEN range(1,29) 
          WHEN year % 400 <> 0 THEN range(1,29) 
          ELSE range(1,28) 
         END 
         ELSE range(1,30) 
        END) |  
     CREATE (d:Day {day: day}) 
     MERGE (m)-[:DAY]->(d)))) 

發生的事情是,有一個節點(與日曆標籤)已創建,但沒有關聯關係,而有一個節點(沒有標籤)正在創建並附加到每個Year節點。

我知道這可能是一個非常簡單的解決方法,但我是CYPHER的新手,我真的很難搞清楚這一點。

回答

5

你忘了通過運送日曆主「X」 WITH

CREATE (master:Calendar { name:'master' }) 
WITH range(2005,2014) AS years, range(1,12) AS months,master 
FOREACH (year IN years | 
     CREATE (y:Year { year: year }) 
     MERGE (master)-[:YEAR]->(y) 
     FOREACH (month IN months | 
        CREATE (m:Month { month: month }) 
        MERGE (y)-[:MONTH]->(m) 
        FOREACH (day IN (
        CASE 
        WHEN month IN [1,3,5,7,8,10,12] 
        THEN range(1,31) 
        WHEN month = 2 
        THEN 
        CASE 
        WHEN year % 4 <> 0 
        THEN range(1,28) 
        WHEN year % 100 <> 0 
        THEN range(1,29) 
        WHEN year % 400 <> 0 
        THEN range(1,29) 
        ELSE range(1,28) END ELSE range(1,30) END)| 
          CREATE (d:Day { day: day }) 
          MERGE (m)-[:DAY]->(d)))) 
+0

我花了幾分鐘,看看你在2號線的變化;我現在明白了。謝謝! –