GraphGists

Bandtrace is a music encyclopedia that is based on Neo4j. This GraphGist illustrates how to find Connected Artists i.e. artists that are not directly linked.

Setup the artists

First, let’s create some artists using the CREATE statement. Each node has a name (e.g. Kurt Cobain) and a label of type Artist:

CREATE
    (kurt:Artist { name:"Kurt Cobain" }),
    (dave:Artist { name:"Dave Grohl" }),
    (nirvana:Artist { name:"Nirvana" }),
    (foo:Artist { name:"Foo Fighters" }),
    (qotsa:Artist { name:"Queens of the Stone Age" })

If we look at the graph we can see that the artist nodes have all been created but there are no relationships between them.

So, let’s map Kurt Cobain and Nirvana to each other via a MEMBER_OF relationship. Here we first MATCH two nodes in the graph and then CREATE a named relationship between them.

MATCH
    (kurt:Artist { name:"Kurt Cobain" }),
    (nirvana:Artist { name:"Nirvana" })
CREATE
    (kurt)-[:MEMBER_OF]->(nirvana)

Then we can link Dave Grohl to the other bands

MATCH
    (dave:Artist { name:"Dave Grohl" }),
    (nirvana:Artist { name:"Nirvana" }),
    (foo:Artist { name:"Foo Fighters" }),
    (qotsa:Artist { name:"Queens of the Stone Age" })
CREATE
    (dave)-[:MEMBER_OF]->(nirvana),
    (dave)-[:MEMBER_OF]->(foo),
    (dave)-[:MEMBER_OF]->(qotsa)

Now the graph looks a bit nicer!

Find connected artists

Connected Artists are artists that is not directly linked to each other. E.g. Dave Grohl and Kurt Cobain are Connected Artists via Nirvana (for social graphs this is commonly known as friend-of-a-friend).

So, to find the artists that are connected to Nirvana the following query can be used:

MATCH
    (nirvana:Artist { name:"Nirvana" })-[:MEMBER_OF]-()-[:MEMBER_OF]-(connected)
RETURN
    connected

Voila!

P.S. If you want to query the graph yourself simply type Cypher queries below: