首页 > 解决方案 > 如何使用展开列表值来指定顶点?

问题描述

我正在尝试编写一个 Gremlin 遍历,意思是“将所有这些身份添加到这个组中”,在食谱中的“注入列表”示例之后对其进行模式化。这是我正在尝试的逻辑:

List identityIds = [...]

gts.inject(identityIds).unfold()
  .V() // filter statement goes here?
  .addE(GROUP_INCLUDES_IDENTITY).from(V(groupId))
  .iterate()

据我了解,这将为列表中的每个元素生成一个遍历器并执行addE操作。但是,我无法弄清楚如何V().hasId(it)在遍历中表达。该identity()步骤似乎应该适用,但我不能将其用作Vor的参数hasId

我尝试使用 来形成表达式unfold().as("id"),但我遇到了同样的问题,即select()返回遍历并且我不能在明显的地方使用遍历。

标签: javagroovygremlintinkerpop3

解决方案


你可以where()用来做你想做的事:

gremlin> g.inject(1,2).as('id').
......1>   V().as('v').
......2>   where('id',eq('v')).
......3>     by().by(id)
==>v[1]
==>v[2]

但这并不理想,因为这种遍历不太可能被优化以使用索引。

我认为此时最好的方法是使用V(identityIds). 您提到了一个更复杂的案例,并且通常该复杂案例与想要传递带有 id 的附加数据(通常是属性)有关,在这种情况下,我仍然会坚持V(identityIds)并简单地将附加数据视为副作用:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> data = [[i: 1, w: 0.99],[i: 2, w: 0.98]]
==>[i:1,w:0.99]
==>[i:2,w:0.98]
gremlin> g.withSideEffect('d',data).
......1>   V(data.collect{it.i}).as('v').
......2>   addE('knows').to(V(6)).as('e').
......3>   select('d').unfold().as('p').
......4>   where('p',eq('v')).
......5>     by('i').by(id).as('weight').
......6>   select('e').
......7>   property('weight',select('weight'))
==>e[13][1-knows->6]
==>e[14][2-knows->6]
gremlin> g.E(13,14).elementMap()
==>[id:13,label:knows,IN:[id:6,label:person],OUT:[id:1,label:person],weight:[i:1,w:0.99]]
==>[id:14,label:knows,IN:[id:6,label:person],OUT:[id:2,label:person],weight:[i:2,w:0.98]]

可能有更好的写法,但一般的想法是你确保你点击了顶点查找的索引,V(ids)然后将你想要使用的数据视为某种副作用,你必须匹配顶点id 到,然后Map你可以更新你想要的任何属性。这篇博客文章更详细地描述了插入的一般方法。


推荐阅读