Count nodes/relationships...

5,814 views
Skip to first unread message

kiteflo

unread,
Mar 27, 2012, 9:50:19 AM3/27/12
to Neo4j
Hi guys,

is there a simple way (Java or Cypher based) of getting the total
number of nodes and relationships in a Neo graph? Iterating is not an
option, something lightweight would be great in terms of
"template.getTotalNodes()"...

If this is not part of Neo by know maybe this is a useful extension,
we would love to use this feature for unit testing when removing
cascaded nodes as we could easily check whether all dependencies have
been removed using a count function.

Thanx + cheers, Florian!

Jim Webber

unread,
Mar 27, 2012, 10:04:02 AM3/27/12
to ne...@googlegroups.com
Hi Florian,

I wonder if GraphGlobalOperations.getAllNodes() and GraphGlobalOperations.getAllRelationships() would help?

There may be less accurate ways (statistically good enough for large graphs) using the ID generators. The kernel folks would know this.

Jim

Andres Taylor

unread,
Mar 27, 2012, 10:13:02 AM3/27/12
to ne...@googlegroups.com
On Tue, Mar 27, 2012 at 3:50 PM, kiteflo <kit...@gmail.com> wrote:
Hi guys,

is there a simple way (Java or Cypher based) of getting the total
number of nodes and relationships in a Neo graph? Iterating is not an
option, something lightweight would be great in terms of
"template.getTotalNodes()"...

Well, you can do: 

START n=node(*) 
RETURN count(n) 

and conversely

START r=relationship(*)
RETURN count(r)

But, internally, they both iterate over all nodes/relationships, which you indicated you didn't want...

Andrés

Michael Hunger

unread,
Mar 27, 2012, 11:21:17 AM3/27/12
to ne...@googlegroups.com
As this is only for testing, iterating over all nodes, rels with cypher or IteratorUtil.count() shouldn't take that long.

Other lightweight ways are not really exact they'll still report nodes as being there (or not) that are gone.

Michael

kiteflo

unread,
Mar 27, 2012, 12:43:50 PM3/27/12
to Neo4j
cool, thanx guys - exactly what we required!

cheers, Florian...

On 27 Mrz., 17:21, Michael Hunger <michael.hun...@neotechnology.com>
wrote:

Jon

unread,
Jul 16, 2012, 8:43:55 PM7/16/12
to ne...@googlegroups.com
I take it that this is unchanged as of 1.8?  There's no REST or Cypher query that will return global graph information?

Thanks!

Peter Neubauer

unread,
Jul 17, 2012, 2:32:37 AM7/17/12
to ne...@googlegroups.com

Jon,
What info for you need? Would be easy to use a plugin for that.

/peter

Send from mobile.

Jon

unread,
Jul 17, 2012, 2:13:17 PM7/17/12
to ne...@googlegroups.com
Sorry, I should've been more specific.  I'm writing a script that will output information about the entire graph; ie total nodes, total relationships, strongly connected components, etc.   And I was curious if anything was recently exposed to Cypher or POST that would have any readily available global information without having to traverse the entire graph.

I can look into writing a plugin for this.  Thanks for the tip!


On Monday, July 16, 2012 11:32:37 PM UTC-7, Peter Neubauer wrote:

Jon,
What info for you need? Would be easy to use a plugin for that.

/peter

Send from mobile.

Jim Webber

unread,
Jul 27, 2012, 12:28:22 PM7/27/12
to ne...@googlegroups.com
Hi Jon,

Cypher can do some global operations in more recent incarnations. For example:

START n=node(*) RETURN count(n)

will give you the number of nodes in the graph. Similar for relationships.

You can build on that if you're going to check the whole graph for densely connected nodes etc.

However this is going to be O(N) complexity, so could be expensive on large graphs.

Jim

NILESH KUMAR

unread,
Mar 8, 2015, 7:46:16 AM3/8/15
to ne...@googlegroups.com, andres...@neotechnology.com
Hi Guys,
how can i filter my nodes based on the count of relationship.
for example let in my database i want to filter node having single relation and keep others.

Michael Hunger

unread,
Mar 8, 2015, 2:31:24 PM3/8/15
to ne...@googlegroups.com, Andres Taylor
MATCH (n)
return size((n)-[:FOO]->())

or

MATCH (n)-->()
return n, count(*)

should both work and in 2.2 at least use the fast node.getDegree() internally

-- 
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