Recursive query on Graph

Hi, Regarding the attached image, having only the id of node label A I need to have all the nodes that follow the relation as in the image. Using the following query I could only get one level of the depth (A, B1, C1, and T1), but I need to have all of them.
GRAPH.QUERY TestGraph "MATCH (a: A) WHERE a.id = '6' OPTIONAL MATCH (a)-[:CREATED_FROM]->(b: B) OPTIONAL MATCH (b)-[: CONTAINS]->(c: C) OPTIONAL MATCH (c)-[: CONTAINS]->(t: T) RETURN a, m, c, t"


So how is it possible to catch all graph nodes with such recursive relations? Is it possible to do it in one single query?

Thanks in advance.

On RedisGraph latest version, the following query would create a sample graph:

127.0.0.1:6379> GRAPH.QUERY g "CREATE (a:A {id:'6'})-[:CREATED_FROM]->(b1:B)-[:CONTAINS]->(c1:C)-[:CONTAINS]->(t1:T), (a)-[:CREATED_FROM]->(b2:B)-[:CONTAINS]->(c2:C)-[:CONTAINS]->(t2:T), (a)-[:CREATED_FROM]->(b3:B)-[:CONTAINS]->(c3:C), (a)-[:CREATED_FROM]->(b4:B)"
1) 1) "Labels added: 4"
   2) "Nodes created: 10"
   3) "Properties set: 1"
   4) "Relationships created: 9"
   5) "Cached execution: 0"
   6) "Query internal execution time: 20.284000 milliseconds"

We’re expecting the following “chains”:

(a)-(b1)-(c1)-(t1),
(a)-(b2)-(c2-(t2),
(a)-(b3)-(c3),
(a)-(b4)
127.0.0.1:6379> GRAPH.QUERY g "MATCH (a: A) WHERE a.id = '6' OPTIONAL MATCH (a)-[:CREATED_FROM]->(b: B) OPTIONAL MATCH (b)-[: CONTAINS]->(c: C) OPTIONAL MATCH (c)-[: CONTAINS]->(t: T) RETURN a, b, c, t"
1) 1) "a"
   2) "b"
   3) "c"
   4) "t"
2) 1) 1) 1) 1) "id"
            2) (integer) 0
         2) 1) "labels"
            2) 1) "A"
         3) 1) "properties"
            2) 1) 1) "id"
                  2) "6"
      2) 1) 1) "id"
            2) (integer) 1
         2) 1) "labels"
            2) 1) "B"
         3) 1) "properties"
            2) (empty array)
      3) 1) 1) "id"
            2) (integer) 2
         2) 1) "labels"
            2) 1) "C"
         3) 1) "properties"
            2) (empty array)
      4) 1) 1) "id"
            2) (integer) 3
         2) 1) "labels"
            2) 1) "T"
         3) 1) "properties"
            2) (empty array)
   2) 1) 1) 1) "id"
            2) (integer) 0
         2) 1) "labels"
            2) 1) "A"
         3) 1) "properties"
            2) 1) 1) "id"
                  2) "6"
      2) 1) 1) "id"
            2) (integer) 4
         2) 1) "labels"
            2) 1) "B"
         3) 1) "properties"
            2) (empty array)
      3) 1) 1) "id"
            2) (integer) 5
         2) 1) "labels"
            2) 1) "C"
         3) 1) "properties"
            2) (empty array)
      4) 1) 1) "id"
            2) (integer) 6
         2) 1) "labels"
            2) 1) "T"
         3) 1) "properties"
            2) (empty array)
   3) 1) 1) 1) "id"
            2) (integer) 0
         2) 1) "labels"
            2) 1) "A"
         3) 1) "properties"
            2) 1) 1) "id"
                  2) "6"
      2) 1) 1) "id"
            2) (integer) 7
         2) 1) "labels"
            2) 1) "B"
         3) 1) "properties"
            2) (empty array)
      3) 1) 1) "id"
            2) (integer) 8
         2) 1) "labels"
            2) 1) "C"
         3) 1) "properties"
            2) (empty array)
      4) (nil)
   4) 1) 1) 1) "id"
            2) (integer) 0
         2) 1) "labels"
            2) 1) "A"
         3) 1) "properties"
            2) 1) 1) "id"
                  2) "6"
      2) 1) 1) "id"
            2) (integer) 9
         2) 1) "labels"
            2) 1) "B"
         3) 1) "properties"
            2) (empty array)
      3) (nil)
      4) (nil)
3) 1) "Cached execution: 1"
   2) "Query internal execution time: 2.643000 milliseconds"

Hi SWilly22,
Thanks for your reply.

But it’s not what I needed. Maybe I didn’t explain the issue well.

See, node ‘A’ only has direct CREATED_FROM relation with node ‘B1’. And then node ‘B1’ has CREATED_FROM relation with node ‘B2’. SO so direct relation with node ‘A’ and ‘B2’.
Also, every B-labeled node can have its own C.

To have sample data, the following query is helpful:
GRAPH.QUERY TestGraph "CREATE (a: A {id: '6', name: 'a1'})-[:CREATED_FROM]->(b1: B {name:'b1'}), (b1)-[: CREATED_FROM]->(b2: B {name: 'b2'}), (b2)-[:CREATED_FROM]-> (b3: B {name: 'b3'}), (b1)-[: CONTAINS]->(c1: C {name: c1}), (c1)-[: CONTAINS]->(t1: T {name: 't1'}), (b2)-[: CONTAINS]->(c2: C {name: c2}) "

So the desired query should return the following path
(a)->(b1)->(c1)->(t1)
(a)->(b1)->(b2)->(c2)
(a)->(b1)->(b2)->(b3)

So the idea is starting from (a),

  1. we recursively traverse through CREATED_FROM relations (first between a and its instant B node and then between all B nodes)
  2. if the B nodes have any CONTAINS relation with the C node, that should be returned as well.
  3. If the C node has a CONTAINS relation with the T node, that should be returned as well.

As I already mentioned using the following query I can get only information for the instant B node that A has CREATED_FROM relation with
GRAPH.QUERY TestGraph "MATCH (a: A) WHERE a.id = '6' OPTIONAL MATCH (a)-[:CREATED_FROM]->(b: B) OPTIONAL MATCH (b)-[: CONTAINS]->(c: C) OPTIONAL MATCH (c)-[: CONTAINS]->(t: T) RETURN a, m, c, t"

But I need to have all B nodes recursively.

Thanks

I see, sorry for the miss understanding
Please try:

GRAPH.QUERY g "match p = (a:A {id:'6'})-[*]->(b:B) OPTIONAL MATCH (b)-[: CONTAINS]->(c: C) OPTIONAL MATCH (c)-[: CONTAINS]->(t: T) RETURN nodes(p), c, t"
1) 1) "nodes(p)"
   2) "c"
   3) "t"
2) 1) 1) "[(0), (1)]"
      2) 1) 1) "id"
            2) (integer) 4
         2) 1) "labels"
            2) 1) "C"
         3) 1) "properties"
            2) 1) 1) "name"
                  2) "c1"
      3) 1) 1) "id"
            2) (integer) 5
         2) 1) "labels"
            2) 1) "T"
         3) 1) "properties"
            2) 1) 1) "name"
                  2) "t1"
   2) 1) "[(0), (1), (2)]"
      2) 1) 1) "id"
            2) (integer) 6
         2) 1) "labels"
            2) 1) "C"
         3) 1) "properties"
            2) 1) 1) "name"
                  2) "c2"
      3) (nil)
   3) 1) "[(0), (1), (2), (3)]"
      2) (nil)
      3) (nil)
3) 1) "Cached execution: 0"
   2) "Query internal execution time: 1.112000 milliseconds"```
1 Like

That’s what I need!

Thanks a million!