Gremlin:将边添加到多个顶点

Gremlin: Add edges to multiple vertices

我有顶点[song1, song2, song3, user]

我想将 user 中的边 listened 添加到歌曲中。

我有以下内容:

g.V().is(within(song1, song2, song3)).addE('listened').from(user)

但是我收到以下错误:

No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.from() is applicable for argument types: (org.janusgraph.graphdb.vertices.CacheVertex) values: [v[4344]] Possible solutions: sort(), drop(int), sum(), find(), grep(), sort(groovy.lang.Closure)

当然,我可以一次一个地遍历它们,但单个查询会更好:

user.addEdge('listened', song1)
user.addEdge('listened', song2)
user.addEdge('listened', song3)

from() 调制器接受两件事:

  1. 一个步骤标签或
  2. 一次遍历

单个顶点或顶点列表可以通过将其包装在 V() 中轻松转换为遍历。另外,请注意 g.V().is(within(...)) 很可能最终会成为对所有顶点的完整扫描;它在很大程度上取决于提供者的实现,但您应该更喜欢使用 g.V(<list of vertices>) 来代替。因此,您的遍历应该看起来更像以下任何一个:

g.V().is(within(song1, song2, song3)).
  addE('listened').from(V(user)) // actually bad, as it's potentially a full scan

g.V(song1, song2, song3).
  addE('listened').from(V(user))

g.V(user).as('u').
  V(within(song1, song2, song3)).
  addE('listened').from('u')