Ichnafi
Ichnafi

Reputation: 33

How to find Paths based on a Relationship property ?

i'm totally new to Neo4J and for 3 days no, I've been trying to figure out, how to get paths between 2 room.

The function allShortestPaths() gets me a result like this http://www.directupload.net/file/d/3948/79bvpbzd_jpg.htm or like this http://www.directupload.net/file/d/3948/56j6plhh_jpg.htm
(sorry, my reputation does not allow me to include pics directly).

This function worked out of the box but uses all kind of relationships to traverse from start to end. But what if i only want to get a path with a relationship with a certain property?

A path could look like this: (start:Room {name: "x"})-[*]-("another node")-[*]-("other node)-[:CONNECTED_TO {state: "free}]-("another node")-[*]-(end:Room {name: "y"}

What i know about wanted path is:

What i don't know and what is not important is:

The next step would be to specify the paths I'm looking for more precisely, for example by properties of nodes in that path.
i.e. find a path from (A) to (H), where (C {type: "SC"})-[:CONNECTED_TO {state: "free"}]-(D {type: "SC"})

I've read a lot of stuff right know and tried a million different cyphers, but the more i read and the more i try, the more i get confused. Can anyone please give me a hint?

Thank you in advance

Ichnafi

Upvotes: 2

Views: 195

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

What i know about wanted path is:

  • start node
  • end node
  • property and label of a relationship somewhere within the path

What you are looking for is the ANY predicate, it tests whether the predicate holds for at least one element in the collection. In your case the collection is all the relationships in the path .

A simple query would be :

MATCH (start:Room {name:'x'}), (end:Room {name:'y'})
MATCH p=allShortestPaths((start)-[*]-(end))
WHERE ANY(
       x IN rels(p) WHERE type(x) = 'RELATIONSHIP_TYPE' 
                    AND x.prop = propValue )
RETURN distinct(p)

More in the documentation : http://neo4j.com/docs/stable/query-predicates.html#functions-single

Upvotes: 3

Related Questions