1
votes

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?

1
If you plan/expect to execute both the generic and specific queries often, I'm wondering if just adding the generic :LIKES relationship in addition to the specific :LIKES_* relationships is the way to go. Won't a graph model aligned with the queries be more efficient, in general?rickhg12hs
Yeah it seems a good solution too. What I would lose though is a little bit of time and space for creating this additional relations and much of db.schema clarity...Jakub Licznerski

1 Answers

5
votes

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.