This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

createRelation

Create relationships between nodes in Neo4j database

Syntax

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

Description

example

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

example

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

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 Neo4j database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the Person node label 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';
relationinfo = createRelation(neo4jconn,startnode,endnode,relationtype)
relationinfo = 

  Neo4jRelation with properties:

      RelationID: 31
    RelationData: [1×1 struct]
     StartNodeID: 39
    RelationType: 'works with'
       EndNodeID: 40

relationinfo is a Neo4jRelation object with these properties:

  • Relationship identifier

  • Relationship data

  • Start node identifier

  • Relationship type

  • End node identifier

Display the relationship type.

relationinfo.RelationType
ans =

    'works with'

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

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 values 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 node 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 Neo4j database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the Person node label 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          
          ___________    ______________    _________    ____________    __________________________________

    33        41         'studies with'       42        [1×1 struct]    [1x1 database.neo4j.Neo4jRelation]
    32        41         'works with'          9        [1×1 struct]    [1x1 database.neo4j.Neo4jRelation]

relationinfo is a table with these variables:

  • Start node identifier

  • Relationship type

  • End node identifier

  • Relationship properties

  • Neo4jRelation object

Display the Neo4jRelation object for the first relationship.

relation = relationinfo.RelationObject(1)
relation = 

  Neo4jRelation with properties:

      RelationID: 33
    RelationData: [1×1 struct]
     StartNodeID: 41
    RelationType: 'studies with'
       EndNodeID: 42

relation is a Neo4jRelation object with these properties:

  • Relationship identifier

  • Relationship data

  • Start node identifier

  • Relationship type

  • End node identifier

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 Neo4j database using the Neo4j database connection. Use the 'Labels' name-value pair argument to specify the Person node label 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. These nodes represent two colleagues that 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';
relationinfo = createRelation(neo4jconn,startnode,endnode,relationtype, ...
    'Properties',properties)
relationinfo = 

  Neo4jRelation with properties:

      RelationID: 34
    RelationData: [1×1 struct]
     StartNodeID: 43
    RelationType: 'works with'
       EndNodeID: 44

relationinfo is a Neo4jRelation object with these properties:

  • Relationship identifier

  • Relationship data

  • Start node identifier

  • Relationship type

  • End node identifier

Display the properties of the relationship.

relationinfo.RelationData
ans = 

  struct with fields:

    StartDate: '09/01/2017'
      Project: 'Database'

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 this object for one node or an array of these 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 that the createRelation function creates 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 this object for one node or an array of these 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 that the createRelation function creates 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 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, each field and its corresponding value convert to a property and its corresponding value in the database relationship.

When you specify a table with one row, each variable and its corresponding value convert to a property and its corresponding value in the database relationship.

The createRelation function 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 resulting relationship does not contain this property.

Data Types: struct | table | cell

Output Arguments

collapse all

Relationship, returned as a Neo4jRelation object or table that contains relationship information. A Neo4jRelation object represents a single relationship. A table represents multiple relationships and contains these variables:

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

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

  • EndNodeID — Node identifier for 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