使用Scala检索与给定节点相关的neo4j节点 [英] Retrieving neo4j nodes related to a given node using scala
问题描述
我有2个名为User_node和Article_node的节点,它们通过这种关系相关联
I have 2 nodes named User_node and Article_node which are related by the relation
article_node->"Written_By"-> user_node
article_node --> "Written_By" --> user_node
如何获取由给定用户节点编写的所有文章节点?
How do i get all the article nodes that are written by a given user node?
推荐答案
我假设您正在使用嵌入式neo4j,因此具有类型为org.neo4j.graphdb.Node
的对象. Node
的方法getRelationships
具有几个重载,但是采用RelationshipType
的varargs的方法应该适合您.要将所有Node
对象连接到起始节点,您必须编写类似以下内容(未经测试):
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))
这篇关于使用Scala检索与给定节点相关的neo4j节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!