Hi,
I am facing a weird situation with sack only being applied to last query when firing multiple queries with gremlin-http.
A User (vertex) -> Buys (edge) -> Item (vertex)
I need to create an edge if the user is buying this item for the first time and at the same time increment the property at Item vertex with unique_user_bought_count. I am using sack with Choose to figure out if the property exists then increment by 1 else create this property with a value of 1.
Lets say User ABC bought two items Item1 and Item2. I need to create an edge between ABC and Item1, Item2 if it does not exist and create/modify the unique_user_bought_count at Item1, Item2
This is my query. For ease of readability, I have broken down these queries in multiple lines. My payload to gremlin-http includes all these queries in one request.
// Check if user exists else create a vertex
user = g.V().has('userid', 'ABC').tryNext().orElseGet{graph.addVertex('userid', 'ABC')};
// Check if item exists else create a vertex
item1 = g.V().has('itemid', 'Item1').tryNext().orElseGet{graph.addVertex('itemid', 'Item1')}
// Check if edge exists else create the edge. Check if unique_user_bought_count property exists at Item Vertex to increment else create with value 1
edge1 = g.V().has('userid', 'ABC').out('bought', g.V().has('itemid', 'Item1').tryNext().orElseGet{
user.addEdge(item1);
g.withSack(0).V().has('itemid', 'Item1').choose(has('unique_user_bought_count'),
sack(assign).by('unique_user_bought_count').sack(sum).by(constant(1)).property('unique_user_bought_count',sack()),property('unique_user_bought_count',1))
};
// Create another item if it does not exist
item2 = g.V().has('itemid', 'Item2').tryNext().orElseGet{graph.addVertex('itemid', 'Item2')}
// Check if edge exists else create the edge. Check if unique_user_bought_count property exists at Item Vertex to increment else create with value 1
edge1 = g.V().has('userid', 'ABC').out('bought', g.V().has('itemid', 'Item2').tryNext().orElseGet{
user.addEdge(item2);
g.withSack(0).V().has('itemid', 'Item2').choose(has('unique_user_bought_count'),
sack(assign).by('unique_user_bought_count').sack(sum).by(constant(1)).property('unique_user_bought_count',sack()),property('unique_user_bought_count',1))
};
Problem - Edge from user -> item gets created correctly but the property gets updated only for the last item. Is there anything that I am doing wrong. I am assuming that we do not need to clone sack since this is primitive. But I did try with clone as well, but to no avail.
Can someone help me with this problem or point me where am I going wrong with the query?
Thanks in advance,
Mitesh