I am trying to run a gremlin query which groups vertices of a certain label into several groups by a certain field (assume it is 'displayName') and limit the number of groups to n and the number of items in each group also to n.
Is there a way to achieve that?
Since group().by() returns a list of the item, I tried using unfold() and then applying limit on the inner items. I managed to limit the number of groups that are returned, but couldn't limit the number of items in each group.
Here's the query I used to limit the number of groups:
g.V().hasLabel('customLabel').group().by('displayName').unfold().limit(n)
// Expected result:(if n == 2)
[
{
"displayName1": [
{ // item 1 in first group
},
{ // item 2 in first group
}
]
},
{
"displayName2": [
{ // item 1 in second group
},
{ // item 2 in second group
}
]
}
]
// Actual result: (when n == 2)
[
{
"displayName1": [
{ // item 1 in first group
},
{ // item 2 in first group
},
... // all the items are included in the result
]
},
{
"displayName2": [
{ // item 1 in second group
},
{ // item 2 in second group
},
... // all the items are included in the result
]
}
]
Currently, with the query above, I get only 2 groups "displayName1" and "displayName2", but each one contains all the items in it and not only 2 as expected.
If you want to limit the answer you can do it by defining the values for each key in the group:
g.V().hasLabel('customLabel')
.group()
.by('displayName')
.by(identity().limit(n).fold())
.unfold().limit(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With