How to do multiple hops irrespective of edge

Hi Guys, I have a graph with different three types of Nodes/vertices. for example , customer, physical address and email address. Customers may be linked to emails or to physical address. Now I want to extract all customers that may be linked to a particular physical address. I have been using the following query:

CREATE QUERY initial_queries(/* Parameters here */)

FOR GRAPH MyGraph SYNTAX(“v2”) {

Seed = {physical_address.*};

customers = SELECT p

         FROM Seed:s - (_*1..6) - customer_number:p

         WHERE [s.id](http://s.id/) == "6******" ;

PRINT  customers; 

}

But this returns only customers that are directly linked to the physical address.

I also want customers that can be indirectly linked to the physical address, like

  1. C1 is linked to physical address A1

  2. C1 and C2 are linked to email address E1

So based on this I want to extract a relationship that shows C2 is also linked to A1. Can anybody help with this ?

Hi Habib,

You can use this query:

CREATE QUERY customers(vertex<address> input) FOR GRAPH MyGraph {
SetAccum<vertex> @@customers;
OrAccum @visited;
 
Start (ANY) = {input};

while Start.size() > 0 do
 Start = Select t from Start:s-(:e)-:t
         where t.@visited == false
         accum t.@visited += true,
            if t.type == "customer" then
              @@customers += t
            end;
end;

print @@customers;
}

What we’re doing in this query is we traverse every connection starting from an initial address vertex. If a target vertex is of type customer, we add it to the SetAccum, which we print at the end. The loop exits when there are no more unvisited vertices to traverse to.
We’re using query syntax v1 for this query - v2’s performance is a bit slower than v1 at the moment. We are working on optimizing v2 performance.

ere is the effectively, the same query but done using v2 syntax in case you wanted to use the newer syntax:

CREATE QUERY customersv2(vertex<address> input) FOR GRAPH MyGraph SYNTAX v2 { 
	
   Seed = {input};
  
   customers = SELECT p
             FROM Seed:s - (_*1..6) - :p
             HAVING p.type == "customer";
            
    PRINT  customers; 
}

The only change is instead of specifying a target vertex type, we check for the type during traversal.

Will the first solution result in a very long running query?

What if i want to limit it saying I want to run it until i get all customers whose name starts with “A” or all customers whose gender is male