首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用neography来查找给定节点的关系类型和节点?

如何使用neography来查找给定节点的关系类型和节点?
EN

Stack Overflow用户
提问于 2017-04-26 04:12:34
回答 1查看 39关注 0票数 0

我一直试图在以下基本用例中使用neography,但似乎无法让它工作:

  1. 对于给定的节点,请告诉我该节点的所有关联关系。
  2. 对于给定节点和特定关系,返回该关系中的一个或多个节点?

我学习了这里的例子:https://maxdemarzi.com/2012/01/04/getting-started-with-ruby-and-neo4j/

我尝试了以下代码:

代码语言:javascript
复制
def create_person(name)
  Neography::Node.create("name" => name)
end

johnathan = create_person('Johnathan')
mark      = create_person('Mark')
phil      = create_person('Phil')
mary      = create_person('Mary')
luke      = create_person('Luke')

johnathan.both(:friends) << mark

首先,我希望看到传入的相关关系。我的期望是看到与:friends类型的关系

代码语言:javascript
复制
johnathan.incoming
 => #<Neography::NodeTraverser:0x0000000133f1c0 @from=#<Neography::Node name="Johnathan">, @order="depth first", @uniqueness="none", @relationships=[{"type"=>"", "direction"=>"in"}]> 

我试过relationships

代码语言:javascript
复制
2.2.1 :060 > johnathan.incoming.relationships
 => [{"type"=>"", "direction"=>"in"}] 

我的期望是看到"type"=>":friends",但我没有。

然而,当我尝试以下内容时,我会这样做,但它对我的用例不起作用,因为我想知道这些关系是什么,而不事先知道它们是什么:

代码语言:javascript
复制
2.2.1 :061 > johnathan.incoming(:friends).relationships
 => [{"type"=>"friends", "direction"=>"in"}] 

第二个用例是实际检索节点,这是可行的。

问题:如何获得与任何给定节点相关联的关系类型?

我想我已经快搞清楚了:

代码语言:javascript
复制
johnathan.rels.map{|n| n}.first.rel_type
 => "friends"
EN

回答 1

Stack Overflow用户

发布于 2017-04-26 04:54:44

你说得对,快到了。这方面的文档位于https://github.com/maxdemarzi/neography/wiki/Phase-2-Node-relationships#retrieval-by-type的底部,但基本上:

代码语言:javascript
复制
n1 = johnathan

n1.rels                            # Get node relationships
n1.rels(:friends)                  # Get friends relationships
n1.rels(:friends).outgoing         # Get outgoing friends relationships
n1.rels(:friends).incoming         # Get incoming friends relationships
n1.rels(:friends, :work)           # Get friends and work relationships
n1.rels(:friends, :work).outgoing  # Get outgoing friends and work relationships

据我所知,无法获得与我连接的所有关系类型,但这将是对Neo4j REST的一个很好的改进。

该功能存在于Java中,请参阅https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/graphdb/Node.html#getRelationshipTypes--

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43624904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档