Retrieving neo4j nodes related to a given node using scala

孤人 提交于 2019-12-10 10:56:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!