2

我有 2 个名为 User_node 和 Article_node 的节点,它们由关系相关

article_node --> "Written_By" --> user_node

如何获取给定用户节点写入的所有文章节点?

4

1 回答 1

4

我假设您使用的是嵌入式 neo4j,因此有一个 type 的对象org.neo4j.graphdb.NodeNode具有getRelationships具有多个重载的方法,但是采用可变参数的方法RelationshipType应该适合您。要将所有Node对象连接到您的起始节点,您必须编写如下内容(未经测试):

// 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))
于 2013-03-22T08:35:20.873 回答