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.

Finding the closest node with a certain label from a given node

Hello,

I am currently using Neo4j Community version 3.5.17. I have :Person nodes out of which a few are :Fraud nodes. For a certain :Person node, I would like to find out how many hops away is the closest :Fraud node.

I have used the following queries up till now but I think there might be a better implementation -

MATCH p=shortestPath( (a:Person {fid:''})-[*..6]-(b:Fraud)) RETURN p limit 1;

MATCH path=(a:Person {fid:''})-[*1..6]-(p:Fraud) RETURN a.fid, min(length(path)) AS distance;

Please let me know if there might be some BFS like implementation because the number of nodes grow very fast as the number of hops increase.

Thanks and Regards,
Kevin

1 ACCEPTED SOLUTION

Using limit in expandConfig does return only one single path for each person. Is this what you're looking for?

match (n:Person) with n limit 100 
call apoc.path.expandConfig(n,{labelFilter:'/Fraud', maxLevel:5, limit:1}) yield path 
return n.fid, length(path)

View solution in original post

3 REPLIES 3

Easiest way would be using apoc.path.expand/expandConfig. You can force BFS and use Fraud as a end node or termination filter and a limit of 1.

Hi Stefan,

Thank you. That worked well. However, I now need to find the closest fraud for 100 people. I wrote the following query -

match (n:Person) with n limit 100 call apoc.path.expandConfig(n,{labelFilter:'/Fraud', maxLevel:5}) yield path return n.fid, length(path)

I want to get the first result per person but I don't know how. Please guide me with this.

Thanks and Regards,
Kevin

Using limit in expandConfig does return only one single path for each person. Is this what you're looking for?

match (n:Person) with n limit 100 
call apoc.path.expandConfig(n,{labelFilter:'/Fraud', maxLevel:5, limit:1}) yield path 
return n.fid, length(path)