Main Content

searchRelation

Search relationships for Neo4j database node

Description

example

relinfo = searchRelation(neo4jconn,nodeinfo,direction) returns relationship information for the origin node nodeinfo and relationship direction using a Neo4j® database connection. The search starts from the origin node. To find an origin node, use searchNode or searchNodeByID.

example

relinfo = searchRelation(neo4jconn,nodeinfo,direction,Name,Value) specifies additional options using one or more name-value pair arguments. For example, 'RelationTypes',{'works with'} returns information for relationships that have the type works with.

Examples

collapse all

Search for information about a relationship in a Neo4j® database and display the information.

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 =

     []

Retrieve the origin node nodeinfo using the Neo4j database connection and the node identifier 3.

nodeid = 3;

nodeinfo = searchNodeByID(neo4jconn,nodeid);

Search for incoming relationships using the Neo4j database connection and the origin node nodeinfo.

direction = 'in';

relinfo = searchRelation(neo4jconn,nodeinfo,direction)
relinfo = struct with fields:
       Origin: 3
        Nodes: [2×3 table]
    Relations: [1×5 table]

relinfo is a structure that contains the results of the search:

  • Origin — The node identifier for the specified origin node

  • Nodes — A table containing all start and end nodes for each matched relationship

  • Relations — A table containing all matched relationships

Access the table of nodes.

relinfo.Nodes
ans=2×3 table
         NodeLabels      NodeData                  NodeObject             
         __________    ____________    ___________________________________

    1     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    3     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Access the table of relationships.

relinfo.Relations
ans=1×5 table
         StartNodeID    RelationType    EndNodeID    RelationData                RelationObject             
         ___________    ____________    _________    ____________    _______________________________________

    3         1           'knows'           3        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Close the database connection.

close(neo4jconn)

Search for information about relationships in a Neo4j® database and display the information. Specify the relationship type and distance to search.

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 =

     []

Retrieve the origin node nodeinfo using the Neo4j database connection and the node identifier 3.

nodeid = 3;

nodeinfo = searchNodeByID(neo4jconn,nodeid);

Search for incoming relationships using the Neo4j database connection and the origin node nodeinfo. Refine the search by filtering for the relationship type knows and for nodes at a distance of two or fewer.

direction = 'in';
reltypes = {'knows'};

relinfo = searchRelation(neo4jconn,nodeinfo,direction, ...
    'RelationTypes',reltypes,'Distance',2)
relinfo = struct with fields:
       Origin: 3
        Nodes: [4×3 table]
    Relations: [3×5 table]

relinfo is a structure that contains the results of the search:

  • Origin — The node identifier for the specified origin node

  • Nodes — A table containing all start and end nodes for each matched relationship

  • Relations — A table containing all matched relationships

Access the table of nodes.

relinfo.Nodes
ans=4×3 table
         NodeLabels      NodeData                  NodeObject             
         __________    ____________    ___________________________________

    0     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    1     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    2     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    3     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Access the table of relationships.

relinfo.Relations
ans=3×5 table
         StartNodeID    RelationType    EndNodeID    RelationData                RelationObject             
         ___________    ____________    _________    ____________    _______________________________________

    3         1           'knows'           3        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    2         2           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    1         0           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Close the database connection.

close(neo4jconn)

Search for information about outgoing relationships in a Neo4j® database. Return the information as a directed graph and display the edges and nodes of the graph.

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 =

     []

Retrieve the origin node nodeinfo using the Neo4j database connection and the node identifier 3.

nodeid = 3;
nodeinfo = searchNodeByID(neo4jconn,nodeid);

Search for outgoing relationships using the Neo4j database connection and the origin node nodeinfo. Return relationship information as a directed graph by using the 'DataReturnFormat' name-value pair argument with the value 'digraph'.

direction = 'out';
relinfo = searchRelation(neo4jconn,nodeinfo,direction, ...
    'DataReturnFormat','digraph')
relinfo = 
  digraph with properties:

    Edges: [2×3 table]
    Nodes: [3×3 table]

Display the edges of the directed graph.

relinfo.Edges
ans=2×3 table
       EndNodes       RelationType    RelationData
    ______________    ____________    ____________

    {'3'}    {'4'}     {'knows'}      {1×1 struct}
    {'3'}    {'5'}     {'knows'}      {1×1 struct}

Display the nodes of the directed graph.

relinfo.Nodes
ans=3×3 table
    Name     NodeLabels      NodeData  
    _____    __________    ____________

    {'3'}    {'Person'}    {1×1 struct}
    {'4'}    {'Person'}    {1×1 struct}
    {'5'}    {'Person'}    {1×1 struct}

Close the database connection.

close(neo4jconn)

Input Arguments

collapse all

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

Origin node information, specified as a Neo4jNode object or numeric scalar that denotes a node identifier.

Data Types: double

Relationship direction, specified as either 'in' for an incoming relationship or 'out' for an outgoing relationship. The relationships are associated with the specified origin node.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: relinfo = searchRelation(neo4jconn,nodeinfo,'in','RelationTypes',{'knows'},'Distance',2) returns the relationship information for the incoming relationships, which have the relationship type knows and are two or fewer nodes away from the origin node.

Relationship types, specified as a comma-separated pair consisting of 'RelationTypes' and a character vector, string scalar, cell array of character vectors, or string array. To search for relationships using only one relationship type, use a character vector or string scalar. To search for relationships using numerous relationship types, use a cell array of character vectors or string array.

Example: 'RelationTypes',{'knows'}

Data Types: char | cell | string

Node distance, specified as a comma-separated pair consisting of 'Distance' and a positive numeric scalar. For example, if the node distance is three, searchRelation returns information for nodes that are three or fewer nodes away from the origin node nodeinfo.

Example: 'Distance',3

Data Types: double

Data return format, specified as the comma-separated pair consisting of 'DataReturnFormat' and the value 'struct' for a structure or 'digraph' for a digraph object. Specify this argument to return relationship information as a digraph object.

Output Arguments

collapse all

Relationship information in the Neo4j database that matches the search criteria from the origin node nodeinfo, returned as a structure with these fields.

FieldDescription

Origin

Node identifier of the origin node nodeinfo.

Nodes

Table that contains node information for each node in the Relations table. The Nodes table contains these variables:

  • NodeLabels — Character vector that denotes the node label for each matched database node

  • NodeData — Structure array that contains node information such as property keys for each matched database node

  • NodeObjectNeo4jNode object that represents each matched database node

The row names in the table are Neo4j node identifiers of the matched database nodes.

Relations

Table that contains relationship information for the nodes in the Nodes table. The Relations table 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 that represents each matched relationship

The row names in the table are Neo4j relationship identifiers.

Note

When you use the 'DataReturnFormat' name-value pair argument with the value 'digraph', the searchRelation function returns relationship information in a digraph object. The resulting digraph object contains the same data as the digraph object created when you execute the neo4jStruct2Digraph function using the relinfo output argument.

Version History

Introduced in R2016b