简体   繁体   中英

Neo4J 2.0 How to refactor the database to introduce Date nodes

I have existing database which i just imported from SQL DB, for eg lets call it Order which has property called PlacedOn of type DateTime stored as part of the node which looks like 2013-05-01T02:15:18+00:00 .

I am now looking to extract the date time and create an association such that I am able query for all the Orders for a month or a year easily.

Basically what I want to achieve is make the association look as below:

(o:Order)-[:PLACED_ON]->(d:Day)-[:BELONGS_TO]->(m:Month)-[:BELONGS_TO]->(y:Year)

For a performance test I have created 1100000 nodes , so having to load it in my c# code and creating it is not practical, so is there a way in Cypher or any other way in Webadmin that can allow me to extract the day month and year part and create the nodes with the associations as mentioned above.

Regards Kiran

Please take a look at this Cypher query for creating a DateTime tree in Neo4j 2.0:

https://gist.github.com/kbastani/8519557#file-calendaryear-cql

Run the CalendarYear Cypher script for each year of data.

You'll need to parse your DateTime timestamp value to extract the Year, Month, Day integer values. Then for each of your orders you'll need to attach a day node. What I suggest is that when you import your data, you add in properties to your order which have the Year, Month, Day values.

Then run a Cypher statement in batches to add in your timeline, by each month of a year with records:

MATCH (order:Order { month: 5, year: 2014 })
WITH collect(order) as orders
FOREACH (order in orders |
    MERGE (day:Day { day: order.day, month: order.month, year: order.year })
    CREATE (day)<-[:PLACED_ON]-(order))

Now you can query your data using the timeline index.

MATCH (order:Order)-[:PLACED_ON]->(day:Day { day: 1, month: 5, year: 2013 })
RETURN order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM