Collapse sequential items in cypher

7 views
Skip to first unread message

S Ben

unread,
Jun 15, 2016, 8:41:48 PM6/15/16
to Neo4j
So here's the deal, I'm using Neo4J 3.01 and I have a graph with nodes of type Action which have amongst other links and properties the following two:
Type: [Comment,Reply,Vote etc]
Date:[epoch timestamp]
I am attempting to run a cypher query that returns a sorted list of nodes ordered by the date field but collapsing (Collect?) sequential items of the same type
So for the following nodes:

{type:'Comment',date:1}
{type:'Comment',date:2}
{type:'Vote',date:3}
{type:'Comment',date:4}
{type:'Comment',date:5}
{type:'Reply',date:6}
{type:'Reply',date:7}
{type:'Vote',date:8}
{type:'Vote',date:9}
 
I would hope to get something like:

{type:'Comment',actions:[{type:'Comment',date:1},{type:'Comment',date:2}]}
{type:'Vote',actions:[{type:'Vote',date:3}]}
{type:'Comment',actions:[{type:'Comment',date:4},{type:'Comment',date:5}]}
{type:'Reply',actions:[{type:'Reply',date:6},{type:'Reply',date:7}]}
{type:'Vote',actions:[{type:'Vote',date:8},{type:'Vote',date:9}]}

I attempted a simple collect and order cypher query:
Match (a:Action) 
with a.type as type, a  order by a.date limit 20
return  type, collect(a) as actions

 but this seems to collect each type in its own group regardless of the sequence, so the actual result is something like:
{type:'Comment',actions:[{type:'Comment',date:1},{type:'Comment',date:2},{type:'Comment',date:4},{type:'Comment',date:5}]}
{type:'Vote',actions:[{type:'Vote',date:3},{type:'Vote',date:8},{type:'Vote',date:9}]}
{type:'Reply',actions:[{type:'Reply',date:6},{type:'Reply',date:7}]}

Any Ideas of how to acheive this using cypher? I have tried different means of collect and sorts but to no avail

Any help would be great

Michael Hunger

unread,
Jun 15, 2016, 8:49:55 PM6/15/16
to ne...@googlegroups.com
You're almost there.

Match (a:Action) 
with a.type as type, a  order by a.date
return  type, collect(a)[0..20] as actions

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages