cypher: counting different relationship types

420 views
Skip to first unread message

tcb

unread,
Oct 2, 2012, 11:36:14 AM10/2/12
to ne...@googlegroups.com
Hi,

I am trying to count the number of relationships of each type for some nodes.

This query (from the docs) is a good start and gives me the type and the count of that type
START n=node(1)
MATCH n-[r?]->()
RETURN type(r), count(*)

But I know the types I want and I would like to return something like this:

RETURN count(type(r) = REL1), count(type(r) = REL2)

- this doesn't work ;(

It's a little hard to figure just what cypher is able to do- it seems like something like this should work. My best attempt so far is two MATCH clauses:

MATCH n-[r1?]->()
MATCH n-[r2?]->()
RETURN count(type(r1)), count(type(r2)) 

Is there a better way to do this?

On a side note- how does having the two match statements affect the performance of the cypher query? Does it loop over all relationships of each node twice? This could be costly when I have a dense graph.

thanks,





Peter Neubauer

unread,
Oct 2, 2012, 3:36:46 PM10/2/12
to ne...@googlegroups.com
Hi there, you can do


START n=node(1)
MATCH n-[r?:REL1|REL2]->()
RETURN type(r), count(*)

see http://console.neo4j.org/r/5pig2k and
http://docs.neo4j.org/chunked/snapshot/query-match.html#match-match-by-multiple-relationship-types

Cheers,

/peter neubauer

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 http://graphconnect.com
> --
>
>

tcb

unread,
Oct 2, 2012, 3:55:21 PM10/2/12
to ne...@googlegroups.com

thanks, but that returns the total count of either REL1 or REL2. What I would like to do is return the separate counts, rather than the aggregate:

return count(REL1), count(REL2) 

Is there a way to do this?

thanks,


--



Michael Hunger

unread,
Oct 2, 2012, 4:21:35 PM10/2/12
to ne...@googlegroups.com
....
with collect(r) as rels
return length(filter(r in rels : type(r) == 'REL1')) as REL1, 
length(filter(r in rels : type(r) == 'REL2')) as REL2

Cheers

Michael

--
 
 

Peter Neubauer

unread,
Oct 2, 2012, 4:23:11 PM10/2/12
to ne...@googlegroups.com
man,
that one is cool Michael "Open Sourcerer" Hunger!

Cheers,

/peter neubauer

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 http://graphconnect.com


> --
>
>

Michael Hunger

unread,
Oct 2, 2012, 4:28:58 PM10/2/12
to ne...@googlegroups.com
Should be easier though :)
> --
>
>

tcb

unread,
Oct 2, 2012, 5:02:32 PM10/2/12
to ne...@googlegroups.com
thanks Michael

that's a very nifty cypher alright, but it doesn't do what I want ;) 

It collects all the relationships for all nodes and then counts the number of each type. I want the counts of each type for each individual node.

This is what I've got working in the meantime:

start n=node(*)
match n-[r1?:REL1]->(), n-[r2?:REL2]->()
return n, count(r1) as REL1, count(r2) as REL2

+------------------------------------------------------------------+
| n                                                   | REL1 | REL2 |
+------------------------------------------------------------------+
| Node[11] | 10   | 2   |
| Node[10] | 4    | 6   |
| Node[12] | 0    | 5   |
....
| Node[13] | 6    | 3   |
| Node[14] | 2    | 1   |
+------------------------------------------------------------------+

and this seems to work just fine.

I've noticed there are stats for cypher queries on the scala side- is there any progress on exposing them to java-side? It would be a great help to improve our cypher queries...

thanks,


--
 
 

Peter Neubauer

unread,
Oct 2, 2012, 5:09:43 PM10/2/12
to ne...@googlegroups.com
Hi there,
the exposure sounds like a perfect contribution area me thinks :)

Cheers,

/peter neubauer

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 http://graphconnect.com


> --
>
>
Reply all
Reply to author
Forward
0 new messages