MATCH-relationships in Neo4j
- Read
- Discuss
The MATCH clause in neo4j is mostly used for searching. The MATCH clause allows you to specify the patterns of nodes or relationships Neo4j will search for in the database.
MATCH is frequently combined with a WHERE clause, which gives the MATCH patterns additional constraints or predicates and increases their specificity.
Predicates should not be viewed as a filter that is applied only after matching because they are a component of the pattern description. This means that WHERE and the MATCH clause to which it belongs should always be combined.
Outgoing relationships
When the direction of a relationship is of interest, it is shown by using –> or <–.
Thus, if we want to find nodes connected by an outgoing relationship to a specific node we can use the following query.
Query
MATCH (:Person {name: 'Oliver Stone'})-->(movie)
RETURN movie
Returns any nodes connected by an outgoing relationship to the Person node with the name property set to Oliver Stone.

Relationship types
It is possible to introduce a variable to a pattern, either for filtering on relationship properties or to return a relationship type. Thus in order to find the relationship type we can use the following query.
Query
MATCH (:Person {name: 'Oliver Stone'})-[r]->(movie)
RETURN type(r)
Returns the type of each outgoing relationship from Oliver Stone.

Match on relationship type
When the relationship type to match on is known, it is possible to specify it by using a colon (:) before the relationship type.
Query
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor)
RETURN actor.name
Returns all actors’ names who ACTED_IN the movie Wall Street.

Or
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[r:ACTED_IN]-(actor)
RETURN wallstreet,r,actor

Match on multiple relationship types
It is possible to match on multiple relationship types by using the pipe symbol (|).
Query
In order to find all nodes having an ACTED_IN or DIRECTED relationship with a movie, we can use the following query.
MATCH (wallstreet {title: 'Wall Street'})<-[r:ACTED_IN|DIRECTED]-(person)
RETURN wallstreet, r, person
Returns nodes with an ACTED_IN or DIRECTED relationship to the movie Wall Street.

Leave a Reply
You must be logged in to post a comment.