我一直在尝试将neography用于以下基本用例,但似乎无法让它工作:
我按照这里的例子:https://maxdemarzi.com/2012/01/04/getting-started-with-ruby-and-neo4j/
我尝试了以下代码:
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
类型的关系:
johnathan.incoming
=> #<Neography::NodeTraverser:0x0000000133f1c0 @from=#<Neography::Node name="Johnathan">, @order="depth first", @uniqueness="none", @relationships=[{"type"=>"", "direction"=>"in"}]>
我尝试了relationships
:
2.2.1 :060 > johnathan.incoming.relationships
=> [{"type"=>"", "direction"=>"in"}]
我的期望是看"type"=>":friends"
,但我不会。
但是,当我尝试以下操作时,我会这样做,但它对我的用例不起作用,因为我想知道这些关系是什么,而不事先知道它们是什么:
2.2.1 :061 > johnathan.incoming(:friends).relationships
=> [{"type"=>"friends", "direction"=>"in"}]
第二个用例是实际检索节点,这确实有效。
问题: 如何获得与任何给定节点关联的关系类型?
我想我已经接近搞清楚了:
johnathan.rels.map{|n| n}.first.rel_type
=> "friends"
答案 0 :(得分:0)
你是对的,几乎就在那里。这方面的文档位于https://github.com/maxdemarzi/neography/wiki/Phase-2-Node-relationships#retrieval-by-type的底部,但基本上是:
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 API来说是一个很好的改进。
Java API中存在该功能,请参阅https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/graphdb/Node.html#getRelationshipTypes--