moshe beeri
moshe beeri

Reputation: 2037

Neo4j cypher time interval histogram query of time tree

I would like to build an histogram on time series stored as time tree in neo4j. The data structures are event done by a user each has timestamp, say user purchases category. What I need to have is the number of browsing on each category by each user between start and end time, with interval of (1 second to days) My model feats graph db very nicely, as I read neo4j documentation I could not find any way to do it in one query, and I'm afraid that calling for each user would be very slow.

Time tree model

I am aware to cypher capabilities, but I have no idea how to create such query. I am looking for something like this (not working)

MATCH startPath=(root)-[:`2010`]->()-[:`12`]->()-[:`31`]->(startLeaf),
endPath=(root)-[:`2011`]->()-[:`01`]->()-[:`03`]->(endLeaf),
valuePath=(startLeaf)-[:NEXT*0..]->(middle)-[:NEXT*0..]->(endLeaf),
vals=(middle)-[:VALUE]->(event)
WHERE root.name = 'Root'
RETURN event.name, count(*)
ORDER BY event.name ASC
GROUP BY event.timestamp % 1000*60*10 // 10 minutes histogram bar 

Then I'd like to have a report, for how many users browse to each site category:

0-9 news 5, commerce 3 ; 10-19 news 6, commerce 19; 1 20-29 news 2, commerce 8;

Any idea if it is optional with neo4j time tree model? if so how? :-)

Upvotes: 2

Views: 731

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

Does this work?

MATCH
  startPath=(root)-[:`2010`]->()-[:`12`]->()-[:`31`]->(startLeaf),
  endPath=(root)-[:`2011`]->()-[:`01`]->()-[:`03`]->(endLeaf),
  valuePath=(startLeaf)-[:NEXT*0..]->(middle)-[:NEXT*0..]->(endLeaf),
  vals=(middle)-[:VALUE]->(event)
WHERE root.name = 'Root'
RETURN event.name, event.timestamp % 1000*60*10 AS slice, count(*)
ORDER BY slice ASC

Basically I just added the event.timestamp % 1000*60*10 into the return so that Neo4j will use that as a grouping criteria

Upvotes: 2

Related Questions