在 gremlin 查询中限制 group().by() 中的项目数

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

我正在尝试 运行 gremlin 查询 将某个标签的顶点分组到几个组中 字段(假设它是 'displayName')并将组数限制为 n,每组中的项目数也限制为 n .

有办法实现吗?

由于 group().by() returns 项目列表,我尝试使用 unfold() 然后对内部项目应用限制。我设法限制了返回的组数,但无法限制每个组中的项目数。

这是我用来限制组数的查询:

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
  ]
 }
]

目前,上面的查询,我只得到 2 组 "displayName1" 和 "displayName2",但是 每个都包含 所有 个项目,而不是预期的 2 个项目。

如果您想限制答案,您可以通过为组中的每个键定义值来实现:

g.V().hasLabel('customLabel')
       .group()
       .by('displayName')
       .by(identity().limit(n).fold())
        .unfold().limit(n)