16.12. Cypher Cookbook

16.12.1. Hyperedges and Cypher
16.12.2. Basic Friend finding based on social neighborhood
16.12.3. Co-favorited places
16.12.4. Find people based on similar favorites
16.12.5. Multirelational social graph

The following cookbook aims to provide a few snippets, examples and use-cases and their query-solutions in Cypher.

16.12.1. Hyperedges and 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

Find Groups

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:

Result

role.name
1 rows, 5 ms

Role1


Find all groups and roles for a user

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

Result

role.namegroup.name
2 rows, 3 ms

Role1

Group2

Role2

Group1


16.12.2. Basic Friend finding based on social neighborhood

Imagine an example graph like

Graph

Simple Friend Finder

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.

Result

friend_of_friend.namecount(*)
3 rows, 9 ms

Ian

2

Derrick

1

Jill

1


16.12.3. Co-favorited places

Graph

Co-Favorited Places - Users Who Like x Also Like y

Find places that people also like who favorite this place:

  • Determine who has favorited place x.
  • What else have they favorited that is not place x.

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.

Result

stuff.namecount(*)
3 rows, 2 ms

MelsPlace

2

CoffeShop2

1

SaunaX

1


Co-Tagged Places - Places Related through Tags

Find places that are tagged with the same tags:

  • Determine the tags for place x.
  • What else is tagged the same as x that is not x.

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.namecollect(tag.name)
3 rows, 2 ms

MelsPlace

List(Cool, Cosy)

CoffeeShop3

List(Cosy)

CoffeeShop2

List(Cool)


16.12.4. Find people based on similar favorites

Graph

Find people based on similar favorites

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.

Result

person.namecount(stuff)
2 rows, 11 ms

Derrick

2

Jill

1


16.12.5. Multirelational social graph

Graph

Who FOLLOWS or LOVES me back

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.

Result

other.nameTYPE(r1)
3 rows, 3 ms

Sara

FOLLOWS

Maria

FOLLOWS

Maria

LOVES