The following cookbook aims to provide a few snippets, examples and use-cases and their query-solutions in Cypher.
Imagine a user being part of different groups. A group can have different roles, and a
user can be part of different groups. He also can have
different roles in different groups apart from the membership. The
association of a User, a Group and a Role can be referred to as a
HyperEdge. However, it can be easily modeled in a property graph as a
node that captures this n-ary relationship, as depicted below in the
U1G2R1
node.
Graph
To find out in what roles a user is for a particular groups (here Group2), the following Cypher Query can traverse this HyperEdge node and provide answers.
Query
START n=node:node_auto_index(name = "User1") MATCH n-[:hasRoleInGroup]->hyperEdge-[:hasGroup]->group, hyperEdge-[:hasRole]->role WHERE group.name = "Group2" RETURN role.name
The role of User1
:
Here, find all groups and the roles a user has, sorted by the roles names.
Query
START n=node:node_auto_index(name = "User1") MATCH n-[:hasRoleInGroup]->hyperEdge-[:hasGroup]->group, hyperEdge-[:hasRole]->role RETURN role.name, group.name ORDER BY role.name ASC
The groups and roles of User1
Imagine an example graph like
Graph
To find out the friends of Joes friends that are not already his friends, Cypher looks like:
Query
START joe=node:node_auto_index(name = "Joe") MATCH joe-[:knows]->friend-[:knows]->friend_of_friend, joe-[r?:knows]->friend_of_friend WHERE r IS NULL RETURN friend_of_friend.name, COUNT(*) ORDER BY COUNT(*) DESC, friend_of_friend.name
The list of Friends-of-friends order by the number of connections to them, secondly by their name.
Graph
Find places that people also like who favorite this place:
Query
START place=node:node_auto_index(name = "CoffeeShop1") MATCH place<-[:favorite]-person-[:favorite]->stuff RETURN stuff.name, count(*) ORDER BY count(*) DESC, stuff.name
The list of places that are favorited by people that favorited the start place.
Find places that are tagged with the same tags:
Query
START place=node:node_auto_index(name = "CoffeeShop1") MATCH place-[:tagged]->tag<-[:tagged]-otherPlace RETURN otherPlace.name, collect(tag.name) ORDER By otherPlace.name DESC
The list of possible friends ranked by them liking similar stuff that are not yet friends.
Result
otherPlace.name | collect(tag.name) |
---|---|
3 rows, 2 ms | |
|
|
|
|
|
|
Graph
To find out the possible new friends based on them liking similar things as the asking person:
Query
START me=node:node_auto_index(name = "Joe") MATCH me-[:favorite]->stuff<-[:favorite]-person, me-[r?:friend]-person WHERE r IS NULL RETURN person.name, count(stuff) ORDER BY count(stuff) DESC
The list of possible friends ranked by them liking similar stuff that are not yet friends.
Graph
This example shows a multi-relational * network between persons and things they like. * A multi-relational graph is a graph with more than * one kind of relationship between nodes.
Query
START me=node:node_auto_index(name = 'Joe') MATCH me-[r1]->other-[r2]->me WHERE type(r1)=type(r2) AND type(r1) =~ /FOLLOWS|LOVES/ RETURN other.name, type(r1)
People that FOLLOWS
or LOVES
Joe
back.
Copyright © 2011 Neo Technology