Shane
Shane

Reputation: 325

Cypher: Match on multiple relationship types with opposite directions

Newbie question... I am trying to match on multiple relationships with Cypher. I found an example in the documentation which is almost what I want:

MATCH (wallstreet { title: 'Wall Street' })<-[:ACTED_IN|:DIRECTED]-(person)
RETURN person.name

In my case the relationships go in opposite directions.

CREATE
(a:Entity { name: 'Entity A'),  
(b:Person { name: 'Person B'),  
(c: Entity { name: 'Entity C'),  
(c)-[:ALIAS_OF]->(b),  
(b)-[:MEMBER_OF]->(a)  

I want to find every Entity to which Person B belongs OR which is an ALIAS_OF Person B and then find products produced by any of these entities

MATCH (b:Person { name: 'Person B'}),
(entities:Entity)<-[:ALIAS_OF|MEMBER_OF]-(b),
(products:Product)-[:PRODUCED_BY]->(entities)
RETURN b, entities, products

This will only find entities of which b is a member as the alias relationship goes the other direction. How can adapt this? Is it an indication that I should have structured the relationships the same way? Thanks!

Upvotes: 0

Views: 360

Answers (1)

stdob--
stdob--

Reputation: 29167

If you don’t care about the direction of the relationship, the arrow head can be omitted...

https://neo4j.com/docs/developer-manual/current/cypher/syntax/patterns/#cypher-pattern-relationship

MATCH (b:Person { name: 'Person B'})-[:ALIAS_OF|MEMBER_OF]-(entities:Entity),
      (products:Product)-[:PRODUCED_BY]->(entities)
RETURN b, entities, products

Upvotes: 1

Related Questions