Gremlin 过滤器最大 属性
Gremlin filter by max property
gremlin 的新手,我一直无法按最大值过滤顶点。
简单的图表看起来像这样:
source.addV("x").property("id", "1").property("version", 1.0)
.addV("x").property("id", "1").property("version", 1.1)
.addV("x").property("id", "2").property("version", 1.0)
我的查询如下所示:
source.V()
.has(T.label, "x")
.group()
.by("id").unfold().where(select(Column.values).unfold().values("version").max())
我正在寻找的输出是
[{type:x, id:1, version:1.1},
{type:x, id:2, version:1.0}]
我的问题是它的投掷:
org.apache.tinkerpop.gremlin.driver.exception.ResponseException: {"requestId":"x","code":"InternalFailureException","detailedMessage":"null:select([null])"}
如有任何帮助,我们将不胜感激。谢谢
这里发生了几件事。
首先,您要添加没有标签的顶点,虽然允许这样做,但这种做法并不常见。您通常希望通过顶点标签将相似的项目组合在一起。您可以通过将标签名称放在 addV() 步骤中来为遍历添加顶点标签,如下所示:
g.addV("labelname")
其次,您将版本属性添加为字符串而不是数字,这是我假设您实际想要的,因为字符串的 max() 并没有真正意义。要更改此设置,请删除版本号周围的引号,如下所示:
g.addV().property("type", "x").property("id", "1").property("version", 1.0)
第三,考虑这个问题的方式与您预期的有点不同。与其试图找到最大版本号并将所有遍历器与它进行比较,不如考虑这个问题的方式是 "Find me the vertex with the highest version, then for each vertex see if it's version number is equal"。绕过这个有点困难,但这里有一个遍历演示了如何做到这一点:
g.V().
order().
by('version', desc).
limit(1).as('b').
V().as('a').
where('a', eq('b')).
by('version')
由于 ID 是唯一值,我假设您指的是自定义 ID 属性。我们将其命名为 _id
.
您可以 group
按 _id
然后按 version
排序答案:
g.V().hasLabel('x').
group().by('_id').
by(fold().order(local).by('version', desc).unfold()
limit(1).valueMap().
with(WithOptions.tokens)).select(values)
gremlin 的新手,我一直无法按最大值过滤顶点。
简单的图表看起来像这样:
source.addV("x").property("id", "1").property("version", 1.0)
.addV("x").property("id", "1").property("version", 1.1)
.addV("x").property("id", "2").property("version", 1.0)
我的查询如下所示:
source.V()
.has(T.label, "x")
.group()
.by("id").unfold().where(select(Column.values).unfold().values("version").max())
我正在寻找的输出是
[{type:x, id:1, version:1.1}, {type:x, id:2, version:1.0}]
我的问题是它的投掷:
org.apache.tinkerpop.gremlin.driver.exception.ResponseException: {"requestId":"x","code":"InternalFailureException","detailedMessage":"null:select([null])"}
如有任何帮助,我们将不胜感激。谢谢
这里发生了几件事。
首先,您要添加没有标签的顶点,虽然允许这样做,但这种做法并不常见。您通常希望通过顶点标签将相似的项目组合在一起。您可以通过将标签名称放在 addV() 步骤中来为遍历添加顶点标签,如下所示:
g.addV("labelname")
其次,您将版本属性添加为字符串而不是数字,这是我假设您实际想要的,因为字符串的 max() 并没有真正意义。要更改此设置,请删除版本号周围的引号,如下所示:
g.addV().property("type", "x").property("id", "1").property("version", 1.0)
第三,考虑这个问题的方式与您预期的有点不同。与其试图找到最大版本号并将所有遍历器与它进行比较,不如考虑这个问题的方式是 "Find me the vertex with the highest version, then for each vertex see if it's version number is equal"。绕过这个有点困难,但这里有一个遍历演示了如何做到这一点:
g.V().
order().
by('version', desc).
limit(1).as('b').
V().as('a').
where('a', eq('b')).
by('version')
由于 ID 是唯一值,我假设您指的是自定义 ID 属性。我们将其命名为 _id
.
您可以 group
按 _id
然后按 version
排序答案:
g.V().hasLabel('x').
group().by('_id').
by(fold().order(local).by('version', desc).unfold()
limit(1).valueMap().
with(WithOptions.tokens)).select(values)