createRelation

Create relationships between nodes in Neo4j database

Syntax

createRelation(neo4jconn,startnode,endnode,relationtype)
createRelation(neo4jconn,startnode,endnode,relationtype,'Properties',properties)
relationinfo = createRelation(___)

Description

example

createRelation(neo4jconn,startnode,endnode,relationtype) creates a single relationship or multiple relationships between the start nodes and end nodes with specified relationship types by using the Neo4j® database connection.

example

createRelation(neo4jconn,startnode,endnode,relationtype,'Properties',properties) specifies the properties of the new relationships.

example

relationinfo = createRelation(___) returns relationship information as a Neo4jRelation object or a table using any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create a single relationship between two nodes in a Neo4j® database and display the relationship.

Create a Neo4j database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Create two nodes in the database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the label Person for each node.

label = 'Person';
startnode = createNode(neo4jconn,'Labels',label);
endnode = createNode(neo4jconn,'Labels',label);

Create a relationship between the two nodes using the Neo4j database connection. Specify the relationship type as works with.

relationtype = 'works with';
createRelation(neo4jconn,startnode,endnode,relationtype)

Search for the new relationship and display its relationship type.

direction = "out";
relinfo = searchRelation(neo4jconn,startnode,direction, ...
    'RelationTypes',relationtype,'Distance',2);
relinfo.Relations.RelationType
ans = 1×1 cell array
    {'works with'}

Close the database connection.

close(neo4jconn)

Create a single relationship with properties between two nodes in a Neo4j® database and display the properties.

Create a Neo4j database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Create two nodes in the database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the label Person for each node.

label = 'Person';
startnode = createNode(neo4jconn,'Labels',label);
endnode = createNode(neo4jconn,'Labels',label);

Create a relationship between the two nodes using the Neo4j database connection. The nodes represent two colleagues who started working together on a project named Database on September 1, 2017. Specify the relationship type as works with. Specify the project name and start date as properties of the relationship by using the properties structure.

relationtype = 'works with';
properties.Project = 'Database';
properties.StartDate = '09/01/2017';
createRelation(neo4jconn,startnode,endnode,relationtype, ...
    'Properties',properties)

Search for the new relationship and display its properties.

direction = "out";
relinfo = searchRelation(neo4jconn,startnode,direction, ...
    'RelationTypes',relationtype,'Distance',2);
relinfo.Relations.RelationData{1}
ans = struct with fields:
    StartDate: '09/01/2017'
      Project: 'Database'

Close the database connection.

close(neo4jconn)

Create two relationships between nodes in a Neo4j® database and display the relationships.

Assume that you have graph data stored in a Neo4j database that represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with a value ranging from User1 through User7. Each relationship has the type knows.

Create a Neo4j database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Search for the node with the label Person and the property key name set to the value User7 by using the Neo4j database connection.

nlabel = 'Person';
user7 = searchNode(neo4jconn,nlabel,'PropertyKey','name', ...
    'PropertyValue','User7');

Create two nodes in the database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the label Person for each node.

label = 'Person';
user8 = createNode(neo4jconn,'Labels',label);
user9 = createNode(neo4jconn,'Labels',label);

Create two relationships using the Neo4j database connection. Specify the relationship types as works with and studies with. The two relationships are:

  • User8 works with User7

  • User8 studies with User9

startnode = [user8,user8];
endnode = [user7,user9];
relationtype = {'works with','studies with'};
relationinfo = createRelation(neo4jconn,startnode,endnode,relationtype)
relationinfo=2×5 table
         StartNodeID     RelationType     EndNodeID    RelationData                RelationObject             
         ___________    ______________    _________    ____________    _______________________________________

    9         6         'works with'          9        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    7         6         'studies with'        7        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

relationinfo is a table with these variables:

  • Start node identifier

  • Relationship type

  • End node identifier

  • Relationship data

  • Neo4jRelation object

Display the Neo4jRelation object for the first relationship.

relation = relationinfo.RelationObject(1)
relation = 
  Neo4jRelation with properties:

      RelationID: 9
    RelationData: [1×1 struct]
     StartNodeID: 6
    RelationType: 'works with'
       EndNodeID: 9

relation is a Neo4jRelation object with these properties:

  • Relationship identifier

  • Relationship data

  • Start node identifier

  • Relationship type

  • End node identifier

Close the database connection.

close(neo4jconn)

Input Arguments

collapse all

Neo4j database connection, specified as a Neo4jConnect object created with the function neo4j.

Start node, specified as a numeric scalar, numeric vector, Neo4jNode object, or Neo4jNode object array. To specify database nodes using node identifiers, use a numeric scalar for one node or a numeric vector for multiple nodes. To specify database nodes as Neo4jNode objects, use the object for one node or an array of the objects for multiple nodes.

The number of start nodes must match the number of end nodes in endnode.

The number of start nodes equals the number of relationships created by the createRelation function in the Neo4j database.

Example: 8

End node, specified as a numeric scalar, numeric vector, Neo4jNode object, or Neo4jNode object array. To specify database nodes using node identifiers, use a numeric scalar for one node or a numeric vector for multiple nodes. To specify database nodes as Neo4jNode objects, use the object for one node or an array of the objects for multiple nodes.

The number of end nodes must match the number of start nodes in startnode.

The number of end nodes equals the number of relationships created by the createRelation function in the Neo4j database.

Example: 9

Relationship type, specified as a character vector, string scalar, cell array of character vectors, or string array. To specify one relationship type, use a character vector or string scalar. To specify multiple relationship types, use a cell array of character vectors or a string array.

If you specify only one relationship type, all relationships must have the same type. Otherwise, the number of relationship types in this input argument must match the number of nodes in startnode and endnode.

Example: 'knows'

Data Types: char | string | cell

Relationship properties, specified as a structure, structure array, table, or cell array of structures.

When you specify a structure, the createRelation function converts each field and its corresponding value to a property and its corresponding value in the database relationship. When you specify a table with one row, the function converts each variable and its corresponding value to a property and its corresponding value in the database relationship.

The createRelation function also sets the RelationObject variable of the relationinfo output argument to the Neo4jRelation object, which contains the relationship information.

For multiple relationships, specify a structure array or table with multiple rows.

For multiple relationships with different properties, specify a cell array of structures.

Note

If a property is missing its corresponding value, then the updated relationship does not contain this property.

Data Types: struct | table | cell

Output Arguments

collapse all

Relationship information, returned as a Neo4jRelation object for one relationship or as a table for multiple relationships.

For multiple relationships, the table contains these variables:

  • StartNodeID — Node identifier of the start node for each matched relationship

  • RelationType — Character vector that denotes the relationship type for each matched relationship

  • EndNodeID — Node identifier of the end node for each matched relationship

  • RelationData — Structure array that contains property keys associated with each matched relationship

  • RelationObjectNeo4jRelation object for each matched relationship

The row names in the table are Neo4j relationship identifiers.

Introduced in R2018a