Following the route with N-nodes and getting all of the matched nodes

I have a tree without predefined depth and amount of nodes, like that:

image
Every node has an edge of the same type to its parent of the same type and a boolean parameter - 0 or 1.

I need to get all of the nodes that have it set to 1 and all of their parents should have it set to 1 as well.

The start of the route is always A1.

Thus, based on the example above, the result should be: A1, C1, F1, G1.

I saw some examples here:

https://docs.tigergraph.com/intro/gsql-102/multiple-hop-pattern

but it answers my questions partly and it’s actually hard to get how to do it.

Basically, I need to:

  1. Query N-hop with condition.

  2. Get all of the nodes that are found along the way.

Hi Oleg,

Thanks for your question. Please find the query example below:

CREATE QUERY test4(vertex inputVertex) FOR GRAPH MyGraph syntax(“v2”) {

SetAccum<vertex> @@vertex_set;

start = {inputVertex};

start = select t from start:s- (_>*)-:t
where t.boolAttr == 1
accum @@vertex_set += t;

print @@vertex_set;

}

However, for better performance, it is recommended to use syntax v1

CREATE QUERY test4(vertex inputVertex) FOR GRAPH MyGraph{ 

    SetAccum<vertex> @@vertex_set;

    start = {inputVertex};

    while(start.size() > 0) do
       start = select t from start:s- (:e)-:t
       where t.boolAttr == 1
       accum @@vertex_set += t;
    end;
    print @@vertex_set;
}

Please let me know if you have any questions.

Thanks.

Thanks for your help!

I still have 2 questions:

  1. How to do it when my boolean parameter is set on edge instead of vertex in case of v2 syntax?

start = select t from start:s- (_>*)-:t

I don’t see here a way to specify * edge as a field (it’s not allowed according to the docs) and then to add a condition to it.

  1. In the output I see only array of ids to vertices. How can I output actual vertices with their parameters?

Is there a way to have an accumulator not just as a vertex but as an array of vertices so I can output it and see the vertices instead of their ids?

Hi Oleg,

  1. As you can see here, we don’t allow alias for edge with Kleene star at this time but will support it in the future. If that’s your use case, you may want to stay with v1 syntax for now.

  2. To print out all attributes and vertex-attached accumulators of a vertex, you can create a Vertex Set from the SetAccum (check out S5 in the link):

    CREATE QUERY test4(vertex inputVertex) FOR GRAPH MyGraph{
    SetAccum @@vertex_set;

    start = {inputVertex};

    while(start.size() > 0) do
    start =
    select t from start:s- (:e)-:t
    where t.boolAttr == 1
    accum @@vertex_set += t;
    end;
    result = @@vertex_set; // convert to vertex set
    print result;
    }