简体   繁体   中英

Retrieve actions that happened in a date range

I have searched an answer for this but haven't got an exact solution.

I have a simple application in which I track the items that a person favourites. I create a relationship like so:

         [:favorite]
(user) ---------------------> (item)
         timestamp:xxxxxx

The user node gets a favorite relationship to the item, and I store the timestamp as a relationship property.

There can be several users favoriting the same item, and also one user can favorite several items. Now, my usecase is to find the items that were favorited in a particular date range, like:

(1) Items favorited in the last 1 hour

(2) Items favorited between 1st Jan 2015 and 15th Jan 2015

(3) Hour-wise split of items favorited on 15th January 2015

I am trying to find out the most optimized way to return these results. I am using Everyman Neo4j for PHP.

My question is how do I index the timestamp property (I believe that would vastly improve performance)?

Also, what would be an optimal way to structure the queries for the three scenarios listed above?

Usually you'd model time as a kind of tree or linked list structure in the graph.

But for your use-case, you can use a relationship-auto-index to index the timestamp property and then query for it: see: http://neo4j.com/docs/stable/rest-api-configurable-auto-indexes.html

I think it would make sense to choose a format that is easy to search and sort, like yyyy-mm-dd-hh

change your conf/neo4j.properties to:

relationship_auto_indexing=true
relationship_keys_indexable=timestamp

but you have to re-index existing data,

start rel=relationship()

and you can query it:

start rel = relationship:relationship_auto_index("timestamp:2015-01-*")
match (user)-[rel]->(item)
....

or

start rel = relationship:relationship_auto_index("timestamp:2015-01-*")
return startNode(rel) as user, endNode(rel) as item

Ranges are a not possible with numeric values but with strings.

start rel = relationship:relationship_auto_index("timestamp:[2015-01-01-00 TO 2015-02-01-00]")
return startNode(rel) as user, endNode(rel) as item

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