Get list of paths

Yesterday I returned a path/list of vertices and edges by passing in a starting vertex (word) and doing a loop until we found the ending vertex where it’s “Text” attribute had a value of “width”.

CREATE QUERY wordpathtext3(vertex<word> w) for graph social {
 // Input value for test 11175600
 SetAccum<edge> @@edgeSet;
 SetAccum<vertex> @@vertexSet; 
 Start (ANY) = {w}; 
 WHILE wordValue != "width" DO
   Start = SELECT t FROM Start:s - (nextword:e) - word:t
     ACCUM @@edgeSet += e,
     @@vertexSet += t;
 END;
 PRINT @@vertexSet;
 PRINT @@edgeSet; 
}

Now I want to modify this so rather than passing in vertex as a parameter, I want to pass in 2 strings:
StartText = “except”
EndText = “width”

I think we would want to get an Accum of start vertices (I dont know how to write this in GSQL so here’s the SQL version):

SELECT word
FROM MyTableOfWords
WHERE word = ‘except’

Then use that accum variable to get a list of paths that end with the word “width” or which would look something like this:
“except this is my sample path width”
“except this widget will be 10 feet in width”
“except this widget will be ten feet in width”
etc.

But of course this would be a graph of several groups of vertices representing the phrases above.

And we need to add a limit of 20 so we don’t end in an endless loop.

Can you please shoe me how this would be written?

To get the starting vertex, you can do this:

create query test ( String startingWord , String endWord) for graph whatevergraph {

Start = {word.*};

Start = select s from Start:s where s.Text == startingWord;

}

This will save all the vertices with a Text attribute of the starting word (or except).

Thanks,

Kevin

Thanks Kevin, making progress.

That returned 4 vertices as expected. Now I tried to apply the rest of the logic shown below and expected to get 4 paths or 4 groups (not how you’re supposed to say this…) of vertices (a path for each of those 4 returned).

However, I got very interesting results.The first time it timed out. so I put a limit of 10 in the loop and it returned 1536 vertices. I was expecting maybe 40 total. Can you please show me where i went wrong?

CREATE QUERY aFindPath(String startingWord, String endWord) FOR GRAPH social {
      string wordValue;

      SetAccum<edge> @@edgeSet;
      SetAccum<vertex> @@vertexSet;   
      Start = {word.*};
      WHILE wordValue != "width" LIMIT 10 DO

        Start = SELECT t FROM Start:s - (nextword:e) - word:t
        ACCUM @@edgeSet += e,
        @@vertexSet += t;
      END;
    PRINT @@vertexSet;
    PRINT @@edgeSet;
}

Hi George,

You should use the code I replied with above before the WHILE loop. That’s to select all the vertices with a specific text attribute.

Since you’re going to be starting with multiple vertices and ending at different edges with a different amount of edges, we’ll have to change the query a bit.

If we check Start.size() > 0 , the while loop will exit once we encounter a word vertex with a word that matches the one you want.

This is possible by using the HAVING clause, read more about it here : https://docs.tigergraph.com/dev/gsql-ref/querying/select-statement#having-clause

You can use this query:

CREATE QUERY test(String startWord, String endWord) FOR GRAPH MyGraph { 

SetAccum<edge> @@edgeSet;

SetAccum<vertex> @@vSet;

Start (ANY) = {word.*};

Start = select s from Start:s where s.test == startWord;

while  **Start.size() > 0**  limit 20 do 

  Start = select t from Start:s-(ctoc:e)-customer:t

          accum @@edgeSet += e, 

                              @@vSet += t

  **having t.test != endWord;**

end;

vSet = @@vSet;

print @@edgeSet, vSet;

}

Kevin,

It looks like you coped code from some other sample so it doesn’t apply directly to mine. anyway, i tried to modify you code so it would compile but I’m getting this error. “Text” is the vertex’s attribute. it might be better to fix my sample from the previous post above.

I’m using a testing instance with a totally different schema I just modified to write a query for you… Just the one word is off… the logic is the important part isn’t it?

CREATE QUERY  **aFindPath** (String startingWord, String endWord) FOR GRAPH MyGraph { 

SetAccum<edge> @@edgeSet;

SetAccum<vertex> @@vSet;

Start (ANY) = {word.*};

Start = select s from Start:s where s. **Text**  == startingWord;

while Start.size() > 0 limit 20 do 

  Start = select t from Start:s-(newtWord:e)-word:t

          accum @@edgeSet += e, 

                              @@vSet += t

          having t.test != endWord;

end;

vSet = @@vSet;

print @@edgeSet, vSet;

}

Hope this works for you.

Kevin

Thanks Kevin, yes the logic is what’s important and thank you for helping to flush out my types.

Glad to see everything works!

Hello @Jon_Herke
Can you please help me to find out the function which returns path from the given source and destination vertex (absolute path from source t destination vertex) . It will be very helpful for me. Suppose we have a Account vertex and City vertex and State vertex and Country vertex
function(Account, City)-> Account:a-(lives_in:l)-City:c-(state:ci)-…Country:c

Thanks

Does this post meet your needs? WHERE clause didn't work as expected

@pkr2 I would also suggest checking out the docs for path finding https://docs.tigergraph.com/tigergraph-platform-overview/graph-algorithm-library#path-algorithms

Code for path finding algos: https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Path

Thanks for your response,
I just want a select query corresponding to given vertex to destination vertex

   function_to_get_select_path(Account, State)

Account-a-(lives_in:l)-City:c-(city_in:ci)-State:s