Graphing collaborators in Neo4j

时间:2016-04-04 18:01:56

标签: neo4j graph-databases

I have a simple graph database of people and projects. The only relations are who worked on what projects. How can I find and graph the project collaborations between people?

I'm hoping to end up with a graph of just people, with connecting lines for collaborations.

I tried this query, but it only returns nodes, not relations:

MATCH (valjean:person {person_id: "24601"})-[:WORKED_ON]->(project)<-[:WORKED_ON]-(collaborators)
RETURN valjean, collaborators;

1 个答案:

答案 0 :(得分:0)

You need to add a variable to the relation, and return it - like so:

MATCH (valjean:person {person_id: "24601"})-[r1:WORKED_ON]->(project)<-[r2:WORKED_ON]-(collaborators)
RETURN valjean, r1, r2, collaborators;

I added "r1" and "r2" to report the relations as well as the nodes.

Here's another way to do the same thing:

MATCH (valjean:person {person_id: "24601"})-[r1:WORKED_ON]->(project)
MATCH (collaborators:person)-[r2:WORKED_ON]->(project)
RETURN valjean, r1, r2, collaborators;

Using the "movie" example, here's how to find everybody else that was part of a movie project other than Halle Berry:

match (p1:Person {name: "Halle Berry"})-[r]-(m:Movie {title: "Cloud Atlas"})
match (m)-[p]-(p2:Person)
where p1 <> p2
return m, p, p2

If the objective is to get the result in a "report" format, this'll give you what you're looking for - it won't give you a graph though.

match (p1:Person {name: "Halle Berry"})-[r]-(m:Movie {title: "Cloud Atlas"})
match (m)-[p]-(p2:Person)
where p1 <> p2
return p1, "WORKED_WITH", p2