Main Content

searchGraph

Search for subgraph or entire graph in Neo4j database

Description

example

graphinfo = searchGraph(neo4jconn,criteria) returns graph information based on the search criteria using a Neo4j® database connection. You can search for a subgraph or the entire graph.

example

graphinfo = searchGraph(neo4jconn,criteria,'DataReturnFormat','digraph') returns graph information as a digraph object.

Examples

collapse all

Search for graph information in a Neo4j® database by using node labels and display the information.

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 the graph for all nodes with the label 'Person' using the Neo4j database connection.

nlabel = {'Person'};

graphinfo = searchGraph(neo4jconn,nlabel)
graphinfo = struct with fields:
        Nodes: [7×3 table]
    Relations: [8×5 table]

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

  • All start and end nodes that denote each matched relationship

  • All matched relationships

Access the table of nodes.

graphinfo.Nodes
ans=7×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]
    4     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    5     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    9     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Access property keys for the first node.

graphinfo.Nodes.NodeData{1}
ans = struct with fields:
    name: 'User1'

Access the table of relationships.

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

    1         0           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    0         0           'knows'           2        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    3         1           'knows'           3        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    2         2           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    5         3           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    4         3           'knows'           5        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    6         5           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    8         5           'knows'           9        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Access property keys for the first relationship. The first relationship has no property keys.

graphinfo.Relations.RelationData{1}
ans = struct with no fields.


Search the graph for all node labels in the database.

allnodes = nodeLabels(neo4jconn);

graphinfo = searchGraph(neo4jconn,allnodes);

Close the database connection.

close(neo4jconn)

Search for graph information in a Neo4j® database by using the relationship type and display the information.

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 the graph for the relationship type 'knows' using the Neo4j database connection.

reltype = {'knows'};

graphinfo = searchGraph(neo4jconn,reltype)
graphinfo = struct with fields:
        Nodes: [7×3 table]
    Relations: [8×5 table]

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

  • All start and end nodes that denote each matched relationship

  • All matched relationships

Access the table of nodes.

graphinfo.Nodes
ans=7×3 table
         NodeLabels      NodeData                  NodeObject             
         __________    ____________    ___________________________________

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

Access the table of relationships.

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

    0         0           'knows'           2        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    1         0           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    2         2           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    3         1           'knows'           3        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    4         3           'knows'           5        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    5         3           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    6         5           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    8         5           'knows'           9        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Search the graph for all relationship types in the database.

allreltypes = relationTypes(neo4jconn);

graphinfo = searchGraph(neo4jconn,allreltypes);

Close the database connection.

close(neo4jconn)

Search for graph information in a Neo4j® database by using node labels. 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 =

     []

Search the graph for all nodes with the node label Person using the Neo4j database connection. Return graph information as a directed graph by using the 'DataReturnFormat' name-value pair argument with the value 'digraph'.

nlabel = "Person";
graphinfo = searchGraph(neo4jconn,nlabel, ...
    'DataReturnFormat','digraph');

Display the edges of the directed graph.

graphinfo.Edges
ans=8×3 table
       EndNodes       RelationType    RelationData
    ______________    ____________    ____________

    {'0'}    {'1'}     {'knows'}      {1×1 struct}
    {'0'}    {'2'}     {'knows'}      {1×1 struct}
    {'1'}    {'3'}     {'knows'}      {1×1 struct}
    {'2'}    {'1'}     {'knows'}      {1×1 struct}
    {'3'}    {'4'}     {'knows'}      {1×1 struct}
    {'3'}    {'5'}     {'knows'}      {1×1 struct}
    {'5'}    {'4'}     {'knows'}      {1×1 struct}
    {'5'}    {'9'}     {'knows'}      {1×1 struct}

Display the nodes of the directed graph.

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

    {'0'}    {'Person'}    {1×1 struct}
    {'1'}    {'Person'}    {1×1 struct}
    {'2'}    {'Person'}    {1×1 struct}
    {'3'}    {'Person'}    {1×1 struct}
    {'4'}    {'Person'}    {1×1 struct}
    {'5'}    {'Person'}    {1×1 struct}
    {'9'}    {'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.

Search criteria, specified as a cell array of character vectors or string array. To search by nodes, specify one or more node labels as character vectors in the cell array. To search by relationships, specify one or more relationship types as character vectors in the cell array. Or, specify a string array for multiple node labels or relationship types.

Data Types: cell | string

Output Arguments

collapse all

Graph information in the Neo4j database that matches the search criteria, returned as a structure with these fields.

FieldDescription

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 for each matched database node

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

If criteria contains node labels, the output is automatically sorted by StartNodeID and Label.

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.

If criteria contains relationship types, the output is automatically sorted by RelationID.

Note

When you use the 'DataReturnFormat' name-value pair argument with the value 'digraph', the searchGraph function returns graph 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 graphinfo output argument.

Version History

Introduced in R2016b