Hi, I am trying to create a query where edge_name is passed as an input parameter. The query calculates a score which is finally stored on “edge_name”.
In the graph shown above, a score is calculated either for “category” or “department” level and then saved on the respective edge.
Below is the query :
CREATE QUERY test(String level, String edge_name) FOR GRAPH RFM {
/* Write query logic here */
MapAccum<String, SumAccum<DOUBLE>> @rel_score;
customers = {customer.*};
customer_product_var = select s from customer:s ACCUM
s.@rel_score+=("a"->1.0);
customer_product_var = select s from customer:s ACCUM
foreach (key,val) in s.@rel_score DO
insert into EDGE edge_name values(s.id customer, key level, val)
end;
print "test";
}
The above query shows below error :
(9, 26) Error: Edge ‘edge_name’ valid TO vertex types are [product, orders, category, department], so TO vertex type hint must be provided for string type expression.
I need to create an edge, either “customer_category” or “customer_department”, depending upon the value of input parameter level (“category” or “department”). For that, the value of “level” should be dynamically interpreted in below query insert into EDGE edge_name values(s.id customer, key level, val).
Hi team,
I am also looking for this type of use case where we can do Dynamic vertex creation
based on Vertex name passed as a input parameter.
here my vertex is already created in the schema. I just want to provide the name
dynamically.
INSERT INTO “profile_calendar_”+ arr.getJsonObject(cnt).getString + “_affinity”
@mt_nagarro can you please let me know what solution you have applied for this ? @Jon_Herke
@mt_nagarro Could you try running the following query from the Graph Data Science library? It has many of the same elements as your query including dynamic edges, vertex and using the INSERT INTO dynamically.
Others = SELECT s
FROM Others:s
POST-ACCUM
IF similarity_edge_type != "" THEN
INSERT INTO EDGE similarity_edge_type VALUES (source, s, s.@sum_similarity)
END,
We took a look into the query and it seems what’s causing the issue is dynamically assigning the vertex type in the INSERT INTO segment of the query.
INSERT INTO EDGE should be followed up with the VALUES (source_vertex_id, source_vertex_type, destination_vertex_id, destination_vertex_type, attribute_values_if_any), however, it will also work when given vertex objects instead of just id’s and types such as: VALUES (source_vertex, destination_vertex, attribute_values_if_any)
The issue with using the first method in your query is that vertex_type cannot be expressed by a variable. meaning that dynamically creating the edge in your case would not work.
We can get around this by using the second method and specifying a list of vertex objects for the INSERT INTO to interact with. It’s also important to note that the attribute that will be modified on the edge will need to exist in the schema before a value can be added to it by the query.
CREATE QUERY test(string level, string edge_name) FOR GRAPH MyGraph {
MapAccum<VERTEX, SumAccum<DOUBLE>> @@rel_score;
customers = {person.*};
level_1 = {level};
customer_product_var = select s from person:s - () - level_1 ACCUM
@@rel_score+=(s ->1.0);
customer_product_var = select s from level_1:s POST-ACCUM
foreach (key,val) in @@rel_score DO
insert into EDGE edge_name VALUES(key, s, _)
end;
print customer_product_var;
}
Breakdown:
The line level_1 = {level}; converts the input string of the level variable into a vertex set of all vertices of type matching the input string value.
The mapaccum has been changed to accept a VERTEX as the key. This vertex will be of the type specified by the level variable
The FOR EACH iterates over the same mapaccum, but is now able to use the VERTEX from the key as an input for the source_vertex, additionally we can also just use the s vertex alias from the SELECT as the second VERTEX input.