0
votes

i have a neo4j graph that pictures a transportation system.

My nodes are:

  • Stop (like a bus stop)
  • Connection (connection node between 2 stops)

Relationships:

  • (Stop)-[:HAS]->(Connection)-[:TO]->(Stop)

My problem is that this pattern might repeat multiple times like this:

  • (Stop)-[:HAS]->(Connection)-[:TO]->(Stop)-[:HAS]->(Connection)-[:TO]->(Stop)-[:HAS]->(Connection)-[:TO]->(Stop)

I would like to have a cypher query with that i can find a connection between 2 stops even if there are multiple -[:HAS]->(Connection)-[:TO]-> steps between them.

I tried

MATCH (:Stop)-[:HAS]->(:Connection)-[:TO]->(:Stop)

but with this i only get results with only 1 hop. I hope my question is understandable. Kind regards

1

1 Answers

1
votes

If the only types of nodes between :Stops are :Connections then I think this will get you what you need:

   MATCH (from:Stop)-[:HAS|TO*1..5]-(to:Stop)
   WHERE ...

From a schema perspective, though, you may want to consider replacing (:Stop)-[:HAS]->(:Connection)-[:T0]->(:Stop) with just a single relationship: (:Stop)-[:CONNECTS_TO]->(:Stop)