Search code examples
neo4jcypher

Finding connected path in neo4j


enter image description here

I have a graph like the one in the image above with three kinds of nodes and four type of relationships, is there a way I can find all the connected nodes given a (PHN) or (EML) node type value.

Note that give a phone number I want to get to all people connected to this node (directly or indirectly). For example, given (:PHN { val: 100 }) should return both EMP A and EMP B nodes

This is for a contact duplicate/merge suggestion kindof a scenario, if there are any other reference implementations I would like to go through them as well.

Thank you!


Solution

  • To just get the EMP nodes connected to the PHN node with val = 100, add a label expression to the right hand node pattern:

    MATCH (:PHN {val: 100})--+(n:EMP)
    RETURN DISTINCT n
    

    The DISTINCT returns each EMP node once and allows the query planner to choose a fast plan.

    The --+ is a quantified relationship that will match on one or more relationships of any type or direction.