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.

Returning graph

gdb_ONE
Node Link

A graph that has uneven depth i.e it might have 4 nodes in a path and 3 nodes in a path. It looks like this

Eg:A-B-C-D and A-E-F (the relationships between them can be of different types and there can be multiple such paths from A but the limit will be 4)

return this graph in a certain format 

1st node | Relationship name | 2nd node | Relationship name | 3rd node | Relationship name | 4th node

A         |     x                               |  B               | x2                              | C             | x3                           | D

A        |       x1                            | E               | x                                 | F             | Null                         | Null

As mentioned above for missing nodes the returned table will have null

The nodes are also have a property "lvl" in case you need it. A is lvl 1, (B,E) is lvl 2 ... 

 

1 ACCEPTED SOLUTION

You stated it is a graph, so I am assuming each path ends on a leave node, i.e. only has one relationship. I assumed you only wanted full paths, i.e. ones that terminate on a leave node (that is what the 'call' subquery is used for, to filter out partial paths).

Try the following, but change the root node 'where' condition to match your selection criteria and change the node property name from 'name' to what property you want to output. 

match(root) where id(root) = 26
match p=(root)-[*..4]-(end)
call {
   with end
   match (end)--()
   return count(*) as cnt
}
with p, cnt
where cnt = 1
with nodes(p) as nodes, relationships(p) as relationships
return nodes[0].name as `1st node`, type(relationships[0]) as `1st Relationship`,
nodes[1].name as `2nd node`, type(relationships[1]) as `2nd Relationship`,
nodes[2].name as `3rd node`, type(relationships[2]) as `3rd Relationship name`,
nodes[3].name as `4th node`

 

View solution in original post

4 REPLIES 4

You stated it is a graph, so I am assuming each path ends on a leave node, i.e. only has one relationship. I assumed you only wanted full paths, i.e. ones that terminate on a leave node (that is what the 'call' subquery is used for, to filter out partial paths).

Try the following, but change the root node 'where' condition to match your selection criteria and change the node property name from 'name' to what property you want to output. 

match(root) where id(root) = 26
match p=(root)-[*..4]-(end)
call {
   with end
   match (end)--()
   return count(*) as cnt
}
with p, cnt
where cnt = 1
with nodes(p) as nodes, relationships(p) as relationships
return nodes[0].name as `1st node`, type(relationships[0]) as `1st Relationship`,
nodes[1].name as `2nd node`, type(relationships[1]) as `2nd Relationship`,
nodes[2].name as `3rd node`, type(relationships[2]) as `3rd Relationship name`,
nodes[3].name as `4th node`

 

I don't know how you would format the result in a tabular form, as you need to enumerate the columns in the return statement, so you need to know a priori how many there are. You could do something where you return a json object with the data. 

Ok got it thanks 🙂 

@glilienfield  how would the updated query look like if there were multiple relationships between the nodes and we wouldnt want duplicated entries in the tabular format? Eg A=[:x|x1]=>B  how to get one single row entry of A,B, {x},{x1}(with their types and properties) rather than two rows for the same example mentioned above

 

You can try the following. It groups the rows by node names and then collects the set of relationships for each path in maps. The relationship maps for each path are returned in a list. The grouping assumes the node names are unique, so the paths will be correctly grouped by common node names.

match(root) where id(root) = 26
match p=(root)-[*..4]-(end)
call {
   with end
   match (end)--()
   return count(*) as cnt
}
with p, cnt
where cnt = 1
with nodes(p) as nodes, relationships(p) as rels
return nodes[0].name as `1st node`, nodes[1].name as `2nd node`, nodes[2].name as `3rd node`, nodes[3].name as `4th node`, collect({`1st Relationship`: type(rels[0]), `2nd Relationship`: type(rels[1]), `3rd Relationship name`: type(rels[2])}) as relationships