neo4j recursive count of nodes - neo4j

In my project I need to count my node size. Node size is amount of connections with other nodes until depth of 2.
Currently I'm able to do so with two queries:
1. Get ids of all nodes with certain labels:
MATCH (n:Target) RETURN n.Key WHERE <some_where_logic_here>
2. Use returned list of Keys for count
MATCH (n)-[r *0..2]-(b) WHERE n.Key in {keyList} RETURN n.Key as targetId, count(r) as cnt
This works however I wonder if I can get same results with only one query? (like sub select in SQL)
Thanks

Can you try this?
MATCH (n:Target)
WHERE <some_condition>
WITH n
MATCH (n)-[r *0..2]-(b)
RETURN n.Key as targetId, count(r) as cnt

You should be able to do everything with one MATCH. For example:
MATCH (n:Target)
WHERE <some_where_logic_here> AND n.Key in $keyList
RETURN n.Key as targetId, SIZE((n)-[*0..2]-()) as cnt

Related

How Many Nodes Are Involved in a Match

How can I know how many nodes and edges are involved in a MATCH? Is there another way besides Explain / Profile Match?
If you mean how many nodes are matched in a path, such as a variable-length path, then you can assign a path variable for this:
MATCH p = (k:Person {name:'Keanu Reeves'})-[*..8]-(t:Person {name:'Tom Hanks'})
WITH p LIMIT 1
RETURN p, length(p) as pathLength, length(p) + 1 as numberOfNodesInPath
You can also use nodes(p) and relationships(p) to get the collection of nodes and relationships that make up the path, and you can use size() on those collections to get their size.
There exists the COUNT() function of Cypher that allows you to count the number of elements. As for example in this query:
MATCH (n)
RETURN COUNT(n);
This query will count all nodes in your database.
You can find more information in the cypher manual, under the aggregating functions. Check it out.
The following Cypher snippet should return the number of distinct nodes and relationships found by any given MATCH clause. Just replace <your code here> with your MATCH pattern.
MATCH <your code here>
WITH COLLECT(NODES(p)) AS ns, SUM(SIZE(RELATIONSHIPS(p))) AS relCount
UNWIND ns AS nodeList
UNWIND nodeList AS node
RETURN COUNT(DISTINCT node) AS nodeCount, relCount;

How to find nodes with more than one relationship between each other with cypher in neo4j?

It is easy to identify nodes with a certain number of incoming or outgoing relationships, but I want to identify connection redundancies so I want to get a set of all nodes with more than one relationship towards each other.
Pseudo code which unfortunately does not return any results:
MATCH (n1)-[r]-(n2)
with distinct n1,r,n2, count(r) as sstcount
where sstcount > 1
RETURN n1,r,n2
I think I found a solution, the queries need to be correctly linked. Any "nicer solutions" highly appreciated.
MATCH (n1)-[r]-(n2)
WITH distinct n1,n2, count(r) as sstcount
MATCH (n1)-[r]-(n2)
where sstcount>1
return n1,r,n2
Try this one instead:
MATCH (n1)-[r]-(n2)
WHERE id(n1) < id(n2) // so we avoid matching to the same nodes in swapped order
WITH n1,n2, count(r) as sstcount
WHERE sstcount > 1
RETURN n1, n2

neo4j how to use count(distinct()) over the nodes of path

I search the longest path of my graph and I want to count the number of distinct nodes of this longest path.
I want to use count(distinct())
I tried two queries.
First is
match p=(primero)-[:ResponseTo*]-(segundo)
with max(length(p)) as lengthPath
match p1=(primero)-[:ResponseTo*]-(segundo)
where length(p1) = lengthPath
return nodes(p1)
The query result is a graph with the path nodes.
But if I tried the query
match p=(primero)-[:ResponseTo*]-(segundo)
with max(length(p)) as lengthPath
match p1=(primero)-[:ResponseTo*]-(segundo)
where length(p1) = lengthPath
return count(distinct(primero))
The result is
count(distinct(primero))
2
How can I use count(distinct()) over the node primero.
Node Primero has a field called id.
You should bind at least one of those nodes, add a direction and also consider a path-limit otherwise this is an extremely expensive query.
match p=(primero)-[:ResponseTo*..30]-(segundo)
with p order by length(p) desc limit 1
unwind nodes(p) as n
return distinct n;

Neo4j Aggregation Subtraction Nodes and properties

I'm starting with Neo4j and using graphs, and I'm trying to get the following:
I have to find the subtraction(difference) between the number of users (each user is a node) and the number of differents names they have. I have 16 nodes, and each one has his own name (name is one of the properties it has), but some of them have the same name (for example the node A has (Name:Amanda,City:Roma) and node B has (Name:Amanda, City:Paris), so I will have less name's count because some of them are repeated.
I have tried this:
match (n) with n, count(n) as c return sum(c)
That gives me the number of nodes. And then I tried this
match (n) with n, count(n) as nodeC with n, count( distinct n.Name) as
nameC return sum(nodeC) as sumN, sum(nameC) as sumC, sumN-sumC
But it doesn't work (I'm not sure if even i'm getting the names well, because when I try it, separated, it doesn't work neither).
I think this is what you are looking for:
MATCH (n)
RETURN COUNT(n) - COUNT(DISTINCT n.name) AS diff;

How to find out connection between a set of nodes?

I have a scenario where I know IDs of a list of nodes.
I need to get connection(if exists) between these nodes given their IDs.
Is there any way to achieve this?
Update:
I am using node id property not the neo4j's internal ID(using like match (n:Person{id:3}))
You can use the IN clause to select from a list of values:
MATCH (n)-[r*..2]-(m)
WHERE ID(n) IN [0,1,2] AND ID(m) IN [2,3,4]
RETURN r
I've limited the path length to 2 hops of indeterminate relationship type here, and arbitrarily picked some IDs.
To return the path instead:
MATCH p=(n)-[r*..2]-(m)
WHERE ID(n) IN [0,1,2] AND ID(m) IN [2,3,4]
RETURN p
START n=node(1,2,3,4,5,6) //your IDs of a list of nodes
MATCH p=n-[r]-m //the connection for 1 hop. for multiple hops do n-[r*]-m
WHERE Id(m) in [1,2,3,4,5,6] //your IDs of a list of nodes
RETURN p

Resources