问题
I have 2 nodes named User_node and Article_node which are related by the relation
article_node --> "Written_By" --> user_node
How do i get all the article nodes that are written by a given user node?
回答1:
I'm assuming you are using the embedded neo4j and thus have an object of type org.neo4j.graphdb.Node
. Node
has the method getRelationships
with several overloads, but the one that takes varargs of RelationshipType
should work for you. To get all the Node
objects connected to your starting node, you would have to write something like this (not tested):
// we use scala, so let's make our code pretty ;-)
import collection.JavaConverters._
val author = db.getNodeById(nodeId)
// getRelationships returns an Iterable[Relationship]
val rels = author.getRelationships(DynamicRelationshipType.withName("Written_By"))
// get the article node from the Relationship object
val articles = rels.asScala.map(_.getOtherNode(author))
来源:https://stackoverflow.com/questions/15565246/retrieving-neo4j-nodes-related-to-a-given-node-using-scala