I am very new to gremlin and currently am doing a proof of concept on Azure CosmosDB. My graph looks like this:
Add vertices of type object1:
g.addV(‘object1’).property('id','1').addV(‘object1’).property('id’,’2’)
Add vertices of type object2:
g.addV(‘object2’).property('id','1').property(‘date’, ’01-08–2023’).property(‘type’, ’ordinary’).property(’object1’, ’1’)
.addV(‘object2’).property('id’, ’2’).property(‘date’, ’02-08–2023’).property(‘type’, ’special’).property(,’object1’, ‘1’)
.addV(‘object2’).property('id’, ’3’).property(‘date’, ’31-07–2023’).property(‘type’, ’ordinary’).property(’object1’, ‘1’)
.addV(‘object2’).property('id’,’4’).property(‘date’, ’01-07–2023’).property(‘type’, ’ordinary’).property(’object1’, ‘2’)
.addV(‘object2’).property('id’,’5’).property(‘date’, ’02-07–2023’).property(‘type’, ’special’).property(’object1’, ‘2’)
.addV(‘object2’).property('id’,’6’).property(‘date’, ’30-06–2023’).property(‘type’, ’ordinary’).property(’object1’, ‘2’)
Add relations between object2 and object1:
g.V().hasLabel(‘object1’).has('id', '1').addE('SCANNED').from(g.V().hasLabel(‘object2’).has('id', '1')).inV().addE('SCANNED').from(g.V().hasLabel(‘object2’).has('id', '2').from(g.V().hasLabel(‘object2’).has('id', ‘3’)
g.V().hasLabel(‘object1’).has('id', ‘2’).addE('SCANNED').from(g.V().hasLabel(‘object2’).has('id', ‘4’)).inV().addE('SCANNED').from(g.V().hasLabel(‘object2’).has('id', ‘5’).from(g.V().hasLabel(‘object2’).has('id', ‘6’)
As seen:
object2 vertices having ids [1,2,3] —SCANNED—> object1 [id:1]
object2 vertices having ids [4,5,6] —SCANNED—> object1 [id:2]
What I need is:
For each object1:
- find out the latest object2 having type “ordinary”
- then, find out the latest object2 having type “special” (which is also later than the object2 in step 1)
- Collect these object2 vertices from step1 and step2
So the output should be: [object2 id:1, object2 id:2, object2 id:4, object2 id:5]
Object2 will have other relations which will be traversed once the output in the above form is obtained.
What I have tried is the grouping by, that works:
g.V().hasLabel(“object1”).in(“SCANNED”).group().by(“object1”)
Then it gets grouped by neatly per object 1 i.e
[
{
1: [
{ ‘id’ : ‘1’, ‘date’: ’01-08-2023’, ‘type’: ‘ordinary’, “object1”: ‘1’},
{ ‘id’ : ‘2’, ‘date’: ’02-08-2023’, ‘type’: ‘ordinary’, “object1”: ‘1’},
{ ‘id’ : ‘3’, ‘date’: ’31-07-2023’, ‘type’: ‘ordinary’, “object1”: ‘1’}
],
2:[
{ ‘id’ : ‘4’, ‘date’: ’01-07-2023’, ‘type’: ‘ordinary’, “object1”: ‘2’},
{ ‘id’ : ‘5’, ‘date’: ’02-07-2023’, ‘type’: ‘ordinary’, “object1”: ‘2’},
{ ‘id’ : ‘6’, ‘date’: ’30-06-2023’, ‘type’: ‘ordinary’, “object1”: ‘2’}
]
}
]
Now, for both object1s:
- How do I order the object2s to get the latest of type ordinary?
- How do I get the latest object2 of type special that comes later than the object2 in step1 ?
- Retrieve these object2s so that it can be used for further traversal ?
I was able to do this by the following approach:
###Explanation:###