Querying the Data
Welcome back to our series on building a social network app with apche-age! In our previous blog post, we covered the basics of data modeling, setting up Neo4j, creating the graph, and adding sample data. Now that we have our graph populated with users, posts, comments, and relationships, it's time to start querying the data. In this post, we'll explore some example queries for retrieving useful information from our social network app. If you haven't read our previous post yet, we recommend starting there to get caught up on the basics of building a social network app with apche-age here
Query to get all posts
SELECT * from cypher('social_network', $$
MATCH (p:Post)
RETURN p.title, p.content
$$) as (V agtype, C agtype);
This query returns the title and content of all posts in the graph.
Get all posts and their comments
SELECT * from cypher('social_network', $$
MATCH (V)-[R:COMMENTED]-(V2)
RETURN V,R,V2
$$) as (V agtype, R agtype, V2 agtype);
This query returns the title of each post and the content of each comment on that post.
Get all posts and their likes
SELECT * from cypher('social_network', $$
MATCH (p:Post)<-[:LIKED]-(u:User)
RETURN p.title, COUNT(u) as likes
$$) as (V agtype, R agtype);
This query returns the title of each post and the number of likes it has.
Get all posts and their authors
SELECT * from cypher('social_network', $$
MATCH (u:User)-[:POSTED]->(p:Post)
RETURN p.title, u.name
$$) as (V agtype, R agtype);
This query returns the title of each post and the name of its author.
Get all users and the users they follow
SELECT * from cypher('social_network', $$
MATCH (u:User)-[:FOLLOWS]->(f:User)
RETURN u.name, COLLECT(f.name) as following
$$) as (V agtype, R agtype);
This query returns the name of each user and a list of the names of the users they follow.
References
For further help you can look though the documentation here and Apache-age repo here.
Top comments (0)