cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return list of Nodes and Relationships from HTTP API

Is there a way to return the result of query as list of Nodes and Relationships ?
e.g.
{
    "relationships":[ {} , {} , {} ] ,
    "nodes":[ {} , {} ]
}
I noticed this parameter:
resultDataContents = "graph"  //, "rows"

But this still returns long list of rows with sub lists of nodes and edges. 

5 REPLIES 5

From what I read, if you don't specify a ''resultDataContents" value, it will default to rows. This will return the data as rows defined in your query. You can try something like this for a 'return' statement:

{
  "statements": [
    {
      "statement": "match p=(n:Entity)-[:REL]->(m:Entity) Return {nodes: nodes(p), relationships: relationships(p)} as result",
    }
  ]
}

You will get results in the 'data' container, which is an list containing your row data. The return structure is a little non-intuitive, so send a request and review the response to understand the structure. 

Thanks @glilienfield Ninja ,  This does not seem to work:
Type mismatch: expected Path but was Node (line 1, column 32 (offset: 31))\n"match (p) RETURN {nodes: nodes(p), relationships: relationships(p)} as result"

That was just an example.  I can see if I can help if with your requirement if you share your query. 

Thanks @glilienfield 

Whole graph:
MATCH ( x ) return x 
Or subgraphs: 
MATCH ( x :Unit  )-[  r1 :RUNS ]-( y :Job )-[  r2 :AWAITS ]-( z :Auditor ) RETURN x, r1 , y , r2 , z 

Ideally is there a way to return any query as collection of Nodes and Relationships?

Not really.  You can return the nodes and relationships along each matched path. Using your example above, the query returns the nodes and relationships for each path 'p'. 

MATCH p=( x :Unit  )-[  r1 :RUNS ]-( y :Job )-[  r2 :AWAITS ]-( z :Auditor ) RETURN nodes(p), relationships(p)

 You can use an apoc function to put all the nodes and relationships into one collection for each across all the paths as follows:

MATCH p=( x :Unit  )-[  r1 :RUNS ]-( y :Job )-[  r2 :AWAITS ]-( z :Auditor ) 
WITH nodes(p) as nodes, relationships(p) as relationships
RETURN apoc.coll.flatten(collect(nodes)) as allNodes, apoc.coll.flatten(collect(relationships)) as allRel

If you are starting from on root node and you want all the nodes and relationships in its graph, then you can use apoc.path.subgraphAll to get the collection of nodes and relationships. 

https://neo4j.com/labs/apoc/4.1/overview/apoc.path/apoc.path.subgraphAll/ 

'Match(x) return x' does not give you the whole graph, just the nodes. It may look like the whole graph when viewed in Neo4j Desktop because desktop will show all the relationships between the display nodes by default. You can turn this feature off so the display only shows relationships returned in each query.