简体   繁体   中英

neo4j 2.0 / cypher searching by date

I have looked at previous SO questions about using neo4j with dates and this blog post

http://blog.nigelsmall.com/2012/09/modelling-dates-in-neo4j.html

I'm not exactly sure how to get this to work however. Basically I need two things, to add a date to a node and then to query nodes by date.

As an example of something similar to what I like, imagine I have the movie The Matrix in my graph. Text examples for queries that should include then the movie The Matrix:

Movies released in Q1, 1999
Movies relased on March 31, 1999
Movies released in March 1999
Movies released before 2000
Movies released between 1998 and 20000

What I've tried for now as a start is building the date graph as described in the blog post. I tried with the following query, but I guess it's not constructed correctly

CREATE UNIQUE p = (CAL)-[:YEAR]->(1999 { number:1999 })-[:QUARTER]->(1 { number:1} )-[:MONTH]->(3 { number:3})-[:DAY]->(31 { number:31}) return p;

I guess then after I've made a node for a specific date, I would add a released_on->(that_date) to The Matrix.

So now I'm wondering if this is the way to go for the kind of queries I'd like to do, and how to actually make it work.

If those are the only queries you are planning to do, I would say that a property and an index query might be a better idea.

For each movie, you add a date property as a string in the following format : YYYYMMDD You can then query for a specific date from a date_index, or have where conditions like : > 19990101 and <19990401 for the first quarter of 1999.

There are of course few shortcomings with this approach, one that comes to my mind being that you can't get movies by "seasonality", say for examples all summer movies for all the years ! In this, the in graph data index is a better idea.

I ended up putting the dates in the graph in a similar way to what is described in the blog post.

To add a date I use the following query:

MERGE (n0:Calendar) CREATE UNIQUE (n0)-[r0:YEAR]->(n1 {number: 2003})-[:QUARTER]->(q { number: 1} )-[r1:MONTH]->(n2 {number: 3})-[r2:DAY]->(n3 {number: 31}) RETURN n3;

Then I use queries similar to this to get nodes and their dates:

MATCH (r)-[:has_date]->(day)<-[:DAY]-(month)<-[:MONTH]-(quarter)<-[:QUARTER]-(year) return day,month,quarter,year;

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