Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

This gist is a Neo4j Cypher query for merging a calendar graph for a specific year. This query has 4 levels of indexes, consisting of year, month, day, hour.

View CalendarDay.cql
1 2 3 4 5 6 7 8 9 10 11 12 13
// Enter the day you would like to create
WITH { day: 18, month: 1, year: 2014 } as dayMap
 
// Merge hours in a day
MERGE (thisDay:Day { day: dayMap.day, month: dayMap.month, year: dayMap.year })
MERGE (firstHour:Hour { day: dayMap.day, month: dayMap.month, year: dayMap.year, hour: 1 })
CREATE (thisDay)-[:FIRST]->(firstHour)
FOREACH (i IN tail(range(1, 24)) |
MERGE (thishour:Hour { day: dayMap.day, month: dayMap.month, year: dayMap.year, hour: i })
MERGE (lasthour:Hour { day: dayMap.day, month: dayMap.month, year: dayMap.year, hour: i - 1 })
CREATE (lasthour)-[:NEXT]->(thishour))
MERGE (lastHour:Hour { day: dayMap.day, month: dayMap.month, year: dayMap.year, hour: 24 })
CREATE (thisDay)-[:LAST]->(lastHour)
View CalendarDay.cql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
// Enter the calendar year you would like to create
MERGE (thisYear:Year { year: 2014 })
 
// This date map enforces the rules for days in a month (leap year not yet implemented)
WITH {
january: range(1, 31),
february: range(1, 28),
march: range(1, 31),
april: range(1, 30),
may: range(1, 31),
june: range(1, 30),
july: range(1, 31),
august: range(1, 31),
september: range(1, 30),
october: range(1, 31),
november: range(1, 30),
december: range(1, 31)
} AS dateMap, thisYear
 
// Merge 12 months of the year using the date map as the calendar rules
MERGE (january:Month { month: 1, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(january)
MERGE (firstDay:Day { day: 1, month: january.month, year: thisYear.year })
CREATE (january)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.january) |
MERGE (thisday:Day { day: i, month: january.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: january.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.january), month: january.month, year: thisYear.year })
CREATE (january)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (february:Month { month: 2, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(february)
MERGE (firstDay:Day { day: 1, month: february.month, year: thisYear.year })
CREATE (february)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.february) |
MERGE (thisday:Day { day: i, month: february.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: february.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.february), month: february.month, year: thisYear.year })
CREATE (february)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (march:Month { month: 3, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(march)
MERGE (firstDay:Day { day: 1, month: march.month, year: thisYear.year })
CREATE (march)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.march) |
MERGE (thisday:Day { day: i, month: march.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: march.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.march), month: march.month, year: thisYear.year })
CREATE (march)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (april:Month { month: 4, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(april)
MERGE (firstDay:Day { day: 1, month: april.month, year: thisYear.year })
CREATE (april)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.april) |
MERGE (thisday:Day { day: i, month: april.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: april.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.april), month: april.month, year: thisYear.year })
CREATE (april)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (may:Month { month: 5, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(may)
MERGE (firstDay:Day { day: 1, month: may.month, year: thisYear.year })
CREATE (may)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.may) |
MERGE (thisday:Day { day: i, month: may.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: may.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.may), month: may.month, year: thisYear.year })
CREATE (may)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (june:Month { month: 6, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(june)
MERGE (firstDay:Day { day: 1, month: june.month, year: thisYear.year })
CREATE (june)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.june) |
MERGE (thisday:Day { day: i, month: june.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: june.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.june), month: june.month, year: thisYear.year })
CREATE (june)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (july:Month { month: 7, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(july)
MERGE (firstDay:Day { day: 1, month: july.month, year: thisYear.year })
CREATE (july)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.july) |
MERGE (thisday:Day { day: i, month: july.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: july.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.july), month: july.month, year: thisYear.year })
CREATE (july)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (august:Month { month: 8, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(august)
MERGE (firstDay:Day { day: 1, month: august.month, year: thisYear.year })
CREATE (august)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.august) |
MERGE (thisday:Day { day: i, month: august.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: august.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.august), month: august.month, year: thisYear.year })
CREATE (august)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (september:Month { month: 9, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(september)
MERGE (firstDay:Day { day: 1, month: september.month, year: thisYear.year })
CREATE (september)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.september) |
MERGE (thisday:Day { day: i, month: september.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: september.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.september), month: september.month, year: thisYear.year })
CREATE (september)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (october:Month { month: 10, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(october)
MERGE (firstDay:Day { day: 1, month: october.month, year: thisYear.year })
CREATE (october)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.october) |
MERGE (thisday:Day { day: i, month: october.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: october.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.october), month: october.month, year: thisYear.year })
CREATE (october)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (november:Month { month: 11, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(november)
MERGE (firstDay:Day { day: 1, month: november.month, year: thisYear.year })
CREATE (november)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.november) |
MERGE (thisday:Day { day: i, month: november.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: november.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.november), month: november.month, year: thisYear.year })
CREATE (november)-[:LAST]->(lastDay)
 
WITH dateMap, thisYear
 
MERGE (december:Month { month: 12, year: thisYear.year })
CREATE (thisYear)-[:HAS_MONTH]->(december)
MERGE (firstDay:Day { day: 1, month: december.month, year: thisYear.year })
CREATE (december)-[:FIRST]->(firstDay)
FOREACH (i IN tail(dateMap.december) |
MERGE (thisday:Day { day: i, month: december.month, year: thisYear.year })
MERGE (lastday:Day { day: i - 1, month: december.month, year: thisYear.year })
CREATE (lastday)-[:NEXT]->(thisday))
MERGE (lastDay:Day { day: last(dateMap.december), month: december.month, year: thisYear.year })
CREATE (december)-[:LAST]->(lastDay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.