Reputation: 1684
I've wrote a cypher query to show the relationship between two movie nodes:
MATCH (m1:Movie)-[r*1..2]-(m2:Movie)
where m1.movieID = '1' AND m2.movieID = '2'
RETURN r
LIMIT 25
It's returns this in the Neo4j browser, and this is exactly what I want to create in d3.js. Eventually the movieIDs will be parameters, so the results will be different for each pair of movies.
I've been following the Neo4j-core Ruby example seen here to create my own graph, but my problem is that I can't RETURN
common nodes between the two moves, i.e Tag
/Country
nodes.
How should I solve this problem? Should I create a query to return the two movies, and another query to return the common nodes between them? How would I write this latter query? I'm really stuck on how I can visualise this relationship...
Upvotes: 1
Views: 807
Reputation: 10856
You could return the movies with the relationships:
MATCH (m1:Movie)-[r*1..2]-(m2:Movie)
WHERE m1.movieID = '1' AND m2.movieID = '2'
RETURN m1, m2, r
LIMIT 25
Though that might give you duplication. If you're going to be displaying this with D3 probably what you want is all of the relationships involved and their start/end nodes. To do that you could do:
MATCH (m1:Movie)-[rels*1..2]-(m2:Movie)
WHERE m1.movieID = '1' AND m2.movieID = '2'
RETURN rels
UNWIND rels AS r
WITH DISTINCT r AS rel
WITH startnode(rel) AS startnode, endnode(rel) AS endnode, rel
LIMIT 25
Generally what you want to end up with is a collection of nodes and relationships involved. So with the result of that query you might do:
result = neo4j_session.query(query_string)
data = {nodes: [], relationships: []}
result.each do |row|
data[:nodes] << row.startnode
data[:nodes] << row.endnode
data[:relationships] << row.rel
end
data[:nodes].uniq!
Upvotes: 0
Reputation: 18002
Perhaps you should try matching the path rather than the relationships as you are right now. Something like:
MATCH p=(m1:Movie { movieID: 1 } )-[r*1..2]-(m2:Movie { movieID: 2 } )
RETURN p;
You can then use collection functions on the path object p
to do whatever you like with it. For example, if you want to get the inner nodes, you can use nodes(p)
and simply remove the first and last nodes (which would be m1
and m2
) and get the nodes along the path.
Upvotes: 2