Showing posts with label meta-graph. Show all posts
Showing posts with label meta-graph. Show all posts
Wednesday, 20 July 2016
Graphing the Tour de France - part 3/3
In the past two blogposts I have been creating and importing some nice Tour de France 2016 data. It's a small dataset, for sure, and this is by no means a realistic graph application - but perhaps we can still have some fun exploiting the data with some cypher queries. That's what we'll try now. I have put all of the example queries together in this gist, so please feel free to play around with it :) ... let's take you through it.
Monday, 23 March 2015
Hidden GraphGems revisited: the 2.2 Meta-graph
Last week I shared this great little gem of a Cypher query that would allow you to very quickly to take a view at the Model of an existing Neo4j database. This was the query that we ran to generate the Meta-graph:
// generate the pre-2.2 META-graph MATCH (a)-[r]->(b) WITH labels(a) AS a_labels,type(r) AS rel_type,labels(b) AS b_labels UNWIND a_labels as l UNWIND b_labels as l2 MERGE (a:Node:Meta {name:l}) MERGE (b:Node:Meta {name:l2})MERGE (a)-[:OUTGOING]->(:Relationship:Meta {name:rel_type})-[:INCOMING]->(b)RETURN distinct l as first_node, rel_type as connected_by, l2 as second_node
That gave us a visualisation in the Neo4j browser that looked like this:
Nice, but not really. It's a little awkward, actually, as it uses nodes to represent Node-labels (makes sense!), but also uses nodes to represent relationship-types (makes a lot less sense). I mean, it was useful and nice, but we want and need better than that.
Turns out that one of the main reasons that Michael originally created this query this way, was that he needed a way to "visualise" the relationship names. In previous versions of Neo4j, the browser did not allow you to choose the relationship property to use on the relationship - and now it does. So ... we could revisit the Meta-graph query, right? Right!
The Meta-graph as from Neo4j 2.2
There have been a LOT of improvements in Neo4j 2.2, most under the hood - but some are very visible in the Neo4j browser. One of the new things is that you can choose the property to put on the relationships in the graph view of the browser. Seems trivial, but if we make a couple of small tweaks to the Meta-graph query, it gets a whole lot better:
// generate the 2.2 META-graph MATCH (a)-[r]->(b) WITH labels(a) AS a_labels,type(r) AS rel_type,labels(b) AS b_labels UNWIND a_labels as l UNWIND b_labels as l2 MERGE (a:Meta_Node {name:l}) MERGE (b:Meta_Node {name:l2})MERGE (a)-[:META_RELATIONSHIP {name:rel_type}]->(b)RETURN distinct l as first_node, rel_type as connected_by, l2 as second_node
Instead of creating a node for every relationship, it now just ... creates a relationship for every relationship type, and adds a "name" property to the META_RELATIONSHIP relationship type. That's the property that we can then select in the new browser to create a visualisation like this one:
How much nicer is that? A lot, if you ask me.
So take it for a spin, and let me know what you think. I for one like it :)
Cheers
Rik
Tuesday, 17 March 2015
Hidden GraphGems: the Meta-graph
Over the past couple of days, I have had multiple occasions where I had to explain a graph model to an audience. Often times I would end up drawing stuff on paper/whiteboard (still my favourite, tbh), sometimes I would revert back Arrows, but more and more I am in a situation where I need to explain the model of an **existing Neo4j database**. So what then?
Well, that's when I pull out a old hidden gem, as explained and handed to me by Michael, of course. He explained it very well on this graphgist, but let me try to take you through it.
Essentially what this does is
Well, that's when I pull out a old hidden gem, as explained and handed to me by Michael, of course. He explained it very well on this graphgist, but let me try to take you through it.
Starting from a standard query
Every Neo4j instance that has the Neo4j browser (so since version 2.0), has had the following query shipped with it under the "favourites": what is related, and how. // What is related, and how
MATCH (a)-[r]->(b)
WHERE labels(a) <> [] AND labels(b) <> []
RETURN DISTINCT head(labels(a)) AS This, type(r) as To, head(labels(b)) AS That
LIMIT 10
Essentially what this does is
- look at all the (a)-[r]->(b) patterns
- ensure that the nodes have a label
- and then take the labels of (a) and the labels of (b), and the type of [r]
that gives you a simple table that explains the model of the database:
But what if I want to do what I said before - and really show the model, graph-ically? Well, as explained in Michael's gist, you can tweak the query above to do that.
Tweaking the query to generate the META-graph
Here's what you do to the query:
// generate the META-graph
MATCH (a)-[r]->(b)
WITH labels(a) AS a_labels,type(r) AS rel_type,labels(b) AS b_labels
UNWIND a_labels as l
UNWIND b_labels as l2
MERGE (a:Node:Meta {name:l})
MERGE (b:Node:Meta {name:l2})
MERGE (a)-[:OUTGOING]->(:Relationship:Meta {name:rel_type})-[:INCOMING]->(b)
RETURN distinct l as first_node, rel_type as connected_by, l2 as second_node
Let's take you through that too:
- we look at the same (a)-[r]->(b) pattern as above
- we collect all the labels of (a), types of [r] and labels of (b)
- we unwind these into l and l2
- and then start looping through these using MERGE: we create a node (a) for every new label l, a node (b) for every new label l2, and leave them alone if they already exist
- and then we also create a node for every relationship between a and b. Note that this may take a bit of getting used to in reading the model: relationships are expressed as nodes, and connected to labels with an "outgoing" and "incoming" relationship.
Overall this yields a very readable model diagram: all the META-nodes have a label "Meta", and that makes for easy visualisation. I my example below, the yellow nodes are "labels" and the blue nodes are "relationships". Very readable, I believe.
To me, this is standard equipment for dealing with existing graph models/databases.
Hope you like it, and that it is useful for you too. Massive kudos to Michael, of course - but that almost goes without saying.
Cheers
Rik
Hope you like it, and that it is useful for you too. Massive kudos to Michael, of course - but that almost goes without saying.
Cheers
Rik
Subscribe to:
Posts (Atom)




