使用Scala检索与给定节点相关的neo4j节点 [英] Retrieving neo4j nodes related to a given node using scala

查看:104
本文介绍了使用Scala检索与给定节点相关的neo4j节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆