Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of items in group().by() in gremlin query

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.

like image 795
Laith Hisham Avatar asked Jul 14 '19 13:07

Laith Hisham


1 Answers

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)

like image 175
noam621 Avatar answered Sep 28 '22 01:09

noam621