DEV Community

Muhammad Muneeb Ur Rehman
Muhammad Muneeb Ur Rehman

Posted on

Read/ Query Edges from Apache AGE

In Apache AGE, we can query the edges in very much different ways depending on our need.

Undirected Edges Query
SELECT * FROM cypher('graph_name', $$
MATCH (:Person)-[edg]-(:movie)
RETURN edg
$$) as (edg agtype);

Explanation:
In the above query, we are using MATCH (:Person)-[edg]-(:movie) to get all the edges that are either from Person node to movie node, or from movie node to Person node.

Outgoing Edges
SELECT * FROM cypher('graph_name', $$
MATCH (:Person)-[edg]->(:movie)
RETURN edg
$$) as (edg agtype);

Explanation:
In the above query, we are using MATCH (:Person)-[edg]->(:movie) to get all the edges that are made from Person node to movie node. We can specify any particular edge type by using MATCH (:Person)-[edg: Directed]->(:movie). We will get only those edges from Person node to movie node in which Person has Directed the movie.

Similarly, for Ingoing Edges, our Query will look like this:

MATCH (:Person)<-[edg]-(:movie)

MATCH (:Person)<-[edg: Directed]-(:movie)

Match on Node's attribute
SELECT * FROM cypher('graph_name', $$
MATCH (:Person {name: 'Oliver Stone'})-[edg: workedIn]->(movie)
RETURN edg
$$) as (edg agtype);

Explanation:
Here in this Query, we have MATCH (:Person {name: 'Oliver Stone'})-[edg: workedIn]->(movie), we are querying all the edges in which Person named 'Oliver' has worked.

Get attribute of Edge
SELECT * FROM cypher('graph_name', $$
MATCH (:movie{title: 'Wall Street'})<-[r:ACTED_IN]-(:actor)
RETURN r.role
$$) as (role agtype);

Explanation:
Here in this query, we are using RETURN r.role to get only the information about the role of actors who acted in movie with title Wall Street.

Multiple Edges
SELECT * FROM cypher('graph_name', $$
MATCH (charlie {name: 'Charlie Sheen'})-[:ACTED_IN]->(movie)<-[:DIRECTED]-(director)
RETURN movie.title, director.name
$$) as (title agtype, name agtype);

Explanation:
Here in this query, we are using multiple edges. We want to get movie title and director's name of all the movies in which 'Charlie Sheen' has acted.
Firstly, we have (charlie {name: 'Charlie Sheen'})-[:ACTED_IN]->(movie) to get all movies in which 'Charlie Sheen' has acted. Secondly, we are connecting it with DIRECTED edge by uings <-[:DIRECTED]-(director) to get information about the director (i. e his name).

Conclusion:
These are a few very commonly used Queries to get data from different relations. Using this pattern of Query make it simple for us to dig deep into complex relations easily without worrying about complex joins logics. This is one of the beauty of Apache AGE.

For more details visit: https://age.apache.org/overview/

AGE Github Link: https://github.com/apache/age

AGE Viewer Github Link: https://github.com/apache/age-viewer

Top comments (0)