Reputation: 1088
I have this theoretical graph structure:
(:Car)<-[:LIKES_C]-(:Person)-[:LIKES_B]->(:Bike)
Person
also has other relationships not containing a word LIKES, say (:Person)-[:LOVES]-(:Person)
I'd like to write a cypher query which will yield all nodes connected to a Person
with a relation, which name starts with LIKES.
Also I cannot change relation names to LIKES
because there are lots of nodes with label Bike
and Car
and according to this post Neo4j will be inefficient in a query like:
MATCH (p:Person)-[:LIKES]->(:Car)
It will search through both Cars
and Bikes
and then filter for Cars
effectively increasing execution time.
Is there an efficient way to query for LIKES*
relationship?
Upvotes: 2
Views: 1904
Reputation: 166
Jakub! There is way to match set of relationship types:
match (p:Person)-[:LIKES_C|LIKES_B]->(carOrBike)
In that case you will be able to traverse both :LIKES_C
and :LIKES_B
relationships from :Person
node.
One could also use UNWIND
with a list of relationship names in combination with apoc.cypher.run
(described here) to query for multiple relations at once.
Upvotes: 5