Search code examples
neo4jcypher

How to find paths between different services in Neo4j


I'm new to Neo4j and I'm working on poc where there are multiple services that can read and write to each other and modeled with <CAN_READ(INCOMING):CAN_WRITE(OUTGOING)> directed relationships as shown in the image below. I want to find all paths from A to F through compute services which can read from A and write to Service F and compute service should also able to write data to Service D. So the query should able to return paths highlighted in green and purple.

I have tried the below query which is giving Compute Services B,C and Storage Service A and Database Service F but Service D is not part of result path

What I'm trying to achieve is, I'm trying to find the services that can read from a service and write to multiple other services with the same relations

Query : MATCH p= (m:Service WHERE m.name = "A")-[r1:CAN_READ]->(n:Service)-[r2:CAN_WRITE]->(o:Service WHERE o.name = "F"),(n)-[r3:CAN_WRITE]->(t:Service WHERE t.name = "D") RETURN p

Result Output Paths: (A)<-[:CAN_READ]-(B)-[:WRITE_TO]->(F), (A)<-[:CAN_READ]-(C)-[:WRITE_TO]->(F)

How to get the node D as part of output path? Will i able to get the node D as part of output or it will be omitted as it not part of the primary MATCH path ? Do I need to use any other functions or features of neo4j to achieve the same enter image description here


Solution

  • Paths are linear, so it isn't possible to have a result with D as part of the same path as A, B and F (without re-traversing the relationship that joins B and D—but that would yield a path with B appearing twice i.e. A->B->D->B->F).

    You can assign the second pattern to another path variable e.g. q and return that along with p:

    MATCH p = (:Service {name: "A"})-[r1:CAN_READ]->(n:Service)-[r2:CAN_WRITE]->
                (:Service {name: "F"}),
          q = (n)-[r3:CAN_WRITE]->(:Service {name: "D"}) 
    RETURN p, q
    
    Results:
    ╒══════════════════════════════════════════════════════════════════════╤═══════════════════════════════════════════════════════════╕
    │p                                                                     │q                                                          │
    ╞══════════════════════════════════════════════════════════════════════╪═══════════════════════════════════════════════════════════╡
    │(:Service {name: "A"})-[:CAN_READ]->(:Service {name: "B"})-[:CAN_WRITE│(:Service {name: "B"})-[:CAN_WRITE]->(:Service {name: "D"})│
    │]->(:Service {name: "F"})                                             │                                                           │
    ├──────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────┤
    │(:Service {name: "A"})-[:CAN_READ]->(:Service {name: "C"})-[:CAN_WRITE│(:Service {name: "C"})-[:CAN_WRITE]->(:Service {name: "D"})│
    │]->(:Service {name: "F"})                                             │                                                           │
    └──────────────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────┘