cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to return node with most incoming relationships of a specific type

argytzak
Node Link

Hi all,

I have a list of Products and Persons. A Product can have relationships to different Persons, such as "hasProductOwner", "hasDeveloper", "hasTechnicalWriter", etc. (these are effectively roles that a person can have for a Product).

A Person can have multiple roles for the same Product or across different Products.

I want to find the Person with the most roles across all Products (i.e. the count), as well as I want to list all the roles that this person has against which product.

I am running this query, which gets me the results, but a) it says it's deprecated and b) how do I filter that I only want to count relationship between Product and Person of a certain type (e.g. only "hasProductOwner", "hasDeveloper", "hasTechnicalWriter") ? 

This gives me the count

MATCH (pr:Product`)-[r*0..1]-(pe:Person`)
WITH pe, count(r) as rs
RETURN pe.lastName, rs
ORDER BY rs DESC
 
This gives me the list of relationships
MATCH (pr:Product`)-[r*0..1]-(pe:Person`)
WITH pe, collect(r) as rs
RETURN pe.lastName, rs
 
This gives me an error, as the type() function expect a relationship, not a list of relationships
MATCH (pr:Product`)-[r*0..1]-(pe:Person`)
WITH pe, collect(type(r)) as rs
RETURN pe.lastName, rs
 
Many thanks
1 ACCEPTED SOLUTION

argytzak
Node Link

Thanks for your answer!

I read a bit more about the collect() function and ended up writing this, which works well.

MATCH (pr:Product)-[r]->(pe:Person)
WITH pe, collect({role:type(r), product: pr.uri}) as rolesOnProducts, count(r) as roleCount
RETURN pe.lastName as "Last Name", roleCount, rolesOnProducts
ORDER BY roleCount DESC

 Best regards

View solution in original post

2 REPLIES 2

Cobra
Ninja
Ninja

Hello @argytzak 🙂

 

MATCH path=(product:Product)-[r*0..1]-(person:Person)
WITH person, [relationship IN relationships(path) | type(relationship)] AS types
RETURN person.lastName, types

 

Regards,
Cobra

argytzak
Node Link

Thanks for your answer!

I read a bit more about the collect() function and ended up writing this, which works well.

MATCH (pr:Product)-[r]->(pe:Person)
WITH pe, collect({role:type(r), product: pr.uri}) as rolesOnProducts, count(r) as roleCount
RETURN pe.lastName as "Last Name", roleCount, rolesOnProducts
ORDER BY roleCount DESC

 Best regards