Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Relationships Between Nodes in Neo4j with Neo4jClient in C#

I'm working with Neo4j using the .Net Neo4jClient (http://hg.readify.net/neo4jclient/wiki/Home). In my code, nodes are airports and relationships are flights.

If I want to create nodes and relationships at the same time, I can do it with the following code:

Classes

public class Airport
{
    public string iata { get; set; }
    public string name { get; set; }
}

public class flys_toRelationship : Relationship, IRelationshipAllowingSourceNode<Airport>, IRelationshipAllowingTargetNode<Airport>
{
    public static readonly string TypeKey = "flys_to";

    // Assign Flight Properties
    public string flightNumber { get; set; }

    public flys_toRelationship(NodeReference targetNode)
        : base(targetNode)
    { }

    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}

Main

// Create a New Graph Object
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

// Create New Nodes
var lax = client.Create(new Airport() { iata = "lax", name = "Los Angeles International Airport" });
var jfk = client.Create(new Airport() { iata = "jfk", name = "John F. Kennedy International Airport" });
var sfo = client.Create(new Airport() { iata = "sfo", name = "San Francisco International Airport" });

// Create New Relationships
client.CreateRelationship(lax, new flys_toRelationship(jfk) { flightNumber = "1" });
client.CreateRelationship(lax, new flys_toRelationship(sfo) { flightNumber = "2" });
client.CreateRelationship(sfo, new flys_toRelationship(jfk) { flightNumber = "3" });

The problem, however, is when I want to add relationships to already existing nodes. Say I have a graph consisting of only two nodes (airports), say SNA and EWR, and I would like to add a relationship (flight) from SNA to EWR. I try the following and it fails:

// Create a New Graph Object
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

Node<Airport> departure = client.QueryIndex<Airport>("node_auto_index", IndexFor.Node, "iata:sna").First();
Node<Airport> arrival = client.QueryIndex<Airport>("node_auto_index", IndexFor.Node, "iata:ewr").First();
//Response.Write(departure.Data.iata); <-- this works fine, btw: it prints "sna"

// Create New Relationships
client.CreateRelationship(departure, new flys_toRelationship(arrival) { flightNumber = "4" });

The two errors I'm receiving are as follows:

1) Argument 1: cannot convert from 'Neo4jClient.Node' to 'Neo4jClient.NodeReference'

2) The type arguments for method 'Neo4jClient.GraphClient.CreateRelationship(Neo4jClient.NodeReference, TRelationship)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The method the error is referring to is in the following class: http://hg.readify.net/neo4jclient/src/2c5446c17a65d6e5accd420a2dff0089799cbe16/Neo4jClient/GraphClient.cs?at=default

Any ideas?

like image 797
Brent Barbata Avatar asked Jan 11 '13 00:01

Brent Barbata


People also ask

How do you create a relationship between two existing nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

How are relationships stored in Neo4j?

Properties are stored as a linked list of property records, each holding a key and value and pointing to the next property. Each node and relationship references its first property record. The Nodes also reference the first relationship in its relationship chain. Each Relationship references its start and end node.

What are nodes and relationships in Neo4j?

The Neo4j property graph database model consists of: Nodes describe entities (discrete objects) of a domain. Nodes can have zero or more labels to define (classify) what kind of nodes they are. Relationships describes a connection between a source node and a target node.

Can relationships have properties in Neo4j?

The graphs in the Neo4j Graph Data Science Library support properties for relationships. We provide multiple operations to work with the stored relationship-properties in projected graphs. Relationship properties are either added during the graph projection or when using the mutate mode of our graph algorithms.

How to create relationship between nodes in Neo4j?

In Neo4j to create relationship between nodes you have to use the CREATE statement like we used to create nodes. lets create relation between two already created nodes. You can see how easy it is to continue creating more nodes and relationships between them.

How to create a path In Neo4j?

In Neo4j, CREATE statement is used to create a path. A path is formed using continuous relationship. First create a node3 name "Champions_Trophy" to do further operations.

How do I create a relationship between two nodes?

Result 2. Create relationships 2.1. Create a relationship between two nodes To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

What's the latest version of neo4jclient?

The official Neo4jClient build and nuget package is automated via AppVeyor. Version 4.0.0 of Neo4jClient is now the stable version. There have been a lot of changes, additions, removals, so it's likely there will be breaking changes. This isn't an exhaustive list of things you need to do, but I'll try to add things if I've forgotten them.


Video Answer


1 Answers

In your CreateRelationship call you will need to use the node references, not the nodes, so:

client.CreateRelationship(departure.Reference, new flys_toRelationship(arrival.Reference) { flightNumber = "4" });

The reason why your initial creation code works and this didn't is because Create returns you a NodeReference<Airport> (the var is hiding that for you), and the QueryIndex returns a Node<Airport> instance instead.

Neo4jClient predominantly uses NodeReference's for the majority of its operations.

The second error you had was just related to not using the .Reference property as it couldn't determine the types, when you use the .Reference property that error will go away as well.

like image 159
Charlotte Skardon Avatar answered Sep 30 '22 12:09

Charlotte Skardon