DEV Community

Rahimullah Shaheen
Rahimullah Shaheen

Posted on

Getting Started with the MATCH Clause in Apache Age

Introduction:
The MATCH clause is a fundamental component of graph querying in Apache Age.The MATCH clause provides a flexible and expressive way to traverse the graph, specify conditions, and filter results based on patterns. It allows you to define relationships, specify properties, and traverse paths within the graph. The power of the MATCH clause lies in its ability to capture complex graph patterns and retrieve the relevant data efficiently.

In this blog, we will explore the key elements and usage of the MATCH clause in Apache Age, providing a comprehensive understanding of its capabilities.

Basic syntax and structure:
It starts with the keyword "MATCH" followed by the pattern specification.

MATCH (node:label)
RETURN node

Example:

MATCH(movie:Movie)
return movie.title
Enter fullscreen mode Exit fullscreen mode

the above query will return all the nodes with Movie label on it

Relationship Matching:
You can also retrieve nodes based on the relationships using match clause. To do this you use the following pattern
MATCH (node:label)<-[: Relationship]-(n)
RETURN n

Example:

MATCH (:Person {name: 'Hassan'})-->(movie)
RETURN movie.title
Enter fullscreen mode Exit fullscreen mode

the above query returns any node with outgoing relation to the person node with name property Hassan

Filtering with WHERE Clause:
You can use the WHERE clause to apply filters and conditions to the matched nodes and relationships. It allows you to specify constraints and logical expressions to further refine the query results.

MATCH (person:Person {name: 'John'})-->(friend:Person)
WHERE friend.age > 25
RETURN person.name
Enter fullscreen mode Exit fullscreen mode

The above query will return any node with outgoing relation to the person node with name property John and filters the node based on the age greater then 25

Conclusion:
This is a basic example, but the MATCH clause can be extended to include more complex patterns, multiple relationships, and additional conditions as per the requirements of your graph analysis.

Top comments (0)