NoSQL Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 516 posts at DZone. You can read more from them at their website. View Full User Profile

Neo4j: Modeling Hyper Edges in a Property Graph

11.19.2013
| 2369 views |
  • submit to reddit

At the Graph Database meet up in Antwerp, we discussed how you would model a hyper edge in a property graph like Neo4j, and I realized that I’d done this in my football graph without realizing.

A hyper edge is defined as follows:

A hyperedge is a connection between two or more vertices, or nodes, of a hypergraph. A hypergraph is a graph in which generalized edges (called hyperedges) may connect more than two nodes with discrete properties.

In Neo4j, an edge (or relationship) can only be between itself or another node; there’s no way of creating a relationship between more than 2 nodes.

I had problems when trying to model the relationship between a player and a football match because I wanted to say that a player participated in a match and represented a specific team in that match.

I started out with the following model:

2013 10 22 22 47 13

Unfortunately, creating a direct relationship from the player to the match means that there’s no way to work out which team they played for.

This information is useful because sometimes players transfer teams in the middle of a season and we want to analyze how they performed for each team.

In a property graph, we need to introduce an extra node which links the match, player and team together:

2013 10 22 22 54 25

Although we are forced to adopt this design it actually helps us realize an extra entity in our domain which wasn’t visible before – a player’s performance in a match.

If we want to capture information about a players’ performance in a match we can store it on this node.

We can also easily aggregate players stats by following the played relationship without needing to worry about the matches they played in.

The Neo4j manual has a few more examples of domain models containing hyper edges which are worth having a look at if you want to learn more.

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)