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

1

7

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.

Czardom answered 14/7, 2019 at 13:21 Comment(0)
M
3

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)

Malemute answered 14/7, 2019 at 14:9 Comment(1)
How to get the actual verices values inside the group ? I only see the label and id .. now ..Juncaceous

© 2022 - 2024 — McMap. All rights reserved.