M. Carlo Bramini
M. Carlo Bramini

Reputation: 2522

Neo4J path results between to nodes passing through n relations [*] are too many and missing?

I’ve been testing the following query

MATCH p=(me)-[*]-(rf)
WHERE me.name = "Filipa" AND rf.name="Anders"
RETURN p

enter image description here

enter image description here

enter image description here

Looking at the graph, in my understanding, the path should just return 2 direct paths (both with 2 hops) from Filipa to Anders , instead I get 2 extra paths both passing from Anders and looping back to Anders with 2 extra paths going in opposite directions after Anders node.

From my comment: @Christophe Willemsen

Since I haven’t included a relation direction, 2 paths should be present, instead I just get back one path. Both should be present:

(Filipa)->(Dilshad)->(Anders) //this path is present
(Anders)->(Dilshad)->(Filipa) //This path is NOT present, why??

Furthermore in the "results" the following paths are included:

(Filipa)->(Dilshad)->(Anders)->(Becky)->(Emil)->(Cesar)->(Anderes) //These 2 paths pass 2 times at Anders node. Why??
(Filipa)->(Dilshad)->(Anders)->(Cesar)->(Emil)->(Becky)->(Anderes) 

The latter 2 paths are both passing twice to Anders’s node, why? This Doesn’t make any sense, if this is the logic we could have infinite paths?

Upvotes: 0

Views: 321

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

There are two points with your query :

  1. You match an entire pattern, with * length of relationships, meaning that if he find a path with 2 hops and one with 3 hops you'll get the both in return.

  2. You do not specify a direction of your relationship, so he will automatically find at least two paths, one in one way and one in another way.

You can specify relationship directions with an arrow :

MATCH p=(me)-[*]->(rf)

You can limit the return of the amount of paths to 1 :

MATCH p=(me)-[*]->(rf)
WHERE me.name = "Filipa" AND rf.name="Anders"
RETURN p
LIMIT 1

You can get the shortest path from me to Anders with the shortestPath clause :

MATCH p=shortestPath((me)-[*]->(rf))

which is automatically limited to one result

Upvotes: 2

Related Questions