Satchel
Satchel

Reputation: 16734

how do I use neography to find the relationship type and nodes for a given node?

I have been trying to use neography for the following basic use case, but can't seem to get it to work:

  1. For a given node, tell me all the associated relationships for that node.
  2. For a given node and a specific relationship, return the node or nodes in that relationships?

I followed the examples from here: https://maxdemarzi.com/2012/01/04/getting-started-with-ruby-and-neo4j/

I tried the following code:

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

First, I want to see the associated relationships that are incoming. My expectation is to see relationship with type :friends:

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

I tried relationships:

2.2.1 :060 > johnathan.incoming.relationships
 => [{"type"=>"", "direction"=>"in"}] 

My expectation would be to see "type"=>":friends" but I don't.

However, when I try the following, I do, but it doesn't work for my use case since I want to know what the relationships are without knowing in advance what they are:

2.2.1 :061 > johnathan.incoming(:friends).relationships
 => [{"type"=>"friends", "direction"=>"in"}] 

Second use case is to actually retrieve the nodes, which does work.

Question: How can I get the types of relationships associated for any given node?

I think I am close to figuring it out:

johnathan.rels.map{|n| n}.first.rel_type
 => "friends"

Upvotes: 0

Views: 46

Answers (1)

Max De Marzi
Max De Marzi

Reputation: 1108

You are right, almost there. The documentation for this is at the bottom of https://github.com/maxdemarzi/neography/wiki/Phase-2-Node-relationships#retrieval-by-type but basically:

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

There is no way to get just what are all the relationship types connected to me as far as I know, but that would be a good improvement in the Neo4j REST API.

The functionality exists in the Java API, see https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/graphdb/Node.html#getRelationshipTypes--

Upvotes: 0

Related Questions