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

时间:2019-07-14 13:21:59

标签: group-by limit gremlin azure-cosmosdb-gremlinapi

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

有没有办法实现这一目标?

由于group()。by()返回该项目的列表,因此我尝试使用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种。

1 个答案:

答案 0 :(得分:0)

如果要限制答案,可以通过定义组中每个键的值来做到:

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

相关问题