Cypher: return/filter the sum of a collection

1,065 views
Skip to first unread message

Wes Freeman

unread,
Sep 17, 2012, 5:43:36 PM9/17/12
to ne...@googlegroups.com
Say I have a property as part of my relationships for a score, and I want to return the sum of all of the score properties within a path.

Is this possible within Cypher? I played with extract(), and sum(), but it seems like sum wants to do a "group by", aggregating the number for a set of results, rather than aggregating for a collection within the result record.

Here's an example graph:

I know that I could do this operation in my app code, but... the next step is being able to filter on the sum via with/where, or something.

Thanks,
Wes

Michael Hunger

unread,
Sep 17, 2012, 6:52:08 PM9/17/12
to ne...@googlegroups.com
Wes,

right now that's not easily possible I'm afraid. SUM only works as aggregation.

I played around with it, trying to use head(distances) + head(tail(distances)) etc. but unfortunately head() fails for empty lists instead of returning null which could be coalesced to 0.
So the above only works if you have an fixed path length for all results which you know upfront.

what I tried:
with extract(r in rels : r.distance) as distances
return head(distances) + head(tail(distances)) as distance

Michael

--
 
 

Wes Freeman

unread,
Sep 17, 2012, 11:50:22 PM9/17/12
to ne...@googlegroups.com
Ok, I'll code it in my app for now. It would be a nice addition to cypher--the syntax seems to be there, but it doesn't quite work.

Thanks,
Wes

--
 
 

Wes Freeman

unread,
Sep 18, 2012, 11:08:09 AM9/18/12
to ne...@googlegroups.com
I changed the way my stuff works so I can just use the last score, instead of the sum.

Something like this:
http://console.neo4j.org/r/q157sy

Also, you guys deserve a million kudos on the console.neo4j.org app; it really makes discussing cypher with example databases trivial, and lets you play with scenarios without going into app code. Keep it up!

Wes

Peter Neubauer

unread,
Sep 19, 2012, 2:14:10 AM9/19/12
to ne...@googlegroups.com
Thank you Wes,
most of the console creds goes to Michael Hunger, who is too shy to
come forth ;)

Cheers,

/peter neubauer

Neo4j 1.8.RC1 "Vindeln Vy" -
http://blog.neo4j.org/2012/09/neo4j-18rc1-really-careful-ftw.html

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Wanna learn something new? Come to @graphconnect.
> --
>
>

Wes Freeman

unread,
Sep 25, 2012, 11:40:49 PM9/25/12
to ne...@googlegroups.com
So I poked at the Cypher code, today, and added an expression called "sumcollect" (because I didn't want to compete with "sum", which is an aggregation thing). Surprisingly easy to get into--kudos to the Scala guys doing Cypher.

Looking for feedback: does this make sense? or am I thinking about this the wrong way... etc.

Feel free to look at my code:

It acts like this:
start n=node(1) 
match p=n-[r*]->m 
where not(m-->()) 
with p, extract(x in r: x.score) as scores, length(p) as len 
return scores, sumcollect(scores), len;
+---------------------------------------+
| scores     | sumcollect(scores) | len |
+---------------------------------------+
| [0.9,0.9]  | 1.8                | 2   |
| [0.8,0.79] | 1.59               | 2   |
| [0.4,0.75] | 1.15               | 2   |
| [0.4,0.45] | 0.8500000000000001 | 2   |
+---------------------------------------+
4 rows
4 ms

Wes

--



Wes Freeman

unread,
Jan 23, 2013, 1:42:41 AM1/23/13
to ne...@googlegroups.com
Instead of sumcollect, shortly after I submitted a more general function called reduce. It's available as of 1.9.M01.

There's an example of "sum" syntax in the manual:

Wes

On Tue, Jan 22, 2013 at 8:12 PM, Arvind Shima <arvind...@gmail.com> wrote:
Yes Wes this definitely makes sense !
I was hoping this was a standard function for I have almost given up doing this using combinations of EXTRACT, FILTER, SUM.
Would appreciate if you or anyone else here knows a way to do this using the standard Functions ?
--
 
 

Arvind Shima

unread,
Jan 23, 2013, 1:23:38 PM1/23/13
to ne...@googlegroups.com
Thanks Wes ! right after i posted this i was able to see your other posting !
REDUCE is exactly what I was looking for !
Reply all
Reply to author
Forward
0 new messages