限制gremlin查询中group().by()中的项目数 [英] Limit number of items in group().by() in gremlin query
问题描述
我正在尝试运行 gremlin查询,该查询将某个标签的顶点分为几个组,通过某个字段(假设它是"displayName"),并限制 n 的组数,每个组中的项目数也 n .
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.
有没有办法实现这一目标?
由于group().by()返回该项目的列表,因此我尝试使用unfold(),然后对内部项目应用限制.我设法限制了返回的组的数量,但无法限制每个组中的项目数.
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)
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种.
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)
这篇关于限制gremlin查询中group().by()中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!